Storing local paths when possible in media

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2013-04-19 17:39:23 -07:00
parent 68cf505426
commit eb4c452003
5 changed files with 28 additions and 5 deletions

View File

@@ -276,6 +276,11 @@ namespace Orchard.Tests.Modules.Media.Services {
public string GetLocalPath(string url) {
throw new NotImplementedException();
}
public string GetRelativePath(string path) {
throw new NotImplementedException();
}
}
private class StubStorageFolder : IStorageFolder {

View File

@@ -54,7 +54,7 @@ namespace Orchard.MediaLibrary.Factories {
_storageProvider.CreateFolder(BaseFolder);
}
part.Resource = _storageProvider.GetPublicUrl(_storageProvider.Combine(BaseFolder, uniquePath));
part.Resource = _storageProvider.GetRelativePath(_storageProvider.Combine(BaseFolder, uniquePath));
part.MimeType = mimeType;
part.Title = Path.GetFileNameWithoutExtension(path);

View File

@@ -177,6 +177,10 @@ namespace Orchard.MediaProcessing.Shapes {
// /OrchardLocal/images/my-image.jpg
if (Uri.IsWellFormedUriString(path, UriKind.Relative)) {
path = storageProvider.Value.GetLocalPath(path);
// images/my-image.jpg
var file = storageProvider.Value.GetFile(path);
return file.OpenRead();
}
// http://blob.storage-provider.net/my-image.jpg
@@ -190,9 +194,7 @@ namespace Orchard.MediaProcessing.Shapes {
return webClient.OpenRead(new Uri(request.Url, VirtualPathUtility.ToAbsolute(path)));
}
// images/my-image.jpg
var file = storageProvider.Value.GetFile(path);
return file.OpenRead();
throw new ArgumentException("invalid path");
}
private static string CreateDefaultFileName(string path) {

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using Orchard.Environment.Configuration;
using Orchard.Localization;
@@ -12,6 +11,7 @@ namespace Orchard.FileSystems.Media {
public class FileSystemStorageProvider : IStorageProvider {
private readonly string _storagePath;
private readonly string _publicPath;
private readonly string _relativePath;
public FileSystemStorageProvider(ShellSettings settings) {
var mediaPath = HostingEnvironment.IsHosted
@@ -29,6 +29,7 @@ namespace Orchard.FileSystems.Media {
if (!appPath.StartsWith("/"))
appPath = '/' + appPath;
_relativePath = "~/Media/" + settings.Name + "/";
_publicPath = appPath + "Media/" + settings.Name + "/";
T = NullLocalizer.Instance;
@@ -83,12 +84,20 @@ namespace Orchard.FileSystems.Media {
return MapPublic(path);
}
public string GetRelativePath(string path) {
return (_relativePath + path).Replace(Path.DirectorySeparatorChar, '/');
}
/// <summary>
/// Retrieves the local path for a given url within the storage provider.
/// </summary>
/// <param name="url">The public url of the media.</param>
/// <returns>The local path.</returns>
public string GetLocalPath(string url) {
if (url.StartsWith(_relativePath)) {
return url.Substring(_relativePath.Length);
}
if (!url.StartsWith(_publicPath)) {
return url;
}

View File

@@ -25,6 +25,13 @@ namespace Orchard.FileSystems.Media {
/// <returns>The local path.</returns>
string GetLocalPath(string url);
/// <summary>
/// Retrieves the relative path for a given url within the storage provider.
/// </summary>
/// <param name="path">The relative path withing the storage provider.</param>
/// <returns>The relative path, or null if the .</returns>
string GetRelativePath(string path);
/// <summary>
/// Retrieves a file within the storage provider.
/// </summary>