8 namespace Fossology\UI\Page;
14 use Symfony\Component\HttpFoundation\File\UploadedFile;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
23 const NAME =
"admin_license_from_yaml";
24 const KEY_UPLOAD_MAX_FILESIZE =
'upload_max_filesize';
25 const FILE_INPUT_NAME =
'file_input';
26 const FILE_INPUT_NAME_V2 =
'fileInput';
27 function __construct()
29 parent::__construct(self::NAME, array(
30 self::TITLE =>
"Admin License Rules Import",
31 self::MENU_LIST =>
"Admin::License Admin::Rules Import",
32 self::REQUIRES_LOGIN =>
true,
41 if ($apiVersion == ApiVersion::V2) {
42 return $this::FILE_INPUT_NAME_V2;
44 return $this::FILE_INPUT_NAME;
52 protected function handle (Request $request)
55 if ($request->isMethod(
'POST')) {
56 $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
57 $vars[
'message'] = $this->handleFileUpload($uploadFile);
59 $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
60 $vars[
'baseUrl'] = $request->getBaseUrl();
61 return $this->
render(
"admin_license_from_yaml.html.twig", $this->mergeWithDefault($vars));
70 public function handleFileUpload($uploadedFile, $fromRest =
false)
73 if (! ($uploadedFile instanceof UploadedFile)) {
74 $errMsg = _(
"No file selected");
75 } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
76 $errMsg = $uploadedFile->getErrorMessage();
77 } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
78 $errMsg = _(
"Larger than upload_max_filesize ") .
79 ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
80 } elseif ($uploadedFile->getClientOriginalExtension() !=
'yaml' &&
81 $uploadedFile->getClientOriginalExtension() !=
'yml') {
82 $errMsg = _(
'Invalid extension ') .
83 $uploadedFile->getClientOriginalExtension() .
' of file ' .
84 $uploadedFile->getClientOriginalName();
86 if (! empty($errMsg)) {
88 return array(
false, $errMsg, 400);
93 $licenseYamlImport = $this->
getObject(
'app.license_yaml_import');
94 $msg = $licenseYamlImport->handleFile($uploadedFile->getRealPath());
96 return array(
true, $msg, 200);
102 register_plugin(
new AdminLicenseFromYAML());
Import licenses from YAML.
Contains the constants and helpers for authentication of user.
render($templateName, $vars=null, $headers=null)
Upload a file from the users computer using the UI.
getFileInputName($apiVersion=ApiVersion::V1)