public Model.Data.Json.Image Copy(DiaryImage source) { var destination = new Model.Data.Json.Image() { Description = source.Description, Source = source.Path }; return(destination); }
protected async Task <DiaryImage> AddImageWithoutSaving(byte[] image, string imageName, int?newBiggestDimensionSize = null) { if (string.IsNullOrWhiteSpace(imageName)) { throw new ArgumentException("Image Name should not be empty"); } _ = image ?? throw new ArgumentNullException(nameof(image)); int imageQuality = await _appSettings.GetAppSettingInt(AppSettingsKey.ImageQuality) ?? throw new Exception("Setting Value ImageQuality not set"); int thumbnailSize = await _appSettings.GetAppSettingInt(AppSettingsKey.ThumbnailSize) ?? throw new Exception("Setting Value ThumbnailSize not set"); var(taken, cameraModel) = GetMetadataFromPhoto(image); var(width, height) = GetImageSize(image); if (newBiggestDimensionSize is not null) { if (width > newBiggestDimensionSize || height > newBiggestDimensionSize) { image = ScaleImage(image, imageQuality, newBiggestDimensionSize.Value); (width, height) = GetImageSize(image); } } var dImage = new DiaryImage { Id = Guid.NewGuid(), Name = imageName, CreateDate = DateTime.UtcNow, ModifyDate = DateTime.UtcNow, SizeByte = image.Length, Thumbnail = ScaleImage(image, imageQuality, thumbnailSize), Taken = taken, CameraModel = cameraModel, Height = height, Width = width, FullImage = new DiaryImageFull { Data = image } }; await _context.Images.AddAsync(dImage).ConfigureAwait(false); return(dImage); }
public static TempImage CropImage(DiaryImage image, byte[] fullImage, int left, int top, int width, int height, int imageQuality) { if (image == null) { throw new ArgumentNullException(nameof(image)); } var result = CropImage(fullImage, left, top, width, height, imageQuality); var temp = new TempImage { SourceImageId = image.Id, Modification = "Обрезка изображения", Data = result, SizeByte = result.Length }; (temp.Width, temp.Height) = GetImageSize(result); return(temp); }
public static TempImage ScaleImage(DiaryImage image, byte[] fullImage, int maxSizePx, int imageQuality) { if (image == null) { throw new ArgumentNullException(nameof(image)); } var result = ScaleImage(fullImage, imageQuality, maxSizePx); var temp = new TempImage { SourceImageId = image.Id, Modification = "Сжатие изображения", Data = result, SizeByte = result.Length }; (temp.Width, temp.Height) = GetImageSize(result); return(temp); }
public async Task <IActionResult> UploadAndPost([FromForm] DiaryEntry diaryEntry) { if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { return(BadRequest($"Expected a multipart request, but got {Request.ContentType}")); } else { try { // A diaryEntry could have multiple files or None. // We have to store the URI of each file in the DiaryImage table with its , associated with a eventID. // Save the event ID in a variable. // Create a diary log and upload the details. // Make the Story URI /* if (not zero files) * there is files * go through each file * upload the file to a blob and get the URI * Put the URI along with the width/height in a new diary image instance with the eventID created * Save changes */ // CloudBlockBlob uploadedTextBlob = await UploadTextToBlob(diaryEntry.Story); int eventID; // Successfully uploaded Blob and identifier is found DiaryLog diaryLogInstance = new DiaryLog { UserId = diaryEntry.UserID, EventName = diaryEntry.Event, StoryUrl = diaryEntry.Story, StartTime = diaryEntry.StartTime, EndTime = diaryEntry.EndTime }; _context.DiaryLog.Add(diaryLogInstance); await _context.SaveChangesAsync(); eventID = diaryLogInstance.Id; // if 0 images, nothing will be attempted to upload if (diaryEntry.Images != null && diaryEntry.Images.Any()) { foreach (IFormFile imageInstance in diaryEntry.Images) { // Read images as a stream of data so it can be uploaded to blob using (System.IO.Stream stream = imageInstance.OpenReadStream()) { // Create and upload a blob for the image. CloudBlockBlob uploadedImageBlob = await UploadImageToBlob(imageInstance.FileName, null, stream); string uploadedImageBlobURI = uploadedImageBlob.StorageUri.ToString(); if (!string.IsNullOrEmpty(uploadedImageBlobURI)) { System.Drawing.Image uploadedImage = System.Drawing.Image.FromStream(stream); // If there is a valid URI, then upload DiaryImage diaryImageInstance = new DiaryImage { // Problem : This is trying to upload before primary key eventId is made EntryId = eventID, ImageURL = uploadedImageBlob.SnapshotQualifiedUri.AbsoluteUri, Height = uploadedImage.Height.ToString(), Width = uploadedImage.Width.ToString() }; // Add this new instance to the dbset (collection of entities) in memory _context.DiaryImage.Add(diaryImageInstance); } } } } // Save all the changes to the database await _context.SaveChangesAsync(); return(Ok($"The file {diaryEntry.Event} has been succesfully uploaded")); #region /* * using (System.IO.Stream stream = diaryEntry.Image.OpenReadStream()) * { * // create and upload Blob for Image and Story * CloudBlockBlob uploadedImageBlob = await UploadImageToBlob(diaryEntry.Image.FileName, null, stream); * CloudBlockBlob uploadedTextBlob = await UploadTextToBlob(diaryEntry.Story); * * // The URI is an identifier for the blob. Its a more general version of a url. * string uploadedImageBlobURI = uploadedImageBlob.StorageUri.ToString(); * string uploadedTextBlobURI = uploadedTextBlob.StorageUri.ToString(); * * // If the URI is empty (there is no identifier for the blob), then something went wrong * if (string.IsNullOrEmpty(uploadedImageBlobURI) || string.IsNullOrEmpty(uploadedTextBlobURI)) * { * return BadRequest("Error when uploading: Identifier not found. Please Try Again"); * } * else * { * // Can now begin uploading data to database * * System.Drawing.Image image = System.Drawing.Image.FromStream(stream); * * // Create an instance of a Diary Log * DiaryLog diaryLog = new DiaryLog * { * EventName = diaryEntry.Event, * StoryUrl = uploadedTextBlob.SnapshotQualifiedUri.AbsoluteUri, // Get the URI for the text blob * StartTime = diaryEntry.StartTime, * EndTime = diaryEntry.EndTime, * ImageUrl = uploadedImageBlob.SnapshotQualifiedUri.AbsoluteUri, // Get the URI for the image blob * Height = image.Height.ToString(), * Width = image.Width.ToString() * }; * * // Adding the files to the database * _context.DiaryLog.Add(diaryLog); * await _context.SaveChangesAsync(); * * return Ok($"File: {diaryEntry.Event} has been successfully uploaded to the database"); * } * } */ #endregion } catch (Exception e) { return(BadRequest($"An error has occured g: {e.Message}")); } } }