Sitecore Upload File to Directory in Cms

In my previous to final post, I discussed the Sitecore UploadWatcher — a Sitecore.IO.FileWatcher which uploads files to the Media Library when files are dropped into the /upload directory of your Sitecore website root.

Unfortunately, in the "out of the box" solution, files are uploaded straight under the Media Library root (/sitecore/media library). Imagine having to sift through all kinds of Media Library Items and folders just to observe the image that y'all are looking for. Such would exist an arduous task at best.

I decided to dig through Sitecore.Kernel.dll to run into why this is the case, and discovered why: the FileCreated() method on the Sitecore.Resource.Media.MediaCreator class uses an empty Sitecore.Resources.Media.MediaCreatorOptions example. In social club for the file to exist uploaded to a specified location in the Media Library, the Destination holding on the Sitecore.Resources.Media.MediaCreatorOptions instance must be set up, or it will be uploaded directly to the Media Library root.

Here's the skillful news: the FileCreated() method is alleged virtual, so why not subclass it and then override this method to include some custom logic to set the Destination property on the MediaCreatorOptions instance?

I did just that in the following class:

using System.IO;  using Sitecore.Configuration; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.IO; using Sitecore.Pipelines.GetMediaCreatorOptions; using Sitecore.Resources.Media;  namespace Sitecore.Sandbox.Resources.Media {     public class MediaCreator : Sitecore.Resources.Media.MediaCreator     {         private string UploadLocation { get; fix; }          public override void FileCreated(string filePath)         {             Assert.ArgumentNotNullOrEmpty(filePath, "filePath");             if (string.IsNullOrWhiteSpace(UploadLocation))             {                 base of operations.FileCreated(filePath);                 return;             }              SetContext();             lock (FileUtil.GetFileLock(filePath))             {                 string destination = GetMediaItemDestination(filePath);                 if (FileUtil.IsFolder(filePath))                 {                     MediaCreatorOptions options = MediaCreatorOptions.Empty;                     options.Destination = destination;                     options.Build(GetMediaCreatorOptionsArgs.FileBasedContext);                     this.CreateFromFolder(filePath, options);                 }                 else                 {                     MediaCreatorOptions options = MediaCreatorOptions.Empty;                     options.Destination = destination;                     long length = new FileInfo(filePath).Length;                     options.FileBased = (length > Settings.Media.MaxSizeInDatabase) || Settings.Media.UploadAsFiles;                     options.Build(GetMediaCreatorOptionsArgs.FileBasedContext);                     this.CreateFromFile(filePath, options);                 }             }         }          protected virtual void SetContext()         {             if (Context.Site == cipher)             {                 Context.SetActiveSite("vanquish");             }         }          protected virtual string GetMediaItemDestination(cord filePath)         {             if(string.IsNullOrWhiteSpace(UploadLocation))             {                 render null;             }              string fileNameNoExtension = Path.GetFileNameWithoutExtension(filePath);             string itemName = ItemUtil.ProposeValidItemName(fileNameNoExtension);             return string.Format("{0}/{i}", UploadLocation, itemName);         }     } }        

The UploadLocation property in the class above is to be divers in Sitecore Configuration — see the patch include configuration file below — and and so populated via the Sitecore Configuration Factory when the class is instantiated (yes, I'm defining this class in Sitecore Configuration equally well).

Near of the logic in the FileCreated() method above comes from its base grade Sitecore.Resources.Media.MediaCreator. I had to copy and paste most of this code from its base course' FileCreated() method as I couldn't only delegate to the base class' FileCreated() method — I needed to ready the Destination belongings on the MediaCreatorOptions instance.

The Destination property on the MediaCreatorOptions example is being gear up to be the UploadLocation plus the Media Library Item name — I determine this full path in the GetMediaItemDestination() method.

Unfortunately, I as well had to bring in the SetContext() method from the base class since it'southward declared private — this method is needed in the FileCreated() method to ensure we have a context site defined.

Now, we need a way to set an instance of the above in the Creator property on the Sitecore.Sandbox.Resources.Media.MediaProvider example. Unfortunately, in that location was no easy way to do this without having to subclass the Sitecore.Sandbox.Resource.Media.MediaProvider class, and then fix the Creator property via its constructor:

using Sitecore.Configuration;  namespace Sitecore.Sandbox.Resources.Media {     public class MediaProvider : Sitecore.Resource.Media.MediaProvider     {         private MediaCreator MediaCreator { go; set; }          public MediaProvider()         {             OverrideMediaCreator();         }          protected virtual void OverrideMediaCreator()         {             Sitecore.Resources.Media.MediaCreator mediaCreator = GetMediaCreator();             if (mediaCreator == null)             {                 return;             }              Creator = mediaCreator;         }          protected virtual Sitecore.Resource.Media.MediaCreator GetMediaCreator()         {             return Factory.CreateObject("mediaLibrary/mediaCreator", false) as MediaCreator;         }     } }        

The OverrideMediaCreator() method above tries to become an instance of a Sitecore.Resources.Media.MediaCreator using the Sitecore Configuration Factory — it delegates to the GetMediaCreator() method to get this instance — and then set it on the Creator belongings of its base of operations class if the MediaCreator obtained from the GetMediaCreator() method isn't null.

If information technology is null, information technology but exits out — there is a default instance created in the Sitecore.Resources.Media.MediaCreator base of operations class, and so that 1 would exist used instead.

I then replaced the "out of the box" Sitecore.Resource.Media.MediaProvider with the new one above, and likewise defined the MediaCreator above in the following patch include configuration file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">   <sitecore>     <mediaLibrary>       <mediaProvider patch:instead="mediaProvider[@type='Sitecore.Resources.Media.MediaProvider, Sitecore.Kernel']"                      type="Sitecore.Sandbox.Resources.Media.MediaProvider, Sitecore.Sandbox" />       <mediaCreator type="Sitecore.Sandbox.Resources.Media.MediaCreator, Sitecore.Sandbox">         <UploadLocation>/sitecore/media library/uploaded</UploadLocation>       </mediaCreator>   </mediaLibrary>   </sitecore> </configuration>        

Let's see how we did.

Equally you can see, I have an empty uploaded Media Library folder:

upload-watcher-not-uploaded-to-folder

Allow's move an prototype into the /upload folder of my Sitecore example:

upload-watcher-upload-to-folder

After reloading the "uploaded" Media Library folder, I see that the image was uploaded to it:

upload-watcher-uploaded-to-folder

I would also to like to mention that this solution will also work if at that place is no uploaded binder in the Media Library — information technology will be created during upload process.

If you have any thoughts on this, please share in a comment.

flinnscather.blogspot.com

Source: https://sitecorejunkie.com/2016/05/22/upload-files-via-the-sitecore-uploadwatcher-to-a-configuration-specified-media-library-folder/

0 Response to "Sitecore Upload File to Directory in Cms"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel