public async Task UploadThumbnailImageAsync(int id, MemoryStream stream, string fileExtension, CancellationToken cancellationToken) { var uniqueFileName = Convert.ToString(Guid.NewGuid()); var directoryPath = Path.Combine(_environment.WebRootPath, ItemCardConstants.thumbnailsMainDirectory, $@"{ItemCardConstants.categorySubdirectoryPrefix}{id}"); _fileProcessingService.CreateDirectory(directoryPath); var image = Image.FromStream(stream); var pathForImage = Path.Combine(directoryPath, $@"{uniqueFileName + fileExtension}"); _fileProcessingService.CreateScaledImage(image, pathForImage, ItemCardConstants.thumbnailImageSize, ItemCardConstants.thumbnailImageSize); var itemCardRepository = _unitOfWork.GetRepository <DAL.Model.ItemCard>(); var itemCardRecord = await itemCardRepository.FindAsync(id).ConfigureAwait(false); if (itemCardRecord == null) { return; } if (!string.IsNullOrWhiteSpace(itemCardRecord.ThumbnailImagePath)) { var oldImagePath = Path.Combine(_environment.WebRootPath, itemCardRecord.ThumbnailImagePath); _fileProcessingService.DeleteFile(oldImagePath); } itemCardRecord.ThumbnailImagePath = Path.Combine(ItemCardConstants.thumbnailsMainDirectory, $@"{ItemCardConstants.categorySubdirectoryPrefix}{id}", $@"{uniqueFileName + fileExtension}"); await _unitOfWork.SaveChangesAsync(); }
public async Task DeleteImageAsync(int id, CancellationToken cancellationToken) { var itemCardImageRepository = _unitOfWork.GetRepository <ItemCardImage>(); var itemCardImageRecord = await itemCardImageRepository.FindAsync(id).ConfigureAwait(false); if (itemCardImageRecord == null) { return; } if (!string.IsNullOrWhiteSpace(itemCardImageRecord.ImagePath)) { var oldImagePath = Path.Combine(_environment.WebRootPath, itemCardImageRecord.ImagePath); _fileProcessingService.DeleteFile(oldImagePath); } itemCardImageRepository.Delete(itemCardImageRecord); await _unitOfWork.SaveChangesAsync().ConfigureAwait(false); }