private MediaImage RevertChanges(MediaImage canceledImage)
        {
            var previousOriginal =
                repository.AsQueryable <MediaImage>().OrderByDescending(i => i.PublishedOn).FirstOrDefault(f => f.Original != null && f.Original.Id == canceledImage.Id);

            if (previousOriginal != null)
            {
                var folderName     = Path.GetFileName(Path.GetDirectoryName(previousOriginal.FileUri.OriginalString));
                var publicFileName = MediaHelper.RemoveInvalidPathSymbols(MediaImageHelper.CreatePublicFileName(previousOriginal.OriginalFileName, previousOriginal.OriginalFileExtension));

                // Get original file stream
                using (var fileStream = DownloadFileStream(previousOriginal.PublicUrl))
                {
                    // Get thumbnail file stream
                    using (var thumbnailFileStream = DownloadFileStream(previousOriginal.PublicThumbnailUrl))
                    {
                        previousOriginal.CopyDataTo(canceledImage);

                        mediaImageVersionPathService.SetPathForArchive(canceledImage, folderName, publicFileName);

                        StartTasksForImage(canceledImage, fileStream, thumbnailFileStream, previousOriginal.IsEdited());

                        canceledImage.Original = null;
                        unitOfWork.BeginTransaction();
                        repository.Save(canceledImage);
                        unitOfWork.Commit();
                    }
                }
            }

            return(previousOriginal);
        }
        /// <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 clonnedOriginalImage = (MediaImage)originalImage.Clone();

            clonnedOriginalImage.Original = originalImage;

            var historicalFileName = MediaImageHelper.CreateHistoricalVersionedFileName(
                originalImage.OriginalFileName,
                originalImage.OriginalFileExtension);

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

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

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

                    StartTasksForImage(clonnedOriginalImage, originalFileStream, originalThumbnailFileStream, originalImage.IsEdited());
                }
            }
            return(clonnedOriginalImage);
        }
示例#4
0
        public MediaImage UploadImageWithStream(Stream fileStream, MediaImage image, bool waitForUploadResult = false)
        {
            using (var thumbnailFileStream = new MemoryStream())
            {
                fileStream = RotateImage(fileStream);
                var size = GetSize(fileStream);

                CreatePngThumbnail(fileStream, thumbnailFileStream, ThumbnailSize);

                var folderName     = mediaFileService.CreateRandomFolderName();
                var publicFileName = RemoveInvalidHtmlSymbols(MediaImageHelper.CreatePublicFileName(image.OriginalFileName, image.OriginalFileExtension));

                // Create new original image and upload file stream to the storage
                var originalImage = CreateImage(null, image.OriginalFileName, image.OriginalFileExtension, image.Title, size, image.Size, thumbnailFileStream.Length, image);
                mediaImageVersionPathService.SetPathForNewOriginal(originalImage, folderName, publicFileName);

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

                if (!waitForUploadResult)
                {
                    unitOfWork.Commit();
                }

                StartTasksForImage(originalImage, fileStream, thumbnailFileStream, false, waitForUploadResult);

                if (waitForUploadResult)
                {
                    unitOfWork.Commit();
                    Events.MediaManagerEvents.Instance.OnMediaFileUpdated(originalImage);
                }

                return(originalImage);
            }
        }
        /// <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));
        }
示例#6
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));
        }
示例#7
0
    public void Init()
    {
        switch (media.type)
        {
        case "photo":
            StartCoroutine(MediaImageHelper.PlayPhoto(transform, media.media_url, rawImageRegularSize));
            break;

        case "video":
            _VideoPlayer = gameObject.AddComponent <VideoPlayer>();
            StartCoroutine(MediaImageHelper.PlayVideo(media, _VideoPlayer, rawImageRegularSize, false));
            break;
        }
    }
示例#8
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>
        /// <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())
            {
                var folderName = mediaFileService.CreateRandomFolderName();

                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
                    MediaImage reuploadImage = (MediaImage)repository.First <MediaImage>(image => image.Id == reuploadMediaId).Clone();
                    reuploadImage.IsTemporary = true;
                    var publicFileName = RemoveInvalidHtmlSymbols(MediaImageHelper.CreatePublicFileName(fileName, Path.GetExtension(fileName)));

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

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

                    StartTasksForImage(reuploadImage, fileStream, thumbnailFileStream, false);

                    return(reuploadImage);
                }
                else
                {
                    // Uploading new image
                    var publicFileName = RemoveInvalidHtmlSymbols(MediaImageHelper.CreatePublicFileName(fileName, Path.GetExtension(fileName)));

                    // Create new original image and upload file stream to the storage
                    MediaImage 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);
                }
            }
        }
示例#9
0
    void ShowFirstMedia()
    {
        Media media = extendedEntities.media[0];

        switch (media.type)
        {
        case "photo":
            StartCoroutine(MediaImageHelper.PlayPhoto(transform, media.media_url, rawImageRegularSize, MediaCallback));
            break;

        case "video":
            _VideoPlayer = gameObject.AddComponent <VideoPlayer>();
            StartCoroutine(MediaImageHelper.PlayVideo(media, _VideoPlayer, rawImageRegularSize, false, MediaCallback));
            break;
        }
    }
        /// <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>
        /// <returns>The new original image.</returns>
        public MediaImage MakeAsOriginal(MediaImage image, MediaImage originalImage, MediaImage archivedImage)
        {
            var folderName     = Path.GetFileName(Path.GetDirectoryName(originalImage.FileUri.OriginalString));
            var publicFileName = MediaImageHelper.CreatePublicFileName(originalImage.OriginalFileName, originalImage.OriginalFileExtension);

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

                mediaImageVersionPathService.SetPathForNewOriginal(originalImage, folderName, publicFileName, archivedImage.OriginalUri, archivedImage.PublicOriginallUrl);

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

                archivedImage.Original = originalImage;

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

                storageService.UploadObject(new UploadRequest {
                    InputStream = fileStream, Uri = originalImage.FileUri, IgnoreAccessControl = true
                });
                if (!image.IsEdited())
                {
                    storageService.UploadObject(new UploadRequest {
                        InputStream = fileStream, Uri = originalImage.OriginalUri, IgnoreAccessControl = true
                    });
                }
                UpdateThumbnail(originalImage, Size.Empty);

                return(originalImage);
            }
        }
        /// <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>
        public void SaveEditedImage(MediaImage image, MediaImage archivedImage, MemoryStream croppedImageFileStream)
        {
            var folderName     = Path.GetFileName(Path.GetDirectoryName(image.FileUri.OriginalString));
            var publicFileName = MediaImageHelper.CreatePublicFileName(image.OriginalFileName, image.OriginalFileExtension);

            using (var fileStream = croppedImageFileStream)
            {
                image.Original    = null;
                image.PublishedOn = DateTime.Now;
                mediaImageVersionPathService.SetPathForNewOriginal(image, folderName, publicFileName, archivedImage.OriginalUri, archivedImage.PublicOriginallUrl);

                archivedImage.Original = image;

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

                storageService.UploadObject(new UploadRequest {
                    InputStream = fileStream, Uri = image.FileUri, IgnoreAccessControl = true
                });
                UpdateThumbnail(image, Size.Empty);
            }
        }
示例#12
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.

            var folderName     = mediaFileService.CreateRandomFolderName();
            var publicFileName = MediaHelper.RemoveInvalidPathSymbols(MediaImageHelper.CreatePublicFileName(fileName, Path.GetExtension(fileName)));
            var fileExtension  = Path.GetExtension(fileName);
            var imageType      = ImageHelper.GetImageType(fileExtension);

            MediaImage uploadImage         = null;
            var        thumbnailFileStream = new MemoryStream();
            Size       size;
            Size       thumbnailSize = ThumbnailSize;
            long       thumbnailImageLength;

            /* Upload standard raster type images */
            if (imageType == ImageType.Raster)
            {
                fileStream = RotateImage(fileStream);
                size       = GetSize(fileStream);

                CreatePngThumbnail(fileStream, thumbnailFileStream, ThumbnailSize);
                thumbnailImageLength = thumbnailFileStream.Length;
            }
            /* Upload vector graphics images */
            else
            {
                size = Size.Empty;
                ReadParametersFormVectorImage(fileStream, ref size);
                thumbnailImageLength = fileStream.Length;
                CreateSvgThumbnail(fileStream, thumbnailFileStream, ThumbnailSize);
            }

            try
            {
                if (!reuploadMediaId.HasDefaultValue())
                {
                    // Re-uploading image: Get original image, folder name, file extension, file name
                    uploadImage             = (MediaImage)repository.First <MediaImage>(image => image.Id == reuploadMediaId).Clone();
                    uploadImage.IsTemporary = true;

                    // Create new original image and upload file stream to the storage
                    uploadImage = CreateImage(rootFolderId, fileName, fileExtension, Path.GetFileName(fileName), size, fileLength);
                }
                else
                {
                    // Uploading new image
                    // Create new original image and upload file stream to the storage
                    uploadImage = CreateImage(
                        rootFolderId,
                        fileName,
                        fileExtension,
                        Path.GetFileName(fileName),
                        size,
                        fileLength);
                }
                SetThumbnailParameters(uploadImage, thumbnailSize, thumbnailImageLength);
                mediaImageVersionPathService.SetPathForNewOriginal(uploadImage, folderName, publicFileName, imageType);

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

                StartTasksForImage(uploadImage, fileStream, thumbnailFileStream, false);
            }
            finally
            {
                if (thumbnailFileStream != null)
                {
                    thumbnailFileStream.Dispose();
                }
            }

            return(uploadImage);
        }
        /// <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);
            }
        }