Orchard - Set special Media folder
-
28-10-2019 - |
Question
I need to setup the jPlayer directory to a folder which is not called "/Default/MediaGallery" but "/MyFolder/CurrentMedia/".
Please, can anyone help me to find a way to change the setting in jPlayer - Version: 1.0.1
Regards
Solution
Looks like the name is hardcoded. There is no way to get rid of the "/Default" part of the path, as the jPlayer module is using the Orchard Media feature, which has a default root folder named after each of the tenants (it's a multitenant-friendly module). You can alter only the following part of that path by changing the name (MediaGalleries) in line 12 of the file linked above.
OTHER TIPS
@PiotrSzmyd is right, it's partly hardcoded. The "Default" comes from the field Name
in Orchard.Web\App_Data\Sites\Default\Settings.txt
and the paths are hardcoded in Orchard\FileSystems\Media\FileSystemStorageProvider.cs
. Sadly there is no way to change the behaviour of this class with handlers or something. There are 2 options now
- change the source code of Orchard
- work around it by adding a module that provides a custom implementation of
IStorageProvider
and when needed (means media storage path is outside of the Orchard directory structure) an additional MVC controller to serve the media files.
I changed the media storage path to \\<server>\Files$\<build_configuration>\Media
by using the second option.
First, here's the essential parts of the IStorageProvider
:
Custom IStorageProvider
implementation
[Orchard.Environment.Extensions.OrchardSuppressDependency("Orchard.FileSystems.Media.FileSystemStorageProvider")] // adopted from AzureBlobStorageProvider.cs
public class FileSystemStorageProvider : Orchard.FileSystems.Media.IStorageProvider
{
private Orchard.FileSystems.Media.FileSystemStorageProvider mFileSystemStorageProvider;
public FileSystemStorageProvider(Orchard.Environment.Configuration.ShellSettings settings)
{
// use Orchard's default IStorageProvider for implementation
mFileSystemStorageProvider = new Orchard.FileSystems.Media.FileSystemStorageProvider(settings);
var lFileSystemStorageProviderType = mFileSystemStorageProvider.GetType();
// get media storage path, e.g. from configuration file
var lStoragePath = GetMediaStoragePath();
lStoragePath = System.IO.Path.Combine(lStoragePath, settings.Name);
// _storagePath is private read-only, so change it the hard way by using reflection
lFileSystemStorageProviderType
.GetField("_storagePath", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.SetValue(mFileSystemStorageProvider, lStoragePath);
string lVirtualPath = "~/Files/" + settings.Name + "/";
lFileSystemStorageProviderType
.GetField("_virtualPath", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.SetValue(mFileSystemStorageProvider, lVirtualPath);
string lPublicPath = mFileSystemStorageProvider.GetPublicUrl(null).Replace("/Media/" + settings.Name + "/", "/Files/" + settings.Name + "/");
lFileSystemStorageProviderType
.GetField("_publicPath", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.SetValue(mFileSystemStorageProvider, lPublicPath);
}
#region Implementation of IStorageProvider
public bool FileExists(string aPath)
{
return mFileSystemStorageProvider.FileExists(aPath);
}
...
#endregion
}
This custom implementation maps
- storage path to
\\<server>\Files$\<build_configuration>\Media
- virtual path to
~/Files/Default/
- absolute path to
/Files/Default/
There is a problem now. Orchard would render media URLs as www.mysite.de/Files/Default...
but there is no directory called Files in the Orchard directory structure. Now the custom route comes into play.
Custom route
public class Routes : Orchard.Mvc.Routes.IRouteProvider
{
public System.Collections.Generic.IEnumerable<Orchard.Mvc.Routes.RouteDescriptor> GetRoutes()
{
Orchard.Mvc.Routes.RouteDescriptor[] lRoutes =
new Orchard.Mvc.Routes.RouteDescriptor[]
{
new Orchard.Mvc.Routes.RouteDescriptor()
{
Name = "Custom route for media files",
Priority = 10000,
Route = new System.Web.Routing.Route(
"Files/{*aRelativePath}",
new System.Web.Routing.RouteValueDictionary()
{
{"area", "MyModule"},
{"controller", "Media"},
{"action", "GetFile"}
},
new System.Web.Routing.RouteValueDictionary() {},
new System.Web.Routing.RouteValueDictionary() { {"area", "MyModule"} },
new System.Web.Mvc.MvcRouteHandler()
)
}
};
return lRoutes;
}
public void GetRoutes(System.Collections.Generic.ICollection<Orchard.Mvc.Routes.RouteDescriptor> arRoutes)
{
foreach (var lRouteDescriptor in GetRoutes())
arRoutes.Add(lRouteDescriptor);
}
}
This route implementation ensures a redirect of www.mysite.de/Files/Default...
to a specific media controller that looks like this:
Media controller
public class MediaController
{
public MediaController(Orchard.FileSystems.Media.IMimeTypeProvider aMimeTypeProvider)
{
mMimeTypeProvider = aMimeTypeProvider;
}
public System.Web.Mvc.ActionResult GetFile(string aRelativePath)
{
string lMediaStoragePath, lErrorMessage;
// get media storage path, e.g. from configuration file
if (GetMediaStoragePath(out lMediaStoragePath, out lErrorMessage))
{
string lFilename = System.IO.Path.Combine(lMediaStoragePath, aRelativePath);
string lMimeType = mMimeTypeProvider.GetMimeType(lFilename);
return File(lFilename, lMimeType);
}
else
return Content(lErrorMessage);
}
// private
private Orchard.FileSystems.Media.IMimeTypeProvider mMimeTypeProvider;
}