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