示例#1
0
        public async Task UploadImageAsync(int itemCardId, MemoryStream stream, string fileExtension, CancellationToken cancellationToken)
        {
            var itemCardRepository = _unitOfWork.GetRepository <DAL.Model.ItemCard>();
            var checkItemCard      = await itemCardRepository.FindAsync(itemCardId).ConfigureAwait(false);

            if (checkItemCard == null)
            {
                return;
            }

            var uniqueFileName = Convert.ToString(Guid.NewGuid());
            var directoryPath  = Path.Combine(_environment.WebRootPath, ItemCardConstants.imagesMainDirectory, $@"{ItemCardConstants.itemCardSubdirectoryPrefix}{itemCardId}");

            _fileProcessingService.CreateDirectory(directoryPath);
            var image        = Image.FromStream(stream);
            var pathForImage = Path.Combine(directoryPath, $@"{uniqueFileName + fileExtension}");

            _fileProcessingService.CreateScaledImage(image, pathForImage, ItemCardConstants.imageWidthImageSize);

            var itemCardImageRepository = _unitOfWork.GetRepository <ItemCardImage>();
            var recordForAdd            = new ItemCardImage
            {
                ItemCardId = itemCardId,
                ImagePath  = Path.Combine(ItemCardConstants.imagesMainDirectory, $@"{ItemCardConstants.itemCardSubdirectoryPrefix}{itemCardId}", $@"{uniqueFileName + fileExtension}")
            };

            recordForAdd.ImagePath = recordForAdd.ImagePath.Replace('\\', '/');
            await itemCardImageRepository.InsertAsync(recordForAdd).ConfigureAwait(false);

            await _unitOfWork.SaveChangesAsync().ConfigureAwait(false);
        }
示例#2
0
        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();
        }