Example #1
0
 public void OnMediaFolderDeleted(MediaFolder mediaFolder)
 {
     if (MediaFolderDeleted != null)
     {
         MediaFolderDeleted(new SingleItemEventArgs<MediaFolder>(mediaFolder));
     }
 }
        private bool DeleteFolder(MediaFolder folder, bool checkSecurity, IPrincipal currentPrincipal)
        {
            try
            {
                var allItemsDeleted = true;
                var folderItems = folder.Medias.ToList();
                foreach (var item in folderItems)
                {
                    var fileToDelete = item as MediaFile;
                    if (fileToDelete != null && !DeleteFile(fileToDelete, checkSecurity, currentPrincipal))
                    {
                        allItemsDeleted = false;
                    }
                    var folderToDelete = item as MediaFolder;
                    if (folderToDelete != null && !DeleteFolder(folderToDelete, checkSecurity, currentPrincipal))
                    {
                        allItemsDeleted = false;
                    }
                }

                if (!allItemsDeleted)
                {
                    return false;
                }

                unitOfWork.BeginTransaction();
                repository.AsQueryable<MediaFolder>(f => f.Id == folder.Id || f.Original.Id == folder.Id)
                    .Fetch(f => f.Categories)
                    .Fetch(f => f.MediaTags)
                    .ToList()
                    .ForEach(
                        media =>
                            {
                                if (media.MediaTags != null)
                                {
                                    foreach (var mediaTag in media.MediaTags)
                                    {
                                        repository.Delete(mediaTag);
                                    }
                                }
                                if (media.Categories != null)
                                {
                                    foreach (var category in media.Categories)
                                    {
                                        repository.Delete(category);
                                    }
                                }

                                repository.Delete(media);
                            });
                unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("Failed to delete folder with id={0}.", ex, folder.Id);
                return false;
            }

            Events.MediaManagerEvents.Instance.OnMediaFolderDeleted(folder);
            return true;
        }
        /// <summary>
        /// Replaces the folder or if it doesn't exist, creates it.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutFolderResponse</c> with a folder id.
        /// </returns>
        public PutFolderResponse Put(PutFolderRequest request)
        {
            IEnumerable<MediaFolder> parentFolderFuture = null;
            if (request.Data.ParentFolderId.HasValue)
            {
                parentFolderFuture = repository.AsQueryable<MediaFolder>()
                    .Where(c => c.Id == request.Data.ParentFolderId.Value && !c.IsDeleted)
                    .ToFuture();
            }

            var mediaFolder = repository.AsQueryable<MediaFolder>()
                .Fetch(media => media.Folder)
                .Distinct()
                .ToFuture()
                .FirstOrDefault(folder => folder.Id == request.Id);

            MediaFolder parentFolder = null;
            if (parentFolderFuture != null)
            {
                parentFolder = parentFolderFuture.First();
                if (parentFolder.Type != (Module.MediaManager.Models.MediaType)(int)request.Data.Type)
                {
                    throw new CmsApiValidationException("Parent folder type does not match to this folder type.");
                }
            }

            var createFolder = mediaFolder == null;
            if (createFolder)
            {
                mediaFolder = new MediaFolder
                                  {
                                      Id = request.Id.GetValueOrDefault(),
                                      ContentType = Module.MediaManager.Models.MediaContentType.Folder,
                                      Type = (Module.MediaManager.Models.MediaType)(int)request.Data.Type
                                  };
            }
            else if (request.Data.Version > 0)
            {
                mediaFolder.Version = request.Data.Version;
            }

            unitOfWork.BeginTransaction();

            mediaFolder.Title = request.Data.Title;
            mediaFolder.Folder = parentFolder;

            mediaFolder.PublishedOn = DateTime.Now;

            var archivedMedias = new List<Media>();
            var unarchivedMedias = new List<Media>();
            if (mediaFolder.IsArchived != request.Data.IsArchived)
            {
                if (request.Data.IsArchived)
                {
                    archivedMedias.Add(mediaFolder);
                    mediaService.ArchiveSubMedias(mediaFolder, archivedMedias);
                }
                else
                {
                    unarchivedMedias.Add(mediaFolder);
                    mediaService.UnarchiveSubMedias(mediaFolder, unarchivedMedias);
                }
            }

            mediaFolder.IsArchived = request.Data.IsArchived;

            repository.Save(mediaFolder);

            unitOfWork.Commit();

            // Fire events.
            if (createFolder)
            {
                Events.MediaManagerEvents.Instance.OnMediaFolderCreated(mediaFolder);
            }
            else
            {
                Events.MediaManagerEvents.Instance.OnMediaFolderUpdated(mediaFolder);
            }

            foreach (var archivedMedia in archivedMedias.Distinct())
            {
                Events.MediaManagerEvents.Instance.OnMediaArchived(archivedMedia);
            }

            foreach (var archivedMedia in unarchivedMedias.Distinct())
            {
                Events.MediaManagerEvents.Instance.OnMediaUnarchived(archivedMedia);
            }

            return new PutFolderResponse { Data = mediaFolder.Id };
        }