示例#1
0
        public void FailsIfTotalIsZero()
        {
            DiskUseStats stats = new DiskUseStats();

            Assert.Throws <Exception>(() =>
            {
                stats.ToPercent();
            });
        }
示例#2
0
        public void ToPercent()
        {
            DiskUseStats stats = new DiskUseStats();

            stats.FreeBytes  = 50;
            stats.TotalBytes = 100;
            long percent = stats.ToPercent();

            Assert.Equal(50, percent);
        }
示例#3
0
        public ActionResult SpaceCheck()
        {
            DiskUseStats useStats      = FileHelper.GetDiskUseSats();
            double       freeMegabytes = FileHelper.BytesToMegabytes(useStats.FreeBytes);

            return(new JsonResult(new
            {
                success = new
                {
                    total = $"{FileHelper.BytesToMegabytes(useStats.TotalBytes)}M",
                    available = $"{freeMegabytes}M ({useStats.ToPercent()}%)",
                    safetyExceeded = freeMegabytes < _settings.SpaceSafetyThreshold
                }
            }));
        }
示例#4
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);
            }
        }