public async Task <IActionResult> UploadImage(IFormFile file)
        {
            if (this.ModelState.IsValid)
            {
                if (file == null)
                {
                    return(this.BadRequest());
                }

                ImageValidateResult result = this.ImageValidator.ValidateImageFile(file);
                if (!result.IsValid)
                {
                    return(this.Json(new ResponseUploadFileController()
                    {
                        Result = false, ErrorMessage = "Invalid image format!"
                    }));
                }

                if (!this.IsCorrectImageSize(file, out string errMessage))
                {
                    return(this.Json(new ResponseUploadFileController()
                    {
                        Result = false, ErrorMessage = errMessage
                    }));
                }

                StoreFileInfo info = await this.UploadFileToLocalFolder(file);

                if (info.BoolResult)
                {
                    string filePath = this.Env.WebRootPath + info.FileAddress;

                    // start backgroun process
                    this.Queue.Enqueue(new ImageInfoParams()
                    {
                        ImageId = info.FileId, ImagePath = filePath
                    });

                    return(this.Json(new ResponseUploadFileController()
                    {
                        Result = true,
                        ImageUrl = info.FileAddress,
                        FileId = info.FileId,
                    }));
                }
                else
                {
                    return(this.Json(new ResponseUploadFileController()
                    {
                        Result = false,
                        ErrorMessage = "Error uploading file to storage",
                    }));
                }
            }
            else
            {
                return(this.BadRequest());
            }
        }
示例#2
0
        private async Task <StoreFileInfo> GenerateThumbnailImage(string fullPath)
        {
            byte[] image = this.ResizeImageToThumbnail(fullPath);

            using MemoryStream cropImage = new MemoryStream(image);
            StoreFileInfo storeFile = await this.storageService.UploadFile(
                new UploadDataInfo(
                    Path.GetFileName(fullPath),
                    cropImage,
                    "WebPictures",
                    string.Empty));

            return(storeFile);
        }
        private async Task <StoreFileInfo> UploadFileToLocalFolder(IFormFile file)
        {
            string fileId          = Guid.NewGuid().ToString();
            string folderForResize = Path.Combine(
                this.Env.WebRootPath,
                this.Configuration.GetSection("Images:LocalImageFolder").Value,
                fileId);

            using MemoryStream stream = new MemoryStream();
            file.CopyTo(stream);
            stream.Position = 0;
            StoreFileInfo info = await this.LocalStorageService.UploadFile(
                new UploadDataInfo(
                    file.FileName,
                    stream,
                    folderForResize));

            info.FileId      = fileId;
            info.FileAddress = info.FileAddress.Replace(this.Env.WebRootPath, string.Empty);
            return(info);
        }
示例#4
0
        private async Task <bool> SaveThumbnailImage(ImageInfoParams imageInfoParam)
        {
            StoreFileInfo remoteImage = await this.GenerateThumbnailImage(imageInfoParam.ImagePath);

            if (!remoteImage.BoolResult)
            {
                return(false);
            }

            TempCloudImage newCloudImage = new TempCloudImage()
            {
                ImageId   = imageInfoParam.ImageId,
                FileId    = remoteImage.FileId,
                ImageUrl  = remoteImage.FileAddress,
                ImageType = (int)ImageType.Thumbnail,
            };

            await this.tempCloudImage.CreateAsync(newCloudImage);

            return(true);
        }
示例#5
0
        private async Task <StoreFileInfo> UploadOriginalFile(string fullPath)
        {
            try
            {
                using FileStream str      = new FileStream(fullPath, FileMode.Open);
                using MemoryStream memory = new MemoryStream();
                str.CopyTo(memory);
                memory.Position = 0;
                StoreFileInfo storeFile = await this.storageService.UploadFile(
                    new UploadDataInfo(
                        Path.GetFileName(fullPath),
                        memory,
                        "WebPictures",
                        string.Empty));

                return(storeFile);
            }
            catch (Exception e)
            {
                this.logger.LogError("Error upload original file", e);
                return(new StoreFileInfo(false));
            }
        }
示例#6
0
 public Task <bool> DeleteData(StoreFileInfo fileInfo)
 {
     throw new System.NotImplementedException();
 }