private async Task <PhotoIdentifier> AddThumbnail( [NotNull] Stream photoData, string filename, string contentType, PhotoCategory photoCategory) { if (photoData == null) { throw new ArgumentNullException(nameof(photoData)); } var thumbnailPhotoData = await GeneratedThumbnailAsync(photoData, photoCategory, contentType); var photoIdentifier = new PhotoIdentifier(photoCategory, true, filename); photoData.Seek(0, 0); await photoStore.AddPhotoAsync( new Photo( photoIdentifier, "image/jpg", thumbnailPhotoData)); return(photoIdentifier); }
public void Edit_UpdateTeaserImage() { var logbookEntry = new LogbookEntry { Id = new Guid("601A3EC6-2BB7-4A47-83CF-389429824CEB"), Title = "Test Title", TeaserImage = "the_orig_image", TeaserImageThumb = "the_thumb_image", }; var newOriginalPhotoIdentifier = new PhotoIdentifier(PhotoCategory.LogbookTeaser, false, "image.jpg"); var newThumbnailPhotoIdentifier = new PhotoIdentifier(PhotoCategory.LogbookTeaser, true, "image.jpg"); logbookEntry.Edit( "New Title", "New Teaser", "New Text", false, new Guid("4B3B79AE-3062-44FE-8AA1-14D425C081FA"), null, null, new PhotoAndThumbnailIdentifiers( newOriginalPhotoIdentifier, newThumbnailPhotoIdentifier) ); logbookEntry.TeaserImage.Should().Be(newOriginalPhotoIdentifier.Serialze()); logbookEntry.TeaserImageThumb.Should().Be(newThumbnailPhotoIdentifier.Serialze()); }
/// <inheritdoc /> #pragma warning disable 1998 public async Task RemovePhotoAsync(PhotoIdentifier photoIdentifier) { if (photoIdentifier == null) { throw new ArgumentNullException(nameof(photoIdentifier)); } var filePath = filePathCalculator .CalculatePath( configuration.RootFolder, photoIdentifier); if (!File.Exists(filePath)) { logger.LogWarning( $"Photo file can not be deleted from disk because it does not exists! File: [{filePath}]", photoIdentifier); return; } try { File.Delete(filePath); } catch (IOException ex) { logger.LogError($"Error deleting photo file [{filePath}] from disk!", ex, photoIdentifier); } }
private static Photo CreateSamplePhoto() { var identifier = new PhotoIdentifier(PhotoCategory.LogbookTeaser, false, "test.jpg"); var contentType = "image/jpeg"; Stream content = new MemoryStream(new byte[] { 42, 21, 1 }); return(new Photo(identifier, contentType, content)); }
public void TestSerializeThumbPhotoIdentifier() { var result = new PhotoIdentifier("LogbookTeaser/thumbs/myimage.jpg"); result.Category.Should().Be(PhotoCategory.LogbookTeaser); result.IsThumb.Should().BeTrue(); result.Filename.Should().Be("myimage.jpg"); }
public async Task <Photo> GetPhotoDataAsync(PhotoIdentifier photoIdentifier) { if (photoIdentifier == null) { throw new ArgumentNullException(nameof(photoIdentifier)); } return(await photoStore.GetPhotoAsync(photoIdentifier)); }
public async Task <IActionResult> Photo(string photoId) { var photoIdentifier = new PhotoIdentifier(WebUtility.UrlDecode(photoId)); var photo = await photoService.GetPhotoDataAsync(photoIdentifier); if (photo?.Content == null) { return(BadRequest()); } return(File(photo.Content, photo.ContentType, photo.Identifier.Filename)); }
private async Task <CloudBlockBlob> GetCloudBlockBlob([NotNull] PhotoIdentifier photoIdentifier) { if (photoIdentifier == null) { throw new ArgumentNullException(nameof(photoIdentifier)); } var cloudBlobContainer = await OpenOrCreateBlobContainerAsync(); var result = cloudBlobContainer.GetBlockBlobReference(GetBlobName(photoIdentifier)); return(result); }
public string CalculatePath( string rootPath, PhotoIdentifier photoIdentifier) { if (rootPath == null) { throw new ArgumentNullException(nameof(rootPath)); } if (photoIdentifier == null) { throw new ArgumentNullException(nameof(photoIdentifier)); } return(CombinePath( rootPath, photoIdentifier.Category, photoIdentifier.IsThumb, photoIdentifier.Filename)); }
/// <inheritdoc /> public async Task RemovePhotoAsync([NotNull] PhotoIdentifier photoIdentifier) { if (photoIdentifier == null) { throw new ArgumentNullException(nameof(photoIdentifier)); } logger.LogInformation( "Removing photo {PhotoIdentifier}", photoIdentifier.ToString()); var cloudBlob = await GetCloudBlockBlob(photoIdentifier); await cloudBlob.DeleteIfExistsAsync(); telemetry.TrackEvent("AZUREBLOBSTORE_REMOVEPHOTO", new Dictionary <string, string> { { nameof(photoIdentifier), photoIdentifier.ToString() } }); }
private async Task <PhotoIdentifier> AddOriginalPhoto( [NotNull] Stream photoData, string filename, string contentType, PhotoCategory photoCategory) { if (photoData == null) { throw new ArgumentNullException(nameof(photoData)); } var photoIdentifier = new PhotoIdentifier(photoCategory, false, filename); photoData.Seek(0, 0); await photoStore.AddPhotoAsync( new Photo( photoIdentifier, contentType, photoData)); return(photoIdentifier); }
#pragma warning restore 1998 /// <inheritdoc /> public async Task <Photo> GetPhotoAsync(PhotoIdentifier photoIdentifier) { if (photoIdentifier == null) { throw new ArgumentNullException(nameof(photoIdentifier)); } var filePath = filePathCalculator.CalculatePath(configuration.RootFolder, photoIdentifier); using (var fileStream = new FileStream(filePath, FileMode.Open)) { var memStream = new MemoryStream(); await fileStream.CopyToAsync(memStream); memStream.Seek(0, 0); return(new Photo( photoIdentifier, mimeMapping.GetMimeMapping(Path.GetExtension(photoIdentifier.Filename)), memStream)); } }
/// <inheritdoc /> public async Task <Photo> GetPhotoAsync(PhotoIdentifier photoIdentifier) { if (photoIdentifier == null) { throw new ArgumentNullException(nameof(photoIdentifier)); } logger.LogInformation("Retrieving photo (Get) with identifier [{PhotoIdentifier}]", photoIdentifier); try { var cloudBlobContainer = await OpenOrCreateBlobContainerAsync(); var cloudBlob = cloudBlobContainer.GetBlockBlobReference(GetBlobName(photoIdentifier)); var memStream = new MemoryStream(); await cloudBlob.DownloadToStreamAsync(memStream); memStream.Seek(0, 0); await cloudBlob.FetchAttributesAsync(); var contentType = mimeMapping.GetMimeMapping(Path.GetExtension(photoIdentifier.Filename)); logger.LogInformation("Retrieved photo (Get) with identifier [{PhotoIdentifier}] successfully.", photoIdentifier); return(new Photo(photoIdentifier, contentType, memStream)); } catch (Exception ex) { logger.LogError( ex, "Error retrieving image from Azure Blob Storage! Photo-Identifier: [{PhotoIdentifier}]", photoIdentifier); throw; } }
private static string GetBlobName(PhotoIdentifier photoIdentifier) { return(photoIdentifier.IsThumb ? $"{photoIdentifier.Category.ToString()}/{ThumbsPrefix}/{photoIdentifier.Filename}" : $"{photoIdentifier.Category.ToString()}/{photoIdentifier.Filename}"); }