/// <summary> /// Upload image from the stream. /// </summary> /// <param name="request">The upload image request.</param> /// <returns>The upload image response.</returns> public UploadImageResponse Post(UploadImageRequest request) { MediaFolder parentFolder = null; if (request.Data.FolderId.HasValue) { parentFolder = repository.AsQueryable <MediaFolder>() .Where(c => c.Id == request.Data.FolderId.Value && !c.IsDeleted) .FirstOne(); if (parentFolder.Type != Module.MediaManager.Models.MediaType.Image) { throw new CmsApiValidationException("Folder must be type of an image."); } } var maxLength = configuration.Storage.MaximumFileNameLength > 0 ? configuration.Storage.MaximumFileNameLength : 100; // Fix for IIS express + IE (if full path is returned) var fileName = Path.GetFileName(request.Data.FileName); if (fileName.Length > maxLength) { fileName = string.Concat(Path.GetFileNameWithoutExtension(fileName.Substring(0, maxLength)), Path.GetExtension(fileName)); } var mediaImage = new MediaImage { Id = request.Data.Id.GetValueOrDefault(), Type = Module.MediaManager.Models.MediaType.Image, Caption = request.Data.Caption, Title = request.Data.Title ?? fileName, Description = request.Data.Description, Size = request.Data.FileStream.Length, Folder = parentFolder, OriginalFileName = fileName, OriginalFileExtension = Path.GetExtension(fileName) }; var savedImage = mediaImageService.UploadImageWithStream(request.Data.FileStream, mediaImage, request.Data.WaitForUploadResult); if (savedImage != null) { Events.MediaManagerEvents.Instance.OnMediaFileUploaded(savedImage); } return(new UploadImageResponse { Data = savedImage.Id, }); }
/// <summary> /// Upload image from the stream. /// </summary> /// <param name="request">The upload image request.</param> /// <returns>The upload image response.</returns> public UploadImageResponse Post(UploadImageRequest request) { MediaFolder parentFolder = null; if (request.Data.FolderId.HasValue) { parentFolder = repository.AsQueryable <MediaFolder>() .Where(c => c.Id == request.Data.FolderId.Value && !c.IsDeleted) .FirstOne(); if (parentFolder.Type != Module.MediaManager.Models.MediaType.Image) { throw new CmsApiValidationException("Folder must be type of an image."); } } var mediaImage = new MediaImage { Id = request.Data.Id.GetValueOrDefault(), Type = Module.MediaManager.Models.MediaType.Image, Caption = request.Data.Caption, Title = request.Data.Title ?? Path.GetFileName(request.Data.FileName), Description = request.Data.Description, Size = request.Data.FileStream.Length, Folder = parentFolder, OriginalFileName = request.Data.FileName, OriginalFileExtension = Path.GetExtension(request.Data.FileName) }; var savedImage = mediaImageService.UploadImageWithStream(request.Data.FileStream, mediaImage, request.Data.WaitForUploadResult); if (savedImage != null) { Events.MediaManagerEvents.Instance.OnMediaFileUploaded(savedImage); } return(new UploadImageResponse { Data = savedImage.Id, }); }