public async Task <IActionResult> PostAttachment([FromForm] FileFormData model)
        {
            //Validate the Model
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Boolean isUploaded = false;

            //Check if the file is not empty
            if (model.File.Length > 0)
            {
                Stream         stream      = model.File.OpenReadStream();
                string         contentType = model.File.ContentType;
                BlobProperties properties  = new BlobProperties
                {
                    ContentType = contentType,
                    Security    = BlobSecurity.Public
                };
                string blobContainer = model.ParentItem.ToString();
                string blobName      = Path.GetRandomFileName();
                isUploaded = await _provider.UploadBlobAsync(blobContainer, blobName, stream, properties);

                //store the data
                var attachment = new Attachment()
                {
                    UploadedBy    = model.UploadedBy,
                    ParentItem    = model.ParentItem,
                    FileName      = model.File.FileName,
                    Size          = model.File.Length,
                    BlobContainer = blobContainer,
                    BlobName      = blobName,
                    UploadedOn    = DateTime.UtcNow
                };
                _context.attachments.Add(attachment);


                if (isUploaded)
                {
                    await _context.SaveChangesAsync();

                    return(Created(nameof(AttachmentController), new AttachmentDTO()
                    {
                        Id = attachment.Id,
                        FileName = attachment.FileName,
                        Size = attachment.Size,
                        UploadedBy = attachment.UploadedBy,
                        UploadedOn = attachment.UploadedOn
                    }));
                }
            }

            ModelState.AddModelError("File", $"The request couldn't be processed (Error 20).");
            // Log error
            return(BadRequest(ModelState));
        }
Пример #2
0
        public async Task <object> Post([FromForm] FileFormData formData)
        {
            IFormFile file = formData?.File;

            if (null == file)
            {
                _logger.LogWarning("File is empty");
                return(Problem(
                           type: "err-empty",
                           title: "File is empty",
                           statusCode: (int)HttpStatusCode.BadRequest));
            }
            if (file.Length > maxFileSize)
            {
                _logger.LogWarning($"File too big {file.Length}");
                return(Problem(
                           type: "err-file-too-large",
                           title: $"Request file should be no more than {maxFileSize / 1024 / 1024} Mbytes",
                           statusCode: (int)HttpStatusCode.RequestEntityTooLarge));
            }
            var sw = new Stopwatch();

            sw.Start();
            try
            {
                await using (var fi = file.OpenReadStream())
                {
                    using (var clamClient = clamClientFactory())
                    {
                        await clamClient.InstreamAsync(fi);
                    }
                }
            }
            catch (ClamException e)
            {
                _logger.LogWarning(e.Message);
                return(Problem(
                           type: "err-scan",
                           title: e.Message,
                           statusCode: (int)HttpStatusCode.Forbidden));
            }
            catch (SocketException e)
            {
                _logger.LogError(e, e.Message);
                return(Problem(
                           type: "err-socket",
                           title: e.Message,
                           statusCode: (int)HttpStatusCode.ServiceUnavailable));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(Problem(
                           type: "err-unexpected",
                           title: e.Message,
                           statusCode: (int)HttpStatusCode.InternalServerError));
            }
            finally
            {
                _logger.LogInformation($"Scanned {file.Length} bytes in {sw.ElapsedMilliseconds}mS ({(long)((double)file.Length*1000 / sw.ElapsedMilliseconds)} bps)");
            }
            _logger.LogInformation("Scan pass");
            return(NoContent()); // a good thing
        }