FOSSology  4.4.0
Open Source License Compliance by Open Source Software
OneShotController.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2024 Divij Sharma <divijs75@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 namespace Fossology\UI\Api\Controllers;
14 
21 use Psr\Http\Message\ServerRequestInterface;
22 
28 {
29  const FILE_INPUT_NAME = 'fileInput';
30 
32  private $highlightProcessor;
33 
35  private $licenseDao;
36 
37  public function __construct($container)
38  {
39  parent::__construct($container);
40  $this->highlightProcessor = $this->container->get('view.highlight_processor');
41  $this->licenseDao = $this->container->get('dao.license');
42  }
43 
53  public function runOneShotNomos($request, $response, $args)
54  {
55  $symReq = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
56  $uploadedFile = $symReq->files->get($this::FILE_INPUT_NAME, null);
57  if (is_null($uploadedFile)) {
58  throw new HttpBadRequestException("No file uploaded");
59  }
60  if ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
61  throw new HttpBadRequestException("Error uploading file");
62  }
63  list($licenses, $highlightInfoKeywords, $highlightInfoLicenses) = $this->restHelper->getPlugin('agent_nomos_once')->
64  AnalyzeFile($uploadedFile->getPathname(), true);
65 
66  $highlights = array();
67 
68  for ($index = 0; $index < count($highlightInfoKeywords['position']); $index ++) {
69  $position = $highlightInfoKeywords['position'][$index];
70  $length = $highlightInfoKeywords['length'][$index];
71 
72  $highlights[] = new Highlight($position, $position + $length,
73  Highlight::KEYWORD);
74  }
75  for ($index = 0; $index < count($highlightInfoLicenses['position']); $index ++) {
76  $position = $highlightInfoLicenses['position'][$index];
77  $length = $highlightInfoLicenses['length'][$index];
78  $name = $highlightInfoLicenses['name'][$index];
79 
80  $highlights[] = new Highlight($position, $position + $length,
81  Highlight::SIGNATURE, $name);
82  }
83  $this->highlightProcessor->sortHighlights($highlights);
84  $returnVal = new OneShot($licenses, $highlights);
85  return $response->withJson($returnVal->getArray(), 200);
86  }
87 
97  public function runOneShotMonk($request, $response, $args)
98  {
99  $symReq = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
100  $uploadedFile = $symReq->files->get($this::FILE_INPUT_NAME, null);
101  if (is_null($uploadedFile)) {
102  throw new HttpBadRequestException("No file uploaded");
103  }
104  if ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
105  throw new HttpBadRequestException("Error uploading file");
106  }
107 
108  list($licenseIds, $highlights) = $this->restHelper->getPlugin('oneshot-monk')->
109  scanMonk($uploadedFile->getPathname());
110  $this->highlightProcessor->addReferenceTexts($highlights);
111  $licenseArray = array_map(function($licenseIds) {
112  return ($this->licenseDao->getLicenseById($licenseIds))
113  ->getShortName();
114  }, $licenseIds);
115  $returnVal = new OneShot($licenseArray, $highlights);
116  return $response->withJson($returnVal->getArray(), 200);
117  }
118 
128  public function runOneShotCEU($request, $response, $args)
129  {
130  $symReq = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
131  $uploadedFile = $symReq->files->get($this::FILE_INPUT_NAME, null);
132  if (is_null($uploadedFile)) {
133  throw new HttpBadRequestException("No file uploaded");
134  }
135  if ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
136  throw new HttpBadRequestException("Error uploading file");
137  }
138  list($copyrights, $highlights) = $this->restHelper->getPlugin('agent_copyright_once')->
139  AnalyzeOne(true, $uploadedFile->getPathname());
140  $this->highlightProcessor->sortHighlights($highlights);
141  $returnVal = new OneShot($copyrights, $highlights);
142  return $response->withJson($returnVal->getArray('copyrights'), 200);
143  }
144 }
Base controller for REST calls.
Override Slim response for withJson function.