Exemple #1
0
        public ActionResult AddPackage([FromForm] PackageCreateFromPost post)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            try
            {
                _log.LogInformation("Package upload request started");

                // check if there is space available
                DiskUseStats useStats = FileHelper.GetDiskUseSats();
                if (useStats.ToPercent() < _settings.SpaceSafetyThreshold)
                {
                    return(Responses.InsufficientSpace("Insufficient space on storage drive."));
                }

                PackageCreateResult result = _packageService.CreatePackage(new PackageCreateArguments
                {
                    Description = post.Description,
                    Files       = post.Files.Select(r => new PackageCreateItem {
                        Content = r.OpenReadStream(),
                        // packages uploaded via webform are forced to include leading folder in path, these can be marked for removal
                        FileName = post.RemoveFirstDirectoryFromPath ? FileHelper.RemoveFirstDirectoryFromPath(r.FileName) : r.FileName
                    }).ToList(),
                    Id        = post.Id,
                    IsArchive = post.IsArchive
                });

                // force flush in-memory list of packages
                if (result.Success)
                {
                    _packageListCache.Clear();
                }

                if (result.Success)
                {
                    // force flush in-memory list of packages
                    _packageListCache.Clear();

                    sw.Stop();

                    return(new JsonResult(new
                    {
                        success = new
                        {
                            id = post.Id,
                            hash = result.PackageHash,
                            description = "Package successfully created",
                            processingTime = sw.Elapsed.TotalSeconds
                        }
                    }));
                }

                if (result.ErrorType == PackageCreateErrorTypes.InvalidArchiveFormat)
                {
                    return(Responses.InvalidArchiveFormatError(post.Format));
                }

                if (result.ErrorType == PackageCreateErrorTypes.InvalidFileCount)
                {
                    return(Responses.InvalidArchiveContent());
                }

                if (result.ErrorType == PackageCreateErrorTypes.PackageExists)
                {
                    return(Responses.PackageExistsError(post.Id));
                }

                if (result.ErrorType == PackageCreateErrorTypes.MissingValue)
                {
                    return(Responses.MissingInputError(result.PublicError));
                }

                return(Responses.UnexpectedError());
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "An unexpected error occurred.");
                return(Responses.UnexpectedError());
            }
            finally
            {
                _log.LogInformation($"Package request processed. {post.Id} took {0} seconds", sw.Elapsed.TotalSeconds);
            }
        }