November Happy Hour will be moved to Thursday December 5th.

How to upload media assets in bulk.

Vote:
 

I have created the following with reference to a tutorial.

Following is my controller

[GuiPlugIn(
      Area = PlugInArea.AdminMenu,
      Url = "/ImportMediaAssets",
      DisplayName = "Import media assets")]
    public class ImportMediaAssetsController : Controller
    {
        public ActionResult Index()
        {
            var model = new ImportMediaAssetModel();
            return View(model);
        }
 
        public ActionResult BeginUpload(ImportMediaAssetModel postedModel)
        {
            var directory = new DirectoryInfo(postedModel.SourcePath);
 
            if (postedModel.Destination == ImportMediaAssetModel.GlobalAssetFolder)
            {
                CreateAndUploadFilesAndFolders(directory, ContentReference.SiteBlockFolder, postedModel.OverrideExistingItems);
            }
            return View(directory);
        }
 
 
        private void CreateAndUploadFilesAndFolders(DirectoryInfo dir, ContentReference parentLink, bool overrideFiles)
        {
            foreach (var childFile in dir.GetFiles())
            {
                if (fileCount >= 1) break;
                UploadFile(childFile, parentLink, overrideFiles);
            }
            foreach (var childDir in dir.GetDirectories())
            {
                var storedFolder = _contentRepository.Service.GetBySegment(parentLink, childDir.Name,
           LanguageSelector.AutoDetect()) as ContentFolder;
 
                if (storedFolder != null)
                {
                    CreateAndUploadFilesAndFolders(childDir, storedFolder.ContentLink, overrideFiles);
                }
                else
                {
                    var contentFolder = _contentRepository.Service.GetDefault<>ContentAssetFolder>(parentLink);
                    contentFolder.Name = childDir.Name;
                    var contentAssetsFolderReference = _contentRepository.Service.Save(contentFolder, SaveAction.Publish);
                    CreateAndUploadFilesAndFolders(childDir, contentAssetsFolderReference, overrideFiles);
                }
            }
        }
 
 
        private void UploadFile(FileInfo file, ContentReference parentLink, bool overrideFiles)
        {
            //Get a suitable MediaData type from extension
            var extension = GetExtension(file.Name);
            var mediaType = _contentMediaResolver.Service.GetFirstMatching(extension);
            var contentType = _contentTypeRepository.Service.Load(mediaType);
 
            // Try get existing media
            var storedMedia = _contentRepository.Service.GetBySegment(parentLink, file.Name,
          LanguageSelector.AutoDetect()) as IContentMedia;
            if (!overrideFiles && storedMedia != nullreturn;
 
            //Get a new empty file data
            var media = _contentRepository.Service.GetDefault<>IContentMedia>(parentLink, contentType.ID);
            var container = media.BinaryDataContainer;
            var blob = _blobFactory.Service.CreateBlob(container, extension);
            using (var fs = new FileStream(file.FullName, FileMode.Open))
            {
                blob.Write(fs);
            }
            media.BinaryData = blob;
            media.Name = file.Name;
            _contentRepository.Service.Save(media, SaveAction.Publish, AccessLevel.NoAccess);
        }
    }





And this is my model :


public class ImportMediaAssetModel 
   {
       public string Destination { getset; }
       public string SourcePath { getset; }
       public bool OverrideExistingItems { getset; }
           
     
   }


Please have a look and tell me how will it work.

#180486
Jul 12, 2017 13:43
Vote:
 
#180507
Jul 13, 2017 8:00
Vote:
 

From which class is object "_contentRepository" referred

Also what the namespaces for class IContentMedia, ContentAssetFolder etc

#188950
Mar 07, 2018 15:19
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.