From 94d874dcba883b12d914fd209eeb18e067fb4a32 Mon Sep 17 00:00:00 2001 From: dushimsam Date: Wed, 2 Aug 2023 12:42:19 +0200 Subject: [PATCH 1/2] feat(api): The REST API to export licenses-list as CSV Signed-off-by: dushimsam --- .../ui/api/Controllers/LicenseController.php | 31 +++++++++++++++++++ src/www/ui/api/documentation/openapi.yaml | 24 ++++++++++++++ src/www/ui/api/index.php | 1 + 3 files changed, 56 insertions(+) diff --git a/src/www/ui/api/Controllers/LicenseController.php b/src/www/ui/api/Controllers/LicenseController.php index 4c56169f6f..9539baefa9 100644 --- a/src/www/ui/api/Controllers/LicenseController.php +++ b/src/www/ui/api/Controllers/LicenseController.php @@ -27,6 +27,7 @@ use Fossology\UI\Page\AdminLicenseCandidate; use Psr\Container\ContainerInterface; use Psr\Http\Message\ServerRequestInterface as Request; +use Slim\Psr7\Factory\StreamFactory; /** * @class LicenseController @@ -878,4 +879,34 @@ public function getSuggestedLicense($request, $response, $args) } return $response->withJson($suggestLicense, 200); } + + /** + * Export licenses to CSV file + * + * @param Request $request + * @param ResponseHelper $response + * @param array $args + * @return ResponseHelper + */ + public function exportAdminLicenseToCSV($request, $response, $args) + { + if (!Auth::isAdmin()) { + $error = new Info(403, "You are not allowed to access the endpoint.", InfoType::ERROR); + return $response->withJson($error->getArray(), $error->getCode()); + } + $dbManager = $this->dbHelper->getDbManager(); + $licenseCsvExport = new \Fossology\Lib\Application\LicenseCsvExport($dbManager); + $content = $licenseCsvExport->createCsv(0); + $fileName = "fossology-license-export-" . date("YMj-Gis"); + $newResponse = $response->withHeader('Content-type', 'text/csv, charset=UTF-8') + ->withHeader('Content-Disposition', 'attachment; filename=' . $fileName . '.csv') + ->withHeader('Pragma', 'no-cache') + ->withHeader('Cache-Control', 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0') + ->withHeader('Expires', 'Expires: Thu, 19 Nov 1981 08:52:00 GMT'); + $sf = new StreamFactory(); + $newResponse = $newResponse->withBody( + $content ? $sf->createStream($content) : $sf->createStream('') + ); + return ($newResponse); + } } diff --git a/src/www/ui/api/documentation/openapi.yaml b/src/www/ui/api/documentation/openapi.yaml index 34ccedb0b7..dcb673250e 100644 --- a/src/www/ui/api/documentation/openapi.yaml +++ b/src/www/ui/api/documentation/openapi.yaml @@ -4029,6 +4029,30 @@ paths: $ref: '#/components/schemas/Info' default: $ref: '#/components/responses/defaultResponse' + /license/export-csv: + get: + operationId: exportLicense + tags: + - License + summary: Export a csv license + description: > + Export a csv license + responses: + '200': + description: Successfully exported + content: + text/plain: + schema: + type: string + format: binary + '403': + description: Route is not accessible + content: + application/json: + schema: + $ref: '#/components/schemas/Info' + default: + $ref: '#/components/responses/defaultResponse' /license/{shortname}: parameters: - name: shortname diff --git a/src/www/ui/api/index.php b/src/www/ui/api/index.php index e62046b062..031f71a96e 100644 --- a/src/www/ui/api/index.php +++ b/src/www/ui/api/index.php @@ -328,6 +328,7 @@ function (\Slim\Routing\RouteCollectorProxy $app) { function (\Slim\Routing\RouteCollectorProxy $app) { $app->get('', LicenseController::class . ':getAllLicenses'); $app->post('/import-csv', LicenseController::class . ':handleImportLicense'); + $app->get('/export-csv', LicenseController::class . ':exportAdminLicenseToCSV'); $app->post('', LicenseController::class . ':createLicense'); $app->put('/verify/{shortname:.+}', LicenseController::class . ':verifyLicense'); $app->put('/merge/{shortname:.+}', LicenseController::class . ':mergeLicense'); From 1630e79d012b3528430660eafec9adfd870b072d Mon Sep 17 00:00:00 2001 From: Gaurav Mishra Date: Tue, 3 Oct 2023 13:48:12 +0530 Subject: [PATCH 2/2] feat(api): export single license as CSV Signed-off-by: Gaurav Mishra --- .../ui/api/Controllers/LicenseController.php | 19 +++++++++++++++---- src/www/ui/api/documentation/openapi.yaml | 10 +++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/www/ui/api/Controllers/LicenseController.php b/src/www/ui/api/Controllers/LicenseController.php index 9539baefa9..d8e876a641 100644 --- a/src/www/ui/api/Controllers/LicenseController.php +++ b/src/www/ui/api/Controllers/LicenseController.php @@ -12,6 +12,7 @@ namespace Fossology\UI\Api\Controllers; +use Fossology\Lib\Application\LicenseCsvExport; use Fossology\Lib\Auth\Auth; use Fossology\Lib\Dao\LicenseAcknowledgementDao; use Fossology\Lib\Dao\LicenseDao; @@ -894,9 +895,20 @@ public function exportAdminLicenseToCSV($request, $response, $args) $error = new Info(403, "You are not allowed to access the endpoint.", InfoType::ERROR); return $response->withJson($error->getArray(), $error->getCode()); } + $query = $request->getQueryParams(); + $rf = 0; + if (array_key_exists('licenseId', $query)) { + $rf = intval($query['licenseId']); + } + if ($rf != 0 && + (! $this->dbHelper->doesIdExist("license_ref", "rf_pk", $rf) && + ! $this->dbHelper->doesIdExist("license_candidate", "rf_pk", $rf))) { + $error = new Info(404, "License not found.", InfoType::ERROR); + return $response->withJson($error->getArray(), $error->getCode()); + } $dbManager = $this->dbHelper->getDbManager(); - $licenseCsvExport = new \Fossology\Lib\Application\LicenseCsvExport($dbManager); - $content = $licenseCsvExport->createCsv(0); + $licenseCsvExport = new LicenseCsvExport($dbManager); + $content = $licenseCsvExport->createCsv($rf); $fileName = "fossology-license-export-" . date("YMj-Gis"); $newResponse = $response->withHeader('Content-type', 'text/csv, charset=UTF-8') ->withHeader('Content-Disposition', 'attachment; filename=' . $fileName . '.csv') @@ -904,9 +916,8 @@ public function exportAdminLicenseToCSV($request, $response, $args) ->withHeader('Cache-Control', 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0') ->withHeader('Expires', 'Expires: Thu, 19 Nov 1981 08:52:00 GMT'); $sf = new StreamFactory(); - $newResponse = $newResponse->withBody( + return $newResponse->withBody( $content ? $sf->createStream($content) : $sf->createStream('') ); - return ($newResponse); } } diff --git a/src/www/ui/api/documentation/openapi.yaml b/src/www/ui/api/documentation/openapi.yaml index dcb673250e..aac477487b 100644 --- a/src/www/ui/api/documentation/openapi.yaml +++ b/src/www/ui/api/documentation/openapi.yaml @@ -4032,6 +4032,14 @@ paths: /license/export-csv: get: operationId: exportLicense + parameters: + - name: licenseId + description: License id to export, 0 for all + in: query + required: false + default: 0 + schema: + type: int tags: - License summary: Export a csv license @@ -4041,7 +4049,7 @@ paths: '200': description: Successfully exported content: - text/plain: + text/csv: schema: type: string format: binary