mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-24 05:23:33 +08:00
Adjusting Orchard.Media module for storage path conventions
IMediaService no longer calculates root path as physical folder Public urls are provided by IStorageProvider --HG-- branch : dev
This commit is contained in:
25
src/Orchard.Specs/Media.feature
Normal file
25
src/Orchard.Specs/Media.feature
Normal file
@@ -0,0 +1,25 @@
|
||||
Feature: Media management
|
||||
In order to reference images and such from content
|
||||
As an author
|
||||
I want to upload and manage files in a media folder
|
||||
|
||||
Scenario: Media admin is available
|
||||
Given I have installed Orchard
|
||||
And I have installed "Orchard.Media"
|
||||
When I go to "admin/media"
|
||||
Then I should see "Manage Media Folders"
|
||||
And the status should be 200 OK
|
||||
|
||||
Scenario: Creating a folder
|
||||
Given I have installed Orchard
|
||||
And I have installed "Orchard.Media"
|
||||
When I go to "admin/media/create"
|
||||
And I fill in
|
||||
| name | value |
|
||||
| Name | Hello World |
|
||||
And I hit "Save"
|
||||
And I am redirected
|
||||
Then I should see "Manage Media Folders"
|
||||
And I should see "Hello World"
|
||||
And the status should be 200 OK
|
||||
|
110
src/Orchard.Specs/Media.feature.cs
generated
Normal file
110
src/Orchard.Specs/Media.feature.cs
generated
Normal file
@@ -0,0 +1,110 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by SpecFlow (http://www.specflow.org/).
|
||||
// SpecFlow Version:1.2.0.0
|
||||
// Runtime Version:2.0.50727.4927
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
// ------------------------------------------------------------------------------
|
||||
namespace Orchard.Specs
|
||||
{
|
||||
using TechTalk.SpecFlow;
|
||||
|
||||
|
||||
[NUnit.Framework.TestFixtureAttribute()]
|
||||
[NUnit.Framework.DescriptionAttribute("Media management")]
|
||||
public partial class MediaManagementFeature
|
||||
{
|
||||
|
||||
private static TechTalk.SpecFlow.ITestRunner testRunner;
|
||||
|
||||
#line 1 "Media.feature"
|
||||
#line hidden
|
||||
|
||||
[NUnit.Framework.TestFixtureSetUpAttribute()]
|
||||
public virtual void FeatureSetup()
|
||||
{
|
||||
testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner();
|
||||
TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "Media management", "In order to reference images and such from content\r\nAs an author\r\nI want to uploa" +
|
||||
"d and manage files in a media folder", ((string[])(null)));
|
||||
testRunner.OnFeatureStart(featureInfo);
|
||||
}
|
||||
|
||||
[NUnit.Framework.TestFixtureTearDownAttribute()]
|
||||
public virtual void FeatureTearDown()
|
||||
{
|
||||
testRunner.OnFeatureEnd();
|
||||
testRunner = null;
|
||||
}
|
||||
|
||||
public virtual void ScenarioSetup(TechTalk.SpecFlow.ScenarioInfo scenarioInfo)
|
||||
{
|
||||
testRunner.OnScenarioStart(scenarioInfo);
|
||||
}
|
||||
|
||||
[NUnit.Framework.TearDownAttribute()]
|
||||
public virtual void ScenarioTearDown()
|
||||
{
|
||||
testRunner.OnScenarioEnd();
|
||||
}
|
||||
|
||||
[NUnit.Framework.TestAttribute()]
|
||||
[NUnit.Framework.DescriptionAttribute("Media admin is available")]
|
||||
public virtual void MediaAdminIsAvailable()
|
||||
{
|
||||
TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Media admin is available", ((string[])(null)));
|
||||
#line 6
|
||||
this.ScenarioSetup(scenarioInfo);
|
||||
#line 7
|
||||
testRunner.Given("I have installed Orchard");
|
||||
#line 8
|
||||
testRunner.And("I have installed \"Orchard.Media\"");
|
||||
#line 9
|
||||
testRunner.When("I go to \"admin/media\"");
|
||||
#line 10
|
||||
testRunner.Then("I should see \"Manage Media Folders\"");
|
||||
#line 11
|
||||
testRunner.And("the status should be 200 OK");
|
||||
#line hidden
|
||||
testRunner.CollectScenarioErrors();
|
||||
}
|
||||
|
||||
[NUnit.Framework.TestAttribute()]
|
||||
[NUnit.Framework.DescriptionAttribute("Creating a folder")]
|
||||
public virtual void CreatingAFolder()
|
||||
{
|
||||
TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Creating a folder", ((string[])(null)));
|
||||
#line 13
|
||||
this.ScenarioSetup(scenarioInfo);
|
||||
#line 14
|
||||
testRunner.Given("I have installed Orchard");
|
||||
#line 15
|
||||
testRunner.And("I have installed \"Orchard.Media\"");
|
||||
#line 16
|
||||
testRunner.When("I go to \"admin/media/create\"");
|
||||
#line hidden
|
||||
TechTalk.SpecFlow.Table table1 = new TechTalk.SpecFlow.Table(new string[] {
|
||||
"name",
|
||||
"value"});
|
||||
table1.AddRow(new string[] {
|
||||
"Name",
|
||||
"Hello World"});
|
||||
#line 17
|
||||
testRunner.And("I fill in", ((string)(null)), table1);
|
||||
#line 20
|
||||
testRunner.And("I hit \"Save\"");
|
||||
#line 21
|
||||
testRunner.And("I am redirected");
|
||||
#line 22
|
||||
testRunner.Then("I should see \"Manage Media Folders\"");
|
||||
#line 23
|
||||
testRunner.And("I should see \"Hello World\"");
|
||||
#line 24
|
||||
testRunner.And("the status should be 200 OK");
|
||||
#line hidden
|
||||
testRunner.CollectScenarioErrors();
|
||||
}
|
||||
}
|
||||
}
|
@@ -112,6 +112,11 @@
|
||||
<Compile Include="Hosting\RequestExtensions.cs" />
|
||||
<Compile Include="Hosting\RequestDetails.cs" />
|
||||
<Compile Include="Hosting\Simple.Web\Global.asax.cs" />
|
||||
<Compile Include="Media.feature.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>Media.feature</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Modules.feature.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
@@ -171,6 +176,10 @@
|
||||
<Content Include="Hosting\Orchard.Web\Config\Host.config">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<None Include="Media.feature">
|
||||
<Generator>SpecFlowSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Media.feature.cs</LastGenOutput>
|
||||
</None>
|
||||
<None Include="Modules.feature">
|
||||
<Generator>SpecFlowSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Modules.feature.cs</LastGenOutput>
|
||||
|
@@ -186,9 +186,9 @@ namespace Orchard.Media.Controllers {
|
||||
}
|
||||
|
||||
var file = Request.Files[0];
|
||||
_mediaService.UploadMediaFile(viewModel.MediaPath, file);
|
||||
var publicUrl = _mediaService.UploadMediaFile(viewModel.MediaPath, file);
|
||||
|
||||
return Content(string.Format("<script type=\"text/javascript\">var result = {{ url: \"{0}\" }};</script>", Path.Combine(_mediaService.GetRootUrl(), string.Format("{0}/{1}", viewModel.MediaPath, Path.GetFileName(file.FileName)).Replace("//", "/")).Replace("\\", "/")));
|
||||
return Content(string.Format("<script type=\"text/javascript\">var result = {{ url: \"{0}\" }};</script>", publicUrl));
|
||||
}
|
||||
catch (Exception exception) {
|
||||
return Content(string.Format("<script type=\"text/javascript\">var result = {{ error: \"{0}\" }};</script>", T("ERROR: Uploading media file failed: {0}", exception.Message)));
|
||||
@@ -203,6 +203,7 @@ namespace Orchard.Media.Controllers {
|
||||
model.Size = size;
|
||||
model.FolderName = folderName;
|
||||
model.MediaPath = mediaPath;
|
||||
model.PublicUrl = _mediaService.GetPublicUrl(Path.Combine(mediaPath, name));
|
||||
return View(model);
|
||||
}
|
||||
|
||||
|
@@ -4,7 +4,7 @@ using Orchard.Media.Models;
|
||||
|
||||
namespace Orchard.Media.Services {
|
||||
public interface IMediaService : IDependency {
|
||||
string GetRootUrl();
|
||||
string GetPublicUrl(string path);
|
||||
IEnumerable<MediaFolder> GetMediaFolders(string path);
|
||||
IEnumerable<MediaFile> GetMediaFiles(string path);
|
||||
void CreateFolder(string path, string name);
|
||||
@@ -12,6 +12,6 @@ namespace Orchard.Media.Services {
|
||||
void RenameFolder(string path, string newName);
|
||||
void DeleteFile(string name, string folderName);
|
||||
void RenameFile(string name, string newName, string folderName);
|
||||
void UploadMediaFile(string folderName, HttpPostedFileBase postedFile);
|
||||
string UploadMediaFile(string folderName, HttpPostedFileBase postedFile);
|
||||
}
|
||||
}
|
@@ -13,38 +13,29 @@ namespace Orchard.Media.Services {
|
||||
[UsedImplicitly]
|
||||
public class MediaService : IMediaService {
|
||||
private readonly IStorageProvider _storageProvider;
|
||||
private readonly string _rootPath;
|
||||
private readonly string _rootUrl;
|
||||
|
||||
public MediaService (
|
||||
public MediaService(
|
||||
IStorageProvider storageProvider) {
|
||||
_storageProvider = storageProvider;
|
||||
_rootPath = HttpContext.Current.Server.MapPath("~/Media");
|
||||
_rootUrl = Path.Combine(HttpContext.Current.Request.ApplicationPath, "Media").Replace("\\", "/");
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
public string GetRootUrl() {
|
||||
return _rootUrl;
|
||||
public string GetPublicUrl(string path) {
|
||||
return _storageProvider.GetPublicUrl(path);
|
||||
}
|
||||
|
||||
public IEnumerable<MediaFolder> GetMediaFolders(string path) {
|
||||
var mediaFolders = new List<MediaFolder>();
|
||||
var folders = (
|
||||
path == null ?
|
||||
_storageProvider.ListFolders(_rootPath) :
|
||||
_storageProvider.ListFolders(_rootPath + "\\" + path));
|
||||
var folders = _storageProvider.ListFolders(path);
|
||||
|
||||
foreach (var folder in folders) {
|
||||
var parentHierarchy = GetParentHierarchy(folder);
|
||||
var mediaPath = GetMediaPath(parentHierarchy, folder.GetName());
|
||||
var mediaFolder = new MediaFolder {
|
||||
Name = folder.GetName(),
|
||||
Size = folder.GetSize(),
|
||||
LastUpdated = folder.GetLastUpdated(),
|
||||
MediaPath = mediaPath
|
||||
MediaPath = folder.GetPath(),
|
||||
};
|
||||
mediaFolders.Add(mediaFolder);
|
||||
}
|
||||
@@ -54,7 +45,7 @@ namespace Orchard.Media.Services {
|
||||
public IEnumerable<MediaFile> GetMediaFiles(string path) {
|
||||
var mediaFiles = new List<MediaFile>();
|
||||
|
||||
var files = _storageProvider.ListFiles(_rootPath + "\\" + path);
|
||||
var files = _storageProvider.ListFiles(path);
|
||||
foreach (var file in files) {
|
||||
var mediaFile = new MediaFile {
|
||||
Name = file.GetName(),
|
||||
@@ -71,44 +62,63 @@ namespace Orchard.Media.Services {
|
||||
//TODO: Use Path.Combine.
|
||||
public void CreateFolder(string mediaPath, string name) {
|
||||
if (String.IsNullOrEmpty(mediaPath)) {
|
||||
_storageProvider.CreateFolder(_rootPath + "\\" + name);
|
||||
_storageProvider.CreateFolder(name);
|
||||
return;
|
||||
}
|
||||
_storageProvider.CreateFolder(_rootPath + "\\" + mediaPath + "\\" + name);
|
||||
_storageProvider.CreateFolder(mediaPath + "\\" + name);
|
||||
}
|
||||
|
||||
public void DeleteFolder(string name) {
|
||||
_storageProvider.DeleteFolder(_rootPath + "\\" + name);
|
||||
_storageProvider.DeleteFolder(name);
|
||||
}
|
||||
|
||||
public void RenameFolder(string path, string newName) {
|
||||
var newPath = RenameFolderPath(path, newName);
|
||||
_storageProvider.RenameFolder(_rootPath + "\\" + path, _rootPath + "\\" + newPath);
|
||||
_storageProvider.RenameFolder(path, newPath);
|
||||
}
|
||||
|
||||
public void DeleteFile(string name, string folderName) {
|
||||
_storageProvider.DeleteFile(_rootPath + "\\" + folderName + "\\" + name);
|
||||
_storageProvider.DeleteFile(folderName + "\\" + name);
|
||||
}
|
||||
|
||||
public void RenameFile(string name, string newName, string folderName) {
|
||||
_storageProvider.RenameFile(_rootPath + "\\" + folderName + "\\" + name, _rootPath + "\\" + folderName + "\\" + newName);
|
||||
_storageProvider.RenameFile(folderName + "\\" + name, folderName + "\\" + newName);
|
||||
}
|
||||
|
||||
public void UploadMediaFile(string folderName, HttpPostedFileBase postedFile) {
|
||||
var targetFolder = HttpContext.Current.Server.MapPath("~/Media/" + folderName);
|
||||
public string UploadMediaFile(string folderName, HttpPostedFileBase postedFile) {
|
||||
|
||||
if (postedFile.FileName.EndsWith(".zip")) {
|
||||
UnzipMediaFileArchive(targetFolder, postedFile);
|
||||
UnzipMediaFileArchive(folderName, postedFile);
|
||||
// Don't save the zip file.
|
||||
return;
|
||||
}
|
||||
if (postedFile.ContentLength > 0) {
|
||||
string filePath = Path.Combine(targetFolder,
|
||||
Path.GetFileName(postedFile.FileName));
|
||||
postedFile.SaveAs(filePath);
|
||||
}
|
||||
return _storageProvider.GetPublicUrl(folderName);
|
||||
}
|
||||
|
||||
private static void UnzipMediaFileArchive(string targetFolder, HttpPostedFileBase postedFile) {
|
||||
if (postedFile.ContentLength > 0) {
|
||||
var filePath = Path.Combine(folderName, Path.GetFileName(postedFile.FileName));
|
||||
var inputStream = postedFile.InputStream;
|
||||
|
||||
SaveStream(filePath, inputStream);
|
||||
return _storageProvider.GetPublicUrl(filePath);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void SaveStream(string filePath, Stream inputStream) {
|
||||
var file = _storageProvider.CreateFile(filePath);
|
||||
var outputStream = file.OpenWrite();
|
||||
var buffer = new byte[8192];
|
||||
for (; ; ) {
|
||||
|
||||
var length = inputStream.Read(buffer, 0, buffer.Length);
|
||||
if (length <= 0)
|
||||
break;
|
||||
outputStream.Write(buffer, 0, length);
|
||||
}
|
||||
outputStream.Dispose();
|
||||
}
|
||||
|
||||
private void UnzipMediaFileArchive(string targetFolder, HttpPostedFileBase postedFile) {
|
||||
var postedFileLength = postedFile.ContentLength;
|
||||
var postedFileStream = postedFile.InputStream;
|
||||
var postedFileData = new byte[postedFileLength];
|
||||
@@ -124,44 +134,20 @@ namespace Orchard.Media.Services {
|
||||
// before the directories that contain them, so we create directories as soon as first
|
||||
// file below their path is encountered.
|
||||
while ((entry = fileInflater.GetNextEntry()) != null) {
|
||||
var directoryName = Path.GetDirectoryName(entry.Name);
|
||||
if (!Directory.Exists(Path.Combine(targetFolder, directoryName))) {
|
||||
Directory.CreateDirectory(Path.Combine(targetFolder, directoryName));
|
||||
}
|
||||
|
||||
if (!entry.IsDirectory && entry.Name.Length > 0) {
|
||||
var len = Convert.ToInt32(entry.Size);
|
||||
var extractedBytes = new byte[len];
|
||||
fileInflater.Read(extractedBytes, 0, len);
|
||||
File.WriteAllBytes(Path.Combine(targetFolder, entry.Name), extractedBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
var entryName = Path.Combine(targetFolder, entry.Name);
|
||||
var directoryName = Path.GetDirectoryName(entryName);
|
||||
|
||||
try { _storageProvider.CreateFolder(directoryName); }
|
||||
catch {
|
||||
// no handling needed - this is to force the folder to exist if it doesn't
|
||||
}
|
||||
|
||||
private static List<string> GetParentHierarchy(IStorageFolder folder) {
|
||||
var parentHierarchy = new List<string>();
|
||||
do {
|
||||
IStorageFolder parentFolder = folder.GetParent();
|
||||
string parentName = parentFolder.GetName();
|
||||
if (String.Equals(parentName, "Media", StringComparison.OrdinalIgnoreCase)) {
|
||||
break;
|
||||
SaveStream(entryName, fileInflater);
|
||||
}
|
||||
parentHierarchy.Insert(0, parentName);
|
||||
folder = parentFolder;
|
||||
} while (true);
|
||||
|
||||
return parentHierarchy;
|
||||
}
|
||||
|
||||
private static string GetMediaPath(IEnumerable<string> parentHierarchy, string folderName) {
|
||||
var mediaPath = new StringBuilder();
|
||||
foreach (string parent in parentHierarchy) {
|
||||
mediaPath.Append(parent);
|
||||
mediaPath.Append("\\");
|
||||
}
|
||||
mediaPath.Append(folderName);
|
||||
return mediaPath.ToString();
|
||||
}
|
||||
|
||||
private static string RenameFolderPath(string path, string newName) {
|
||||
|
@@ -14,5 +14,7 @@ namespace Orchard.Media.ViewModels {
|
||||
return MediaPath.Replace("\\", "/");
|
||||
}
|
||||
}
|
||||
|
||||
public string PublicUrl { get; set; }
|
||||
}
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@
|
||||
<%= Html.ValidationSummary() %>
|
||||
<div class="primary">
|
||||
<div>
|
||||
<img src="<%=ResolveUrl("~/Media/" + Html.Encode(Model.RelativePath + "/" + Model.Name))%>" class="previewImage" alt="<%=Html.Encode(Model.Caption) %>" />
|
||||
<img src="<%=Model.PublicUrl%>" class="previewImage" alt="<%=Html.Encode(Model.Caption) %>" />
|
||||
</div>
|
||||
<fieldset>
|
||||
<%-- todo: make these real (including markup) --%>
|
||||
|
@@ -82,7 +82,7 @@ namespace Orchard.Setup.Controllers {
|
||||
"Orchard.Themes",
|
||||
"Orchard.MultiTenancy",
|
||||
"Orchard.Pages",
|
||||
"Orchard.Comments" };
|
||||
"Orchard.Comments"};
|
||||
|
||||
var setupContext = new SetupContext {
|
||||
SiteName = model.SiteName,
|
||||
|
@@ -7,30 +7,47 @@ using Orchard.Environment.Configuration;
|
||||
|
||||
namespace Orchard.Storage {
|
||||
public class FileSystemStorageProvider : IStorageProvider {
|
||||
private readonly ShellSettings _settings;
|
||||
private string _storagePath;
|
||||
private readonly string _storagePath;
|
||||
private readonly string _publicPath;
|
||||
|
||||
public FileSystemStorageProvider(ShellSettings settings) {
|
||||
_settings = settings;
|
||||
|
||||
var mediaPath = HostingEnvironment.IsHosted
|
||||
? HostingEnvironment.MapPath("~/Media/")
|
||||
? HostingEnvironment.MapPath("~/Media/") ?? ""
|
||||
: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Media");
|
||||
|
||||
_storagePath = Path.Combine(mediaPath, settings.Name);
|
||||
|
||||
var appPath = "";
|
||||
if (HostingEnvironment.IsHosted) {
|
||||
appPath = HostingEnvironment.ApplicationVirtualPath;
|
||||
}
|
||||
if (!appPath.EndsWith("/"))
|
||||
appPath = appPath + '/';
|
||||
if (!appPath.StartsWith("/"))
|
||||
appPath = '/' + appPath;
|
||||
|
||||
_publicPath = appPath + "Media/" + settings.Name + "/";
|
||||
}
|
||||
|
||||
string Map(string path) {
|
||||
return Path.Combine(_storagePath, path);
|
||||
return string.IsNullOrEmpty(path) ? _storagePath : Path.Combine(_storagePath, path);
|
||||
}
|
||||
|
||||
static string Fix(string path) {
|
||||
return Path.DirectorySeparatorChar != '/'
|
||||
return string.IsNullOrEmpty(path)
|
||||
? ""
|
||||
: Path.DirectorySeparatorChar != '/'
|
||||
? path.Replace('/', Path.DirectorySeparatorChar)
|
||||
: path;
|
||||
}
|
||||
|
||||
#region Implementation of IStorageProvider
|
||||
|
||||
public string GetPublicUrl(string path) {
|
||||
|
||||
return _publicPath + path.Replace(Path.DirectorySeparatorChar, '/');
|
||||
}
|
||||
|
||||
public IStorageFile GetFile(string path) {
|
||||
if (!File.Exists(Map(path))) {
|
||||
throw new ArgumentException("File " + path + " does not exist");
|
||||
@@ -105,9 +122,8 @@ namespace Orchard.Storage {
|
||||
}
|
||||
|
||||
var fileInfo = new FileInfo(Map(path));
|
||||
using (var stream = fileInfo.Create()) {
|
||||
File.WriteAllBytes(Map(path), new byte[0]);
|
||||
|
||||
}
|
||||
return new FileSystemStorageFile(Fix(path), fileInfo);
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Orchard.Storage {
|
||||
public interface IStorageProvider : IDependency {
|
||||
string GetPublicUrl(string path);
|
||||
IStorageFile GetFile(string path);
|
||||
IEnumerable<IStorageFile> ListFiles(string path);
|
||||
IEnumerable<IStorageFolder> ListFolders(string path);
|
||||
|
Reference in New Issue
Block a user