예제 #1
0
        /// <summary>
        /// Replaces the file or if it doesn't exist, creates it.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutFileResponse</c> with a file id.
        /// </returns>
        public PutFileResponse Put(PutFileRequest request)
        {
            IEnumerable <MediaFolder> parentFolderFuture = null;

            if (request.Data.FolderId.HasValue)
            {
                parentFolderFuture = repository.AsQueryable <MediaFolder>()
                                     .Where(c => c.Id == request.Data.FolderId.Value && !c.IsDeleted)
                                     .ToFuture();
            }

            var mediaFile = repository.AsQueryable <MediaFile>()
                            .Where(file => file.Id == request.Id)
                            .FetchMany(f => f.AccessRules)
                            .ToFuture()
                            .FirstOrDefault();

            MediaFolder parentFolder = null;

            if (parentFolderFuture != null)
            {
                parentFolder = parentFolderFuture.First();
                if (parentFolder.Type != Module.MediaManager.Models.MediaType.File)
                {
                    throw new CmsApiValidationException("Folder must be type of an file.");
                }
            }

            var createFile = mediaFile == null;

            if (createFile)
            {
                mediaFile = new MediaFile
                {
                    Id          = request.Id.GetValueOrDefault(),
                    Type        = Module.MediaManager.Models.MediaType.File,
                    AccessRules = new List <AccessRule>()
                };
            }
            else if (request.Data.Version > 0)
            {
                mediaFile.Version = request.Data.Version;
            }

            unitOfWork.BeginTransaction();

            if (!createFile)
            {
                repository.Save(mediaFile.CreateHistoryItem());
            }

            mediaFile.Title                 = request.Data.Title;
            mediaFile.Description           = request.Data.Description;
            mediaFile.Size                  = request.Data.FileSize;
            mediaFile.PublicUrl             = request.Data.PublicUrl;
            mediaFile.Folder                = parentFolder;
            mediaFile.PublishedOn           = request.Data.PublishedOn;
            mediaFile.OriginalFileName      = request.Data.OriginalFileName;
            mediaFile.OriginalFileExtension = request.Data.OriginalFileExtension;

            mediaFile.Image = request.Data.ThumbnailId.GetValueOrDefault() != default(Guid)
                ? repository.AsProxy <MediaImage>(request.Data.ThumbnailId.Value)
                : null;

            mediaFile.FileUri     = new Uri(request.Data.FileUri);
            mediaFile.IsUploaded  = request.Data.IsUploaded;
            mediaFile.IsTemporary = request.Data.IsTemporary;
            mediaFile.IsCanceled  = request.Data.IsCanceled;

            var archivedMedias   = new List <Media>();
            var unarchivedMedias = new List <Media>();

            if (mediaFile.IsArchived != request.Data.IsArchived)
            {
                if (request.Data.IsArchived)
                {
                    archivedMedias.Add(mediaFile);
                    mediaService.ArchiveSubMedias(mediaFile, archivedMedias);
                }
                else
                {
                    unarchivedMedias.Add(mediaFile);
                    mediaService.UnarchiveSubMedias(mediaFile, unarchivedMedias);
                }
            }

            mediaFile.IsArchived = request.Data.IsArchived;

            IList <Tag> newTags = null;

            if (request.Data.Tags != null)
            {
                tagService.SaveMediaTags(mediaFile, request.Data.Tags, out newTags);
            }

            if (request.Data.AccessRules != null)
            {
                mediaFile.AccessRules.RemoveDuplicateEntities();
                var accessRules =
                    request.Data.AccessRules.Select(
                        r => (IAccessRule) new AccessRule {
                    AccessLevel = (Core.Security.AccessLevel)(int) r.AccessLevel, Identity = r.Identity, IsForRole = r.IsForRole
                })
                    .ToList();
                accessControlService.UpdateAccessControl(mediaFile, accessRules);
            }

            if (request.Data.Categories != null)
            {
                categoryService.CombineEntityCategories <Media, MediaCategory>(mediaFile, request.Data.Categories);
            }

            repository.Save(mediaFile);

            unitOfWork.Commit();

            // Fire events.
            Events.RootEvents.Instance.OnTagCreated(newTags);
            if (createFile)
            {
                Events.MediaManagerEvents.Instance.OnMediaFileUploaded(mediaFile);
            }
            else
            {
                Events.MediaManagerEvents.Instance.OnMediaFileUpdated(mediaFile);
            }

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

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

            return(new PutFileResponse
            {
                Data = mediaFile.Id
            });
        }
예제 #2
0
        /// <summary>
        /// Replaces the image or if it doesn't exist, creates it.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutImageResponse</c> with a image id.
        /// </returns>
        public PutImageResponse Put(PutImageRequest request)
        {
            IEnumerable <MediaFolder> parentFolderFuture = null;

            if (request.Data.FolderId.HasValue)
            {
                parentFolderFuture = repository.AsQueryable <MediaFolder>()
                                     .Where(c => c.Id == request.Data.FolderId.Value && !c.IsDeleted)
                                     .ToFuture();
            }

            var mediaImage = repository.AsQueryable <MediaImage>()
                             .Where(file => file.Id == request.Id)
                             .ToFuture()
                             .FirstOrDefault();

            MediaFolder parentFolder = null;

            if (parentFolderFuture != null)
            {
                parentFolder = parentFolderFuture.First();
                if (parentFolder.Type != Module.MediaManager.Models.MediaType.Image)
                {
                    throw new CmsApiValidationException("Folder must be type of an image.");
                }
            }

            var createImage = mediaImage == null;

            if (createImage)
            {
                mediaImage = new MediaImage
                {
                    Id   = request.Id.GetValueOrDefault(),
                    Type = Module.MediaManager.Models.MediaType.Image
                };
            }
            else if (request.Data.Version > 0)
            {
                mediaImage.Version = request.Data.Version;
            }

            unitOfWork.BeginTransaction();

            if (!createImage)
            {
                repository.Save(mediaImage.CreateHistoryItem());
            }

            mediaImage.Title                 = request.Data.Title;
            mediaImage.Description           = request.Data.Description;
            mediaImage.Caption               = request.Data.Caption;
            mediaImage.Size                  = request.Data.FileSize;
            mediaImage.PublicUrl             = request.Data.ImageUrl;
            mediaImage.Width                 = request.Data.Width;
            mediaImage.Height                = request.Data.Height;
            mediaImage.PublicThumbnailUrl    = request.Data.ThumbnailUrl;
            mediaImage.ThumbnailWidth        = request.Data.ThumbnailWidth;
            mediaImage.ThumbnailHeight       = request.Data.ThumbnailHeight;
            mediaImage.ThumbnailSize         = request.Data.ThumbnailSize;
            mediaImage.Folder                = parentFolder;
            mediaImage.PublishedOn           = request.Data.PublishedOn;
            mediaImage.OriginalFileName      = request.Data.OriginalFileName;
            mediaImage.OriginalFileExtension = request.Data.OriginalFileExtension;
            mediaImage.OriginalWidth         = request.Data.OriginalWidth;
            mediaImage.OriginalHeight        = request.Data.OriginalHeight;
            mediaImage.OriginalSize          = request.Data.OriginalSize;
            mediaImage.PublicOriginallUrl    = request.Data.OriginalUrl;

            mediaImage.FileUri      = new Uri(request.Data.FileUri);
            mediaImage.IsUploaded   = request.Data.IsUploaded;
            mediaImage.IsTemporary  = request.Data.IsTemporary;
            mediaImage.IsCanceled   = request.Data.IsCanceled;
            mediaImage.OriginalUri  = new Uri(request.Data.OriginalUri);
            mediaImage.ThumbnailUri = new Uri(request.Data.ThumbnailUri);

            var archivedMedias   = new List <Media>();
            var unarchivedMedias = new List <Media>();

            if (mediaImage.IsArchived != request.Data.IsArchived)
            {
                if (request.Data.IsArchived)
                {
                    archivedMedias.Add(mediaImage);
                    mediaService.ArchiveSubMedias(mediaImage, archivedMedias);
                }
                else
                {
                    unarchivedMedias.Add(mediaImage);
                    mediaService.UnarchiveSubMedias(mediaImage, unarchivedMedias);
                }
            }

            mediaImage.IsArchived = request.Data.IsArchived;

            repository.Save(mediaImage);

            IList <Tag> newTags = null;

            if (request.Data.Tags != null)
            {
                tagService.SaveMediaTags(mediaImage, request.Data.Tags, out newTags);
            }

            unitOfWork.Commit();

            // Fire events.
            Events.RootEvents.Instance.OnTagCreated(newTags);
            if (createImage)
            {
                Events.MediaManagerEvents.Instance.OnMediaFileUploaded(mediaImage);
            }
            else
            {
                Events.MediaManagerEvents.Instance.OnMediaFileUpdated(mediaImage);
            }

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

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

            return(new PutImageResponse
            {
                Data = mediaImage.Id
            });
        }
예제 #3
0
        /// <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
            });
        }