/// <summary>
        /// Saves edited image as original.
        /// </summary>
        /// <param name="image">The edited image.</param>
        /// <param name="archivedImage">The archived image.</param>
        /// <param name="croppedImageFileStream">The stream with edited image.</param>
        /// <param name="overrideUrl">To override public url or not.</param>
        public void SaveEditedImage(MediaImage image, MediaImage archivedImage, MemoryStream croppedImageFileStream, bool overrideUrl = true)
        {
            overrideUrl = false; // TODO: temporary disabling feature #1055.
            var folderName = Path.GetFileName(Path.GetDirectoryName(image.FileUri.OriginalString));

            using (var fileStream = croppedImageFileStream ?? DownloadFileStream(image.PublicUrl))
            {
                image.Original    = null;
                image.PublishedOn = DateTime.Now;

                if (!overrideUrl)
                {
                    var publicFileName = MediaHelper.RemoveInvalidPathSymbols(MediaImageHelper.CreateVersionedFileName(image.OriginalFileName, GetVersion(image)));
                    mediaImageVersionPathService.SetPathForNewOriginal(image, folderName, publicFileName, ImageHelper.GetImageType(image.OriginalFileExtension), archivedImage.OriginalUri, archivedImage.PublicOriginallUrl);
                }

                unitOfWork.BeginTransaction();
                repository.Save(image);
                unitOfWork.Commit();

                storageService.UploadObject(new UploadRequest {
                    InputStream = fileStream, Uri = image.FileUri, IgnoreAccessControl = true
                });
                UpdateThumbnail(image, Size.Empty);
            }
        }
        /// <summary>
        /// Moves current original image to history.
        /// </summary>
        /// <param name="originalImage">The current original image.</param>
        /// <returns>The archived image.</returns>
        public MediaImage MoveToHistory(MediaImage originalImage)
        {
            var versionedFileName = MediaImageHelper.CreateVersionedFileName(
                originalImage.OriginalFileName,
                originalImage.OriginalFileExtension,
                GetVersion(originalImage));

            var folderName    = Path.GetFileName(Path.GetDirectoryName(originalImage.FileUri.OriginalString));
            var archivedImage = (MediaImage)originalImage.Clone();

            using (var originalFileStream = DownloadFileStream(archivedImage.PublicUrl))
            {
                using (var originalThumbnailFileStream = DownloadFileStream(archivedImage.PublicThumbnailUrl))
                {
                    mediaImageVersionPathService.SetPathForArchive(archivedImage, folderName, versionedFileName);

                    unitOfWork.BeginTransaction();
                    repository.Save(archivedImage);
                    unitOfWork.Commit();

                    StartTasksForImage(archivedImage, originalFileStream, originalThumbnailFileStream, originalImage.IsEdited());
                }
            }
            return(archivedImage);
        }
        /// <summary>
        /// Applies the version to file URL.
        /// </summary>
        /// <param name="fileUrl">The file URL.</param>
        /// <param name="originalFileUrl">The original file URL.</param>
        /// <param name="version">The version.</param>
        /// <returns>
        /// File name with new applied version
        /// </returns>
        private static string ApplyVersionToFileUrl(string fileUrl, string origFileName, int version)
        {
            origFileName = Path.GetFileNameWithoutExtension(origFileName);
            var realOldFileName  = Path.GetFileNameWithoutExtension(fileUrl);
            var realFileNamePath = fileUrl.Substring(0, fileUrl.LastIndexOf(Path.GetFileName(fileUrl)));
            var realFileName     = Path.Combine(realFileNamePath, string.Concat(realOldFileName.Substring(0, realOldFileName.IndexOf(origFileName)), origFileName, Path.GetExtension(fileUrl)));

            return(MediaImageHelper.CreateVersionedFileName(realFileName, version));
        }
示例#4
0
        /// <summary>
        /// Applies the version to file URL.
        /// </summary>
        /// <param name="fileUrl">The file URL.</param>
        /// <param name="originalFileUrl">The original file URL.</param>
        /// <param name="version">The version.</param>
        /// <returns>
        /// File name with new applied version
        /// </returns>
        private static string ApplyVersionToFileUrl(string fileUrl, string originalFileUrl, int version)
        {
            var start        = MediaImageHelper.OriginalImageFilePrefix.Length;
            var origFileName = Path.GetFileNameWithoutExtension(originalFileUrl);

            origFileName = origFileName.Substring(start, origFileName.Length - start);

            var realOldFileName  = Path.GetFileNameWithoutExtension(fileUrl);
            var realFileNamePath = fileUrl.Substring(0, fileUrl.LastIndexOf(Path.GetFileName(fileUrl)));
            var realFileName     = Path.Combine(realFileNamePath, string.Concat(realOldFileName.Substring(0, realOldFileName.IndexOf(origFileName)), origFileName, Path.GetExtension(fileUrl)));

            return(MediaImageHelper.CreateVersionedFileName(realFileName, version));
        }
示例#5
0
        /// <summary>
        /// Uploads the image.
        /// </summary>
        /// <param name="rootFolderId">The root folder id.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="fileLength">Length of the file.</param>
        /// <param name="fileStream">The file stream.</param>
        /// <returns>Image entity.</returns>
        public MediaImage UploadImage(Guid rootFolderId, string fileName, long fileLength, Stream fileStream, Guid reuploadMediaId)
        {
            MediaImage originalMedia;
            string     folderName;
            string     versionedFileName;
            Size       size;

            if (!reuploadMediaId.HasDefaultValue())
            {
                originalMedia     = repository.First <MediaImage>(image => image.Id == reuploadMediaId);
                fileName          = string.Concat(Path.GetFileNameWithoutExtension(originalMedia.OriginalFileName), Path.GetExtension(fileName));
                folderName        = Path.GetFileName(Path.GetDirectoryName(originalMedia.FileUri.OriginalString));
                versionedFileName = MediaImageHelper.CreateVersionedFileName(fileName, originalMedia.Version + 1);
            }
            else
            {
                folderName        = mediaFileService.CreateRandomFolderName();
                versionedFileName = MediaImageHelper.CreateVersionedFileName(fileName, 1);
            }

            try
            {
                size = GetImageSize(fileStream);
            }
            catch (ImagingException ex)
            {
                var          message    = MediaGlobalization.MultiFileUpload_ImageFormatNotSuported;
                const string logMessage = "Failed to get image size.";
                throw new ValidationException(() => message, logMessage, ex);
            }

            using (var thumbnailImage = new MemoryStream())
            {
                CreatePngThumbnail(fileStream, thumbnailImage, ThumbnailSize);

                MediaImage image = new MediaImage();
                if (!rootFolderId.HasDefaultValue())
                {
                    image.Folder = repository.AsProxy <MediaFolder>(rootFolderId);
                }

                image.Title                 = Path.GetFileName(fileName);
                image.Caption               = null;
                image.OriginalFileName      = fileName;
                image.OriginalFileExtension = Path.GetExtension(fileName);
                image.Type = MediaType.Image;

                image.Width     = size.Width;
                image.Height    = size.Height;
                image.Size      = fileLength;
                image.FileUri   = mediaFileService.GetFileUri(MediaType.Image, folderName, versionedFileName);
                image.PublicUrl = mediaFileService.GetPublicFileUrl(MediaType.Image, folderName, versionedFileName);

                image.CropCoordX1 = null;
                image.CropCoordY1 = null;
                image.CropCoordX2 = null;
                image.CropCoordY2 = null;

                image.OriginalWidth      = size.Width;
                image.OriginalHeight     = size.Height;
                image.OriginalSize       = fileLength;
                image.OriginalUri        = mediaFileService.GetFileUri(MediaType.Image, folderName, MediaImageHelper.OriginalImageFilePrefix + versionedFileName);
                image.PublicOriginallUrl = mediaFileService.GetPublicFileUrl(MediaType.Image, folderName, MediaImageHelper.OriginalImageFilePrefix + versionedFileName);


                image.ThumbnailWidth     = ThumbnailSize.Width;
                image.ThumbnailHeight    = ThumbnailSize.Height;
                image.ThumbnailSize      = thumbnailImage.Length;
                image.ThumbnailUri       = mediaFileService.GetFileUri(MediaType.Image, folderName, ThumbnailImageFilePrefix + Path.GetFileNameWithoutExtension(versionedFileName) + ".png");
                image.PublicThumbnailUrl = mediaFileService.GetPublicFileUrl(MediaType.Image, folderName, ThumbnailImageFilePrefix + Path.GetFileNameWithoutExtension(versionedFileName) + ".png");

                image.ImageAlign          = null;
                image.IsTemporary         = true;
                image.IsUploaded          = null;
                image.IsThumbnailUploaded = null;
                image.IsOriginalUploaded  = null;

                unitOfWork.BeginTransaction();
                repository.Save(image);
                unitOfWork.Commit();

                Task imageUpload     = mediaFileService.UploadMediaFileToStorage <MediaImage>(fileStream, image.FileUri, image.Id, img => { img.IsUploaded = true; }, img => { img.IsUploaded = false; }, true);
                Task originalUpload  = mediaFileService.UploadMediaFileToStorage <MediaImage>(fileStream, image.OriginalUri, image.Id, img => { img.IsOriginalUploaded = true; }, img => { img.IsOriginalUploaded = false; }, true);
                Task thumbnailUpload = mediaFileService.UploadMediaFileToStorage <MediaImage>(thumbnailImage, image.ThumbnailUri, image.Id, img => { img.IsThumbnailUploaded = true; }, img => { img.IsThumbnailUploaded = false; }, true);

                Task.Factory.ContinueWhenAll(
                    new[]
                {
                    imageUpload,
                    originalUpload,
                    thumbnailUpload
                },
                    result =>
                {
                    // During uploading progress Cancel action can by executed. Need to remove uploaded images from the storage.
                    ExecuteActionOnThreadSeparatedSessionWithNoConcurrencyTracking(session =>
                    {
                        var media      = session.Get <MediaImage>(image.Id);
                        var isUploaded = (media.IsUploaded.HasValue && media.IsUploaded.Value) ||
                                         (media.IsThumbnailUploaded.HasValue && media.IsThumbnailUploaded.Value) ||
                                         (media.IsOriginalUploaded.HasValue && media.IsOriginalUploaded.Value);
                        if (media.IsCanceled && isUploaded)
                        {
                            RemoveImageWithFiles(media.Id, media.Version);
                        }
                    });
                });

                imageUpload.Start();
                originalUpload.Start();
                thumbnailUpload.Start();

                return(image);
            }
        }
        /// <summary>
        /// Makes image as original.
        /// </summary>
        /// <param name="image">The new original image.</param>
        /// <param name="originalImage">The current original image.</param>
        /// <param name="archivedImage">The archived image.</param>
        /// <param name="overrideUrl">To override public Url ot not.</param>
        /// <returns>The new original image.</returns>
        public MediaImage MakeAsOriginal(MediaImage image, MediaImage originalImage, MediaImage archivedImage, bool overrideUrl = true)
        {
            overrideUrl = false; // TODO: temporary disabling feature #1055.
            var folderName = Path.GetFileName(Path.GetDirectoryName(originalImage.FileUri.OriginalString));

            using (var fileStream = DownloadFileStream(image.PublicUrl))
            {
                string publicUrlTemp = string.Empty,
                       publicThumbnailUrlTemp = string.Empty,
                       publicOriginallUrlTemp = string.Empty;
                Uri fileUriTemp = null, thumbnailUriTemp = null, originalUriTemp = null;

                if (overrideUrl)
                {
                    publicUrlTemp          = originalImage.PublicUrl;
                    fileUriTemp            = originalImage.FileUri;
                    publicThumbnailUrlTemp = originalImage.PublicThumbnailUrl;
                    thumbnailUriTemp       = originalImage.ThumbnailUri;
                    publicOriginallUrlTemp = originalImage.PublicOriginallUrl;
                    originalUriTemp        = originalImage.OriginalUri;
                }

                image.CopyDataTo(originalImage, false);
                MediaHelper.SetCollections(repository, image, originalImage);

                if (!overrideUrl)
                {
                    var publicFileName = MediaHelper.RemoveInvalidPathSymbols(MediaImageHelper.CreateVersionedFileName(originalImage.OriginalFileName, GetVersion(originalImage)));
                    mediaImageVersionPathService.SetPathForNewOriginal(originalImage, folderName, publicFileName, ImageHelper.GetImageType(originalImage.OriginalFileExtension), archivedImage.OriginalUri, archivedImage.PublicOriginallUrl);
                }
                else
                {
                    originalImage.PublicUrl          = publicUrlTemp;
                    originalImage.FileUri            = fileUriTemp;
                    originalImage.PublicThumbnailUrl = publicThumbnailUrlTemp;
                    originalImage.ThumbnailUri       = thumbnailUriTemp;
                    originalImage.PublicOriginallUrl = publicOriginallUrlTemp;
                    originalImage.OriginalUri        = originalUriTemp;
                }


                originalImage.Original    = null;
                originalImage.PublishedOn = DateTime.Now;

                if (image.IsEdited())
                {
                    originalImage.PublicOriginallUrl = image.PublicOriginallUrl;
                    originalImage.OriginalUri        = image.OriginalUri;
                }

                unitOfWork.BeginTransaction();
                repository.Save(originalImage);
                unitOfWork.Commit();

                if (!image.IsEdited())
                {
                    using (var fileStreamReplica = new MemoryStream())
                    {
                        fileStream.CopyTo(fileStreamReplica);
                        storageService.UploadObject(new UploadRequest {
                            InputStream = fileStreamReplica, Uri = originalImage.OriginalUri, IgnoreAccessControl = true
                        });
                    }
                }
                storageService.UploadObject(new UploadRequest {
                    InputStream = fileStream, Uri = originalImage.FileUri, IgnoreAccessControl = true
                });

                UpdateThumbnail(originalImage, Size.Empty);

                return(originalImage);
            }
        }
        /// <summary>
        /// Uploads the image.
        /// </summary>
        /// <param name="rootFolderId">The root folder id.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="fileLength">Length of the file.</param>
        /// <param name="fileStream">The file stream.</param>
        /// <param name="reuploadMediaId">The reupload media identifier.</param>
        /// <param name="overrideUrl">if set to <c>true</c> override URL.</param>
        /// <returns>
        /// Image entity.
        /// </returns>
        public MediaImage UploadImage(Guid rootFolderId, string fileName, long fileLength, Stream fileStream, Guid reuploadMediaId, bool overrideUrl = true)
        {
            overrideUrl = false; // TODO: temporary disabling feature #1055.
            using (var thumbnailFileStream = new MemoryStream())
            {
                MediaImage originalImage;
                string     folderName;
                string     publicFileName;

                fileStream = RotateImage(fileStream);
                var size = GetSize(fileStream);

                CreatePngThumbnail(fileStream, thumbnailFileStream, ThumbnailSize);

                if (!reuploadMediaId.HasDefaultValue())
                {
                    // Re-uploading image: Get original image, folder name, file extension, file name
                    originalImage = repository.First <MediaImage>(image => image.Id == reuploadMediaId);
                    folderName    = Path.GetFileName(Path.GetDirectoryName(originalImage.FileUri.OriginalString));
                    MediaImage clonedOriginalImage = (MediaImage)originalImage.Clone();
                    clonedOriginalImage.Original = originalImage;

                    // Get original file stream
                    using (var originalFileStream = DownloadFileStream(originalImage.PublicUrl))
                    {
                        // Get thumbnail file stream
                        using (var originalThumbnailFileStream = DownloadFileStream(originalImage.PublicThumbnailUrl))
                        {
                            // Check is re-uploaded image has the same extension as original
                            var reuploadedFileExtension = Path.GetExtension(fileName);
                            if (reuploadedFileExtension != null && !reuploadedFileExtension.Equals(originalImage.OriginalFileExtension))
                            {
                                fileStream = UpdateCodec(fileStream, originalFileStream);
                            }

                            // Create version file name for current original image
                            var historicalUrl = MediaImageHelper.CreateHistoricalVersionedFileName(
                                clonedOriginalImage.OriginalFileName,
                                clonedOriginalImage.OriginalFileExtension);

                            // Update urls with version file name
                            mediaImageVersionPathService.SetPathForArchive(clonedOriginalImage, folderName, historicalUrl);

                            unitOfWork.BeginTransaction();
                            repository.Save(clonedOriginalImage);
                            unitOfWork.Commit();

                            // Re-upload original and thumbnail images to version urls
                            StartTasksForImage(clonedOriginalImage, originalFileStream, originalThumbnailFileStream, originalImage.IsEdited());
                        }
                    }

                    UpdateImageProperties(originalImage, rootFolderId, originalImage.OriginalFileName, originalImage.OriginalFileExtension, originalImage.Title, size, fileLength,
                                          thumbnailFileStream.Length);

                    if (!overrideUrl)
                    {
                        publicFileName = MediaImageHelper.CreateVersionedFileName(originalImage.OriginalFileName, GetVersion(originalImage));
                        mediaImageVersionPathService.SetPathForNewOriginal(originalImage, folderName, publicFileName);
                    }
                }
                else
                {
                    // Uploading new image
                    folderName     = mediaFileService.CreateRandomFolderName();
                    publicFileName = MediaImageHelper.CreatePublicFileName(fileName, Path.GetExtension(fileName));

                    // Create new original image and upload file stream to the storage
                    originalImage = CreateImage(rootFolderId, fileName, Path.GetExtension(fileName), fileName, size, fileLength, thumbnailFileStream.Length);
                    mediaImageVersionPathService.SetPathForNewOriginal(originalImage, folderName, publicFileName);
                }

                unitOfWork.BeginTransaction();
                repository.Save(originalImage);
                unitOfWork.Commit();

                StartTasksForImage(originalImage, fileStream, thumbnailFileStream, false);

                return(originalImage);
            }
        }
        /// <summary>
        /// Uploads the image.
        /// </summary>
        /// <param name="rootFolderId">The root folder id.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="fileLength">Length of the file.</param>
        /// <param name="fileStream">The file stream.</param>
        /// <returns>Image entity.</returns>
        public MediaImage UploadImage(Guid rootFolderId, string fileName, long fileLength, Stream fileStream, Guid reuploadMediaId, MediaImage filledInImage = null)
        {
            var size = GetSize(fileStream);

            using (var thumbnailFileStream = new MemoryStream())
            {
                MediaImage publicImage;
                string     folderName;
                string     publicFileName;

                fileStream = RotateImage(fileStream);
                CreatePngThumbnail(fileStream, thumbnailFileStream, ThumbnailSize);

                if (!reuploadMediaId.HasDefaultValue())
                {
                    // Re-uploading image: Get original image, folder name, file extension, file name
                    var originalImage = repository.First <MediaImage>(image => image.Id == reuploadMediaId);
                    folderName     = Path.GetFileName(Path.GetDirectoryName(originalImage.FileUri.OriginalString));
                    publicFileName = MediaImageHelper.CreatePublicFileName(originalImage.OriginalFileName, originalImage.OriginalFileExtension);

                    // Get original file stream
                    using (var originalFileStream = DownloadFileStream(originalImage.PublicUrl))
                    {
                        // Get thumbnail file stream
                        using (var originalThumbnailFileStream = DownloadFileStream(originalImage.PublicThumbnailUrl))
                        {
                            // Check is re-uploaded image has the same extension as original
                            var reuploadedFileExtension = Path.GetExtension(fileName);
                            if (reuploadedFileExtension != null && !reuploadedFileExtension.Equals(originalImage.OriginalFileExtension))
                            {
                                fileStream = UpdateCodec(fileStream, originalFileStream);
                            }

                            // Create version file name for current original image
                            var versionedFileName = MediaImageHelper.CreateVersionedFileName(
                                originalImage.OriginalFileName, originalImage.OriginalFileExtension, GetVersion(originalImage));

                            // Update urls with version file name
                            mediaImageVersionPathService.SetPathForArchive(originalImage, folderName, versionedFileName);

                            // Re-upload original and thumbnail images to version urls
                            StartTasksForImage(originalImage, originalFileStream, originalThumbnailFileStream, originalImage.IsEdited());
                        }
                    }

                    publicImage = CreateImage(rootFolderId, originalImage.OriginalFileName, originalImage.OriginalFileExtension, originalImage.Title, size, fileLength,
                                              thumbnailFileStream.Length);

                    mediaImageVersionPathService.SetPathForNewOriginal(publicImage, folderName, publicFileName);
                }
                else
                {
                    // Uploading new image
                    folderName     = mediaFileService.CreateRandomFolderName();
                    publicFileName = MediaImageHelper.CreatePublicFileName(fileName, Path.GetExtension(fileName));

                    // Create new original image and upload file stream to the storage
                    publicImage = CreateImage(rootFolderId, fileName, Path.GetExtension(fileName), fileName, size, fileLength, thumbnailFileStream.Length, filledInImage);
                    mediaImageVersionPathService.SetPathForNewOriginal(publicImage, folderName, publicFileName);
                }

                unitOfWork.BeginTransaction();
                repository.Save(publicImage);
                unitOfWork.Commit();

                StartTasksForImage(publicImage, fileStream, thumbnailFileStream);

                if (!reuploadMediaId.HasDefaultValue())
                {
                    // Update original id for version image
                    var originalMedia = repository.First <MediaImage>(image => image.Id == reuploadMediaId);
                    originalMedia.Original = publicImage;

                    unitOfWork.BeginTransaction();
                    while (true)
                    {
                        var preOriginalId  = originalMedia.Id;
                        var prePreOriginal = repository.FirstOrDefault <MediaImage>(i => i.Original != null && i.Original.Id == preOriginalId);
                        if (prePreOriginal != null)
                        {
                            prePreOriginal.Original = publicImage;
                            repository.Save(prePreOriginal);
                            preOriginalId = prePreOriginal.Id;
                        }
                        else
                        {
                            break;
                        }
                    }

                    repository.Save(originalMedia);
                    unitOfWork.Commit();
                }

                return(publicImage);
            }
        }