9 namespace Fossology\UI\Page;
17 use GuzzleHttp\Client;
18 use GuzzleHttp\Exception\GuzzleException;
19 use GuzzleHttp\Exception\RequestException;
20 use Symfony\Component\HttpFoundation\File\UploadedFile;
21 use Symfony\Component\HttpFoundation\Request;
22 use Symfony\Component\HttpFoundation\Response;
29 const NAME =
"admin_license_from_csv";
30 const KEY_UPLOAD_MAX_FILESIZE =
'upload_max_filesize';
31 const FILE_INPUT_NAME =
'file_input';
32 const FILE_INPUT_NAME_V2 =
'fileInput';
37 private $guzzleClient;
43 function __construct()
46 parent::__construct(self::NAME, array(
47 self::TITLE =>
"Admin License Import",
48 self::MENU_LIST =>
"Admin::License Admin::License Import",
49 self::REQUIRES_LOGIN =>
true,
53 $this->licenseCsvImport = $GLOBALS[
'container']->get(
'app.license_csv_import');
55 $this->configuration = [
70 if ($apiVersion == ApiVersion::V2) {
71 return $this::FILE_INPUT_NAME_V2;
73 return $this::FILE_INPUT_NAME;
81 protected function handle(Request $request)
84 if (!$request->isMethod(
'POST')) {
85 $getHealth = $this->configuration[
'url'] . $this->configuration[
'uri'] .
"/health";
88 if ($request->isMethod(
'POST')) {
89 if ($request->get(
'importFrom') ===
'licensedb') {
90 $startTime = microtime(
true);
92 $fetchLicenseTime = microtime(
true) - $startTime;
93 $this->fileLogger->debug(
"Fetching License and Check if exist took: " . sprintf(
"%0.3fms", 1000 * $fetchLicenseTime));
94 $this->fileLogger->debug(
"****************** Message From LicenseDB import [" . date(
'Y-m-d H:i:s') .
"] ******************");
95 $this->fileLogger->debug($vars[
"message"]);
96 $this->fileLogger->debug(
"****************** End Message From LicenseDB import ******************");
98 $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
99 $delimiter = $request->get(
'delimiter') ?:
',';
100 $enclosure = $request->get(
'enclosure') ?:
'"';
105 $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
106 $vars[
'baseUrl'] = $request->getBaseUrl();
107 $vars[
'license_csv_import'] =
true;
109 if (!empty(
trim($this->configuration[
'url']))) {
110 $vars[
'baseURL'] = !empty($this->configuration[
'uri']);
111 $vars[
'authToken'] = !empty($this->configuration[
'token']);
112 $vars[
'exportEndpoint'] = !empty($this->configuration[
'content']);
113 return $this->
render(
"admin_license_from_licensedb.html.twig", $this->mergeWithDefault($vars));
115 return $this->
render(
"admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
130 $finalURL = $this->configuration[
'url'] . $this->configuration[
'uri'] . $this->configuration[
'content'];
132 $startTimeReq = microtime(
true);
133 $response = $this->guzzleClient->get($finalURL);
134 $fetchLicenseTimeReq = microtime(
true) - $startTimeReq;
135 $this->fileLogger->debug(
"LicenseDB req:' took " . sprintf(
"%0.3fms", 1000 * $fetchLicenseTimeReq));
138 return $this->licenseCsvImport->importJsonData($data, $msg);
140 return $msg . $e->getMessage();
141 }
catch (RequestException|GuzzleException $e) {
142 return $msg . _(
'Something Went Wrong, check if host is accessible') .
': ' . $e->getMessage();
153 if (!($uploadedFile instanceof UploadedFile)) {
154 $errMsg = _(
"No file selected");
155 } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
156 $errMsg = $uploadedFile->getErrorMessage();
157 } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
158 $errMsg = _(
"Larger than upload_max_filesize ") .
159 ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
160 } elseif ($uploadedFile->getClientOriginalExtension() !=
'csv'
161 && $uploadedFile->getClientOriginalExtension() !=
'json') {
162 $errMsg = _(
'Invalid file extension ') .
163 $uploadedFile->getClientOriginalExtension() .
' of file ' .
164 $uploadedFile->getClientOriginalName();
166 if (!empty($errMsg)) {
167 return array(
false, $errMsg, 400);
169 $this->licenseCsvImport->setDelimiter($delimiter);
170 $this->licenseCsvImport->setEnclosure($enclosure);
172 return array(
true, $this->licenseCsvImport->handleFile($uploadedFile->getRealPath(), $uploadedFile->getClientOriginalExtension()), 200);
176 register_plugin(
new AdminLicenseFromCSV());
Import licenses from CSV.
Contains the constants and helpers for authentication of user.
Exception when Guzzle client response is not as expected.
render($templateName, $vars=null, $headers=null)
static processHttpResponse($response)
static getGuzzleClient(array $SysConf, string $baseUri, string $token="")
static checkLicenseDBHealth(string $getHealth, $guzzleClient)
Upload a file from the users computer using the UI.
getFileInputName($apiVersion=ApiVersion::V1)
handleFileUpload($uploadedFile, $delimiter=',', $enclosure='"')
char * trim(char *ptext)
Trimming whitespace.