Example #1
0
 public void Add(File item)
 {
     item.Id = Guid.NewGuid().ToString();
     _Files[item.Id] = item;
 }
Example #2
0
 public void Update(File item)
 {
     _Files[item.Id] = item;
 }
Example #3
0
        public async Task<IActionResult> Post()
        {
            var form = await Request.ReadFormAsync();
            log.Information("Request with {@filesAttached} file(s)", form.Files.Count);
            Response.StatusCode = 201;

            var files = new FilesData();
            foreach (var formFile in form.Files)
            {
                var file = new File();
                try
                {
                    file.Id = Guid.NewGuid().ToString();
                    file.Path = Path.Combine(fileUploadPath, file.Id);

                    Directory.CreateDirectory(Path.GetPathRoot(file.Path));
                    var taskSave = formFile.SaveAsAsync(file.Path);

                    file.Name = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition).FileName.Trim('"');
                    file.Size = formFile.Length;
                    file.Uploader = User.Identity.Name;
                    file.DateUploaded = DateTime.Now;
                    Db.Files.Add(file);

                    await taskSave;
                    Db.SaveChanges();
                    files.files.Add(file.ToSerializeable());
                }
                catch (Exception ex)
                {
                    var error = new UploadFailed
                    {
                        name = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition).FileName.Trim('"'),
                        size = formFile.Length,
                        error = ex.ToString()
                    };
                    log.Warning("Processing error: {@Exception}", ex);
                    files.files.Add(error);
                    Response.StatusCode = 500;
                }
                finally
                {
                    if (file.Size != new FileInfo(file.Path).Length)
                    {
                        log.Error(
                            "File is a different size than advertised! {sizeAdvertised} != {Actualsize}",
                            file.Size,
                            new FileInfo(file.Path).Length);
                    }

                    log.Information(
                        "File '{name}' with a size of {size} processed",
                        file.Name,
                        Format.Bytes(file.Size));
                }
            }

            log.Information("{FileCount} file(s) processed", files.files.Count);
            log.Verbose("Response {@fileData}", files.files);
            return Json(files);
        }