public async Task <CreateUpdatePackageResponse?> Handle(CreateUpdatePackageRequest message) { var project = await _projectRepository.GetById(message.ProjectId); if (project == null) { return(ReturnError(ResourceNotFoundError.ProjectNotFound(message.ProjectId))); } var package = message.UpdatePackage; var updatePackage = new UpdatePackage(package.Version, message.ProjectId); updatePackage.Description = package.Description; updatePackage.CustomFields = package.CustomFields; if (!CopyChangelogs(updatePackage, package.Changelogs)) { return(null); // error } await _addFilesAction.AddFiles(updatePackage, package.Files, message.AsymmetricKeyPassword); if (InheritError(_addFilesAction)) { return(null); // error } CopyDistributions(updatePackage, package.Distributions); try { await _updatePackageRepository.Add(updatePackage); } catch (Exception) { if (await _updatePackageRepository.GetFirstOrDefaultBySpecs(new ProjectSpec(project.Id), new VersionSpec(updatePackage.VersionInfo.SemVersion)) != null) { SetError(new UpdatePackageAlreadyExistsError()); return(null); } throw; } await _updatePackageRepository.OrderUpdatePackages(message.ProjectId, updatePackage.VersionInfo.SemVersion); return(new CreateUpdatePackageResponse(updatePackage.Id)); }
public async Task AddFiles(UpdatePackage package, IEnumerable <UpdateFileInfo> files, string?keyPassword) { var project = await _projectRepository.GetById(package.ProjectId); if (project == null) { SetError(ResourceNotFoundError.ProjectNotFound(package.ProjectId)); return; } var signatureFactory = CreateFileSignatureFactory(project.AsymmetricKey, keyPassword); if (signatureFactory == null) { return; // error } foreach (var file in files) { if (string.IsNullOrEmpty(file.Path)) { SetError(new FieldValidationError("Files", "The path of a file must not be empty")); return; } var storedFile = await _filesRepository.FindByHash(file.Hash); if (storedFile == null) { SetError(new StoredFileNotFoundError(file.Hash)); return; } var signature = signatureFactory(file); if (signature == null) { return; // error } var updateFile = new UpdateFile(file.Path, file.Hash.ToString(), signature); package.AddFile(updateFile); } }