Пример #1
0
        public ActionResult AddTag(string tag, string packageId)
        {
            try
            {
                tag = HttpUtility.UrlDecode(tag);
                _tagsService.AddTag(packageId, tag);

                return(new JsonResult(new
                {
                    success = new
                    {
                        description = $"Tag {tag} was added to package {packageId}"
                    }
                }));
            }
            catch (PackageNotFoundException)
            {
                return(Responses.NotFoundError(this, $"Package ${packageId} not found."));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "An unexpected error occurred.");
                return(Responses.UnexpectedError());
            }
        }
Пример #2
0
        public ActionResult GetItem(string fileId)
        {
            try
            {
                GetFileResponse payload = _indexService.GetFile(fileId);
                if (payload == null)
                {
                    return(Responses.NotFoundError(this, $"file {fileId} not found. Id is invalid, or has been deleted."));
                }

                if (payload.Content == null)
                {
                    throw new Exception($"File {fileId} has no content.");
                }

                return(File(payload.Content, "application/octet-stream", payload.FileName));
            }
            catch (InvalidFileIdentifierException)
            {
                return(Responses.InvalidFileId());
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "An unexpected error occurred.");
                return(Responses.UnexpectedError());
            }
        }
Пример #3
0
        public ActionResult GetArchive(string packageId)
        {
            Stream archiveStream = null;

            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                _indexService.PurgeOldArchives();
                archiveStream = _indexService.GetPackageAsArchive(packageId);

                sw.Stop();
                _log.LogInformation($"Archive generation for package {packageId} took {0} seconds", sw.Elapsed.TotalSeconds);

                return(File(archiveStream, "application/octet-stream", $"{packageId}.zip"));
            }
            catch (PackageNotFoundException)
            {
                return(Responses.NotFoundError(this, $"Package ${packageId} not found."));
            }
            catch (Exception ex)
            {
                if (archiveStream != null)
                {
                    archiveStream.Close();
                }

                _log.LogError(ex, "Unexpected error");
                return(Responses.UnexpectedError());
            }
        }
Пример #4
0
        public ActionResult DeletePackage(string packageId)
        {
            try
            {
                _indexService.DeletePackage(packageId);
                _packageListCache.Clear();

                return(new JsonResult(new
                {
                    success = new
                    {
                        description = "Package deleted"
                    }
                }));
            }
            catch (PackageNotFoundException)
            {
                return(Responses.NotFoundError(this, $"Package "));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "An unexpected error occurred.");
                return(Responses.UnexpectedError());
            }
        }
Пример #5
0
 public ActionResult GetTags()
 {
     try
     {
         return(new JsonResult(new
         {
             success = new
             {
                 tags = _tagsService.GetAllTags().ToArray()
             }
         }));
     }
     catch (Exception ex)
     {
         _log.LogError(ex, "An unexpected error occurred.");
         return(Responses.UnexpectedError());
     }
 }
Пример #6
0
        public ActionResult GetTagPackages(string tags)
        {
            try
            {
                string[] tagsSplit = tags.Split(",", StringSplitOptions.RemoveEmptyEntries);

                return(new JsonResult(new
                {
                    success = new
                    {
                        packages = _tagsService.GetPackageIdsWithTags(tagsSplit).ToArray()
                    }
                }));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "An unexpected error occurred.");
                return(Responses.UnexpectedError());
            }
        }
Пример #7
0
        public ActionResult GetLatestPackageWithTag(string tags)
        {
            try
            {
                string[] tagsSplit = tags.Split(",", StringSplitOptions.RemoveEmptyEntries);
                Package  package   = _packageList.GetLatestWithTags(tagsSplit);

                return(new JsonResult(new
                {
                    success = new
                    {
                        package = package
                    }
                }));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "An unexpected error occurred.");
                return(Responses.UnexpectedError());
            }
        }
Пример #8
0
        public ActionResult VerifyPackage(string packageId)
        {
            try
            {
                (bool, string)result = _indexService.VerifyPackage(packageId);

                return(new JsonResult(new
                {
                    success = new
                    {
                        isValid = result.Item1,
                        description = result.Item2
                    }
                }));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "An unexpected error occurred.");
                return(Responses.UnexpectedError());
            }
        }
Пример #9
0
        public ActionResult GetArchiveStatus(string packageId)
        {
            try
            {
                _indexService.PurgeOldArchives();

                return(new JsonResult(new
                {
                    success = new
                    {
                        status = _indexService.GetPackageArchiveStatus(packageId)
                    }
                }));
            }
            catch (PackageNotFoundException)
            {
                return(Responses.NotFoundError(this, $"Package ${packageId} not found."));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Unexpected error");
                return(Responses.UnexpectedError());
            }
        }
Пример #10
0
        public ActionResult GetPackage(string packageId)
        {
            try
            {
                Manifest manifest = _indexService.GetManifest(packageId);
                if (manifest == null)
                {
                    return(Responses.NotFoundError(this, $"Package ${packageId} does not exist"));
                }

                return(new JsonResult(new
                {
                    success = new
                    {
                        package = manifest
                    }
                }));
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "An unexpected error occurred.");
                return(Responses.UnexpectedError());
            }
        }
Пример #11
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);
            }
        }