Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(restapi): Post upload and get folders path
Added new paths to post a new file using uploads and get folders. Signed-off-by: Gaurav Mishra <gmishx@gmail.com>
- Loading branch information
Showing
12 changed files
with
558 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/*************************************************************** | ||
Copyright (C) 2018 Siemens AG | ||
Author: Gaurav Mishra <mishra.gaurav@siemens.com> | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
version 2 as published by the Free Software Foundation. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
***************************************************************/ | ||
/** | ||
* @file | ||
* @brief Controller for folder queries | ||
*/ | ||
|
||
namespace Fossology\UI\Api\Controllers; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Fossology\UI\Api\Models\Info; | ||
use Fossology\UI\Api\Models\InfoType; | ||
use Fossology\UI\Api\Models\Analysis; | ||
use Fossology\UI\Api\Models\Decider; | ||
use Fossology\UI\Api\Models\Reuser; | ||
use Fossology\UI\Api\Models\ScanOptions; | ||
use Fossology\Lib\Auth\Auth; | ||
use Fossology\UI\Api\Models\Folder; | ||
|
||
/** | ||
* @class FolderController | ||
* @brief Controller for Folder model | ||
*/ | ||
class FolderController extends RestController | ||
{ | ||
|
||
/** | ||
* Get all folders accessible by the user | ||
* | ||
* @param ServerRequestInterface $request | ||
* @param ResponseInterface $response | ||
* @param array $args | ||
* @return ResponseInterface | ||
*/ | ||
public function getFolders($request, $response, $args) | ||
{ | ||
$id = null; | ||
$allUserFolders = null; | ||
|
||
$folderDao = $this->container->get('dao.folder'); | ||
if (isset($args['id'])) { | ||
$id = intval($args['id']); | ||
$returnVal = null; | ||
if (! $folderDao->isFolderAccessible($id)) { | ||
$returnVal = new Info(403, "Folder id $id is not accessible", | ||
InfoType::ERROR); | ||
} | ||
if (! $this->dbHelper->doesIdExist("folder", "folder_pk", $id)) { | ||
$returnVal = new Info(404, "Folder id $id does not exists", | ||
InfoType::ERROR); | ||
} | ||
if ($returnVal !== null) { | ||
return $response->withJson($returnVal->getArray(), $returnVal->getCode()); | ||
} | ||
$allUserFolders = [ | ||
$id | ||
]; | ||
} else { | ||
$rootFolder = $folderDao->getRootFolder(Auth::getUserId())->getId(); | ||
$allUserFolders = array(); | ||
GetFolderArray($rootFolder, $allUserFolders); | ||
$allUserFolders = array_keys($allUserFolders); | ||
} | ||
$foldersList = array(); | ||
foreach ($allUserFolders as $folderId) { | ||
$folder = $folderDao->getFolder($folderId); | ||
$folderModel = new Folder($folder->getId(), $folder->getName(), $folder->getDescription()); | ||
$foldersList[] = $folderModel->getArray(); | ||
} | ||
if ($id !== null) { | ||
$foldersList = $foldersList[0]; | ||
} | ||
return $response->withJson($foldersList, 200); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/*************************************************************** | ||
Copyright (C) 2018 Siemens AG | ||
Author: Gaurav Mishra <mishra.gaurav@siemens.com> | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
version 2 as published by the Free Software Foundation. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
***************************************************************/ | ||
/** | ||
* @file | ||
* @brief Helper to handle file uploads | ||
*/ | ||
|
||
namespace Fossology\UI\Api\Helper; | ||
|
||
use Fossology\UI\Page\UploadFilePage; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Slim\Http\UploadedFile; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
|
||
/** | ||
* @class UploadHelper | ||
* @brief Handle new file uploads from Slim framework and move to FOSSology | ||
*/ | ||
class UploadHelper extends UploadFilePage | ||
{ | ||
/** | ||
* Get a request from Slim and translate to Symfony request to be | ||
* processed by FOSSology | ||
* | ||
* @param ServerRequestInterface $request | ||
* @param string $folderName | ||
* @param string $fileDescription | ||
* @param string $isPublic | ||
* @return boolean[]|string[]|unknown[]|NULL[]|mixed[] | ||
*/ | ||
public function createNewUpload(ServerRequestInterface $request, $folderName, | ||
$fileDescription, $isPublic) | ||
{ | ||
$uploadedFile = $request->getUploadedFiles()[self::FILE_INPUT_NAME]; | ||
$path = $uploadedFile->file; | ||
$originalName = $uploadedFile->getClientFilename(); | ||
$originalMime = $uploadedFile->getClientMediaType(); | ||
$originalError = $uploadedFile->getError(); | ||
$symfonyFile = new \Symfony\Component\HttpFoundation\File\UploadedFile($path, | ||
$originalName, $originalMime, $originalError); | ||
$symfonyRequest = new \Symfony\Component\HttpFoundation\Request(); | ||
$symfonySession = new Session(); | ||
$symfonySession->set(self::UPLOAD_FORM_BUILD_PARAMETER_NAME, "restUpload"); | ||
|
||
$symfonyRequest->request->set(self::FOLDER_PARAMETER_NAME, $folderName); | ||
$symfonyRequest->request->set(self::DESCRIPTION_INPUT_NAME, $fileDescription); | ||
$symfonyRequest->files->set(self::FILE_INPUT_NAME, $symfonyFile); | ||
$symfonyRequest->setSession($symfonySession); | ||
$symfonyRequest->request->set(self::UPLOAD_FORM_BUILD_PARAMETER_NAME, | ||
"restUpload"); | ||
$symfonyRequest->request->set('public', $isPublic); | ||
|
||
return $this->handleUpload($symfonyRequest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/*************************************************************** | ||
Copyright (C) 2018 Siemens AG | ||
Author: Gaurav Mishra <mishra.gaurav@siemens.com> | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
version 2 as published by the Free Software Foundation. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
***************************************************************/ | ||
/** | ||
* @file | ||
* @brief Middleware to load and unload plugins | ||
*/ | ||
|
||
namespace Fossology\UI\Api\Middlewares; | ||
|
||
require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))) . | ||
"/lib/php/common-plugin.php"; | ||
|
||
class PluginLoaderHelper | ||
{ | ||
/** | ||
* Load all the plugins before the call and unload them after the call | ||
* | ||
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request | ||
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response | ||
* @param callable $next Next middleware | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function __invoke($request, $response, $next) | ||
{ | ||
plugin_load(); | ||
plugin_preinstall(); | ||
plugin_postinstall(); | ||
|
||
$response = $next($request, $response); | ||
|
||
plugin_unload(); | ||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.