public virtual IEnumerable <TmpFileData> Upload([FromForm] UploadRequestDto dto)
        {
            dto.Files = new List <FileBytes>();

            foreach (var f in Request.Form.Files)
            {
                using (MemoryStream str = new MemoryStream())
                {
                    f.CopyTo(str);
                    dto.Files.Add(new FileBytes(f.FileName, str.ToArray()));
                }
            }
            return(service.Upload(dto));
        }
        public virtual IEnumerable <TmpFileData> Upload(UploadRequestDto dto)
        {
            dto.AttachmentTypeId = dto.AttachmentTypeId == 0 ? DefaultAttachmentType : dto.AttachmentTypeId;
            var cat = unit.AttachmentCategoryRepository.FindSingle(dto.AttachmentTypeId);

            ValidateFiles(cat, dto.Files);

            List <TempFileDto> lst = new List <TempFileDto>();
            int i = 1;

            foreach (var d in dto.Files)
            {
                var tmpFile = AddToTemp(d, "file_" + (i++));
                var t       = tmpFile.MapTo <TempFileDto>(false);
                t.AttachmentTypeId = cat.Id;
                lst.Add(t);
            }
            return(lst);
        }
예제 #3
0
        /// <summary>
        /// Uploads to an S3 bucket with specified name
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="file"></param>
        /// <param name="objectTypeName"></param>
        /// <param name="metaTags"></param>
        /// <returns>An AWSUploadResult with the link to the uploaded content, if successful</returns>
        public async Task <AWSUploadResult <string> > UploadImageToS3BucketAsync(UploadRequestDto requestDto)
        {
            try
            {
                var    file       = requestDto.File;
                string bucketName = requestDto.BucketName;

                if (!IsValidImageFile(file))
                {
                    _logger.LogInformation("Invalid file");
                    return(new AWSUploadResult <string>
                    {
                        Status = false,
                        StatusCode = StatusCodes.Status400BadRequest
                    });
                }

                // Rename file to random string to prevent injection and similar security threats
                var trustedFileName    = WebUtility.HtmlEncode(file.FileName);
                var ext                = Path.GetExtension(file.FileName).ToLowerInvariant();
                var randomFileName     = Path.GetRandomFileName();
                var trustedStorageName = "files/" + randomFileName + ext;

                // Create the image object to be uploaded in memory
                var transferUtilityRequest = new TransferUtilityUploadRequest()
                {
                    InputStream = file.OpenReadStream(),
                    Key         = trustedStorageName,
                    BucketName  = bucketName,
                    CannedACL   = S3CannedACL.PublicRead, // Ensure the file is read-only to allow users view their pictures
                    PartSize    = 6291456
                };

                // Add metatags which can include the original file name and other decriptions
                var metaTags = requestDto.Metatags;
                if (metaTags != null && metaTags.Count() > 0)
                {
                    foreach (var tag in metaTags)
                    {
                        transferUtilityRequest.Metadata.Add(tag.Key, tag.Value);
                    }
                }

                transferUtilityRequest.Metadata.Add("originalFileName", trustedFileName);


                await _transferUtility.UploadAsync(transferUtilityRequest);

                // Retrieve Url
                var ImageUrl = GenerateAwsFileUrl(bucketName, trustedStorageName).Data;

                _logger.LogInformation("File uploaded to Amazon S3 bucket successfully");
                return(new AWSUploadResult <string>
                {
                    Status = true,
                    Data = ImageUrl
                });
            }
            catch (Exception ex) when(ex is NullReferenceException)
            {
                _logger.LogError("File data not contained in form", ex);
                throw;
            }
            catch (Exception ex) when(ex is AmazonS3Exception)
            {
                _logger.LogError("Something went wrong during file upload", ex);
                throw;
            }
        }
예제 #4
0
        public async Task <IActionResult> Post([FromForm] UploadRequestDto requestDto)
        {
            var result = await _uploadService.UploadImageToS3BucketAsync(requestDto);

            return(StatusCode(result.StatusCode));
        }