コード例 #1
0
ファイル: ImageService.cs プロジェクト: sachith-1/Multiseller
        public async Task <StorageFile> GetImage(string fileName, int width, int height)
        {
            try
            {
                var resizedFileName = $"{Path.GetFileNameWithoutExtension(fileName)}_{width}_{height}{Path.GetExtension(fileName)}";

                //If exists cached resized file return it

                if (await _imageStorageService.ImageExists(resizedFileName))
                {
                    return(await _imageStorageService.GetImage(resizedFileName));
                }

                //If not exists cached resize file, generate it and store it

                var file = await _imageStorageService.GetImage(fileName);

                var resizeStream = _imageResizeService.ResizeImage(width, height, file.Stream);

                await _imageStorageService.UploadImage(new ImageFile()
                {
                    Stream      = resizeStream,
                    ContentType = file.ContentType,
                    FileName    = resizedFileName
                }, true);

                resizeStream.Position = 0;

                file.Stream = resizeStream;

                return(file);
            }
            catch (Exception e)
            {
                if (e is ServiceException)
                {
                    throw;
                }

                throw new Exception($"Unable to process {fileName}", e);
            }
        }
コード例 #2
0
        public async Task <Image> InsertListingImage(int listingId, IFormFile file)
        {
            var folderPath       = $"{_environment}/listing/{listingId}";
            var cloudinaryResult = await _imageStorageService.UploadImage(file, folderPath);

            if (cloudinaryResult.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"Can not upload listing image {file.FileName}: {cloudinaryResult.Error.Message}");
            }

            var insertImage = await _imageRepository.InsertListingImage(listingId, cloudinaryResult.PublicId);

            if (insertImage == 1)
            {
                return(new Image {
                    Url = cloudinaryResult.SecureUri.OriginalString
                });
            }

            throw new Exception($"Can not upload listing image {file.FileName}: Database error");
        }