Skip to content

Commit

Permalink
feat(restapi): Post upload and get folders path
Browse files Browse the repository at this point in the history
Added new paths to post a new file using uploads
and get folders.

Signed-off-by: Gaurav Mishra <gmishx@gmail.com>
  • Loading branch information
GMishx committed Feb 19, 2019
1 parent 0f5991a commit 62b25d0
Show file tree
Hide file tree
Showing 12 changed files with 558 additions and 37 deletions.
92 changes: 92 additions & 0 deletions src/www/ui/api/Controllers/FolderController.php
@@ -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);
}
}
9 changes: 1 addition & 8 deletions src/www/ui/api/Controllers/JobController.php
Expand Up @@ -51,7 +51,7 @@ public function getJobs($request, $response, $args)
$limit = 0;
if ($request->hasHeader('limit')) {
$limit = $request->getHeaderLine('limit');
if (isset($limit) && (! is_int($limit) || $limit < 0)) {
if (isset($limit) && (! is_numeric($limit) || $limit < 0)) {
$returnVal = new Info(400,
"Limit cannot be smaller than 1 and has to be numeric!",
InfoType::ERROR);
Expand Down Expand Up @@ -84,13 +84,6 @@ public function getJobs($request, $response, $args)
*/
public function createJob($request, $response, $args)
{
// Initialize plugins
require_once dirname(dirname(dirname(dirname(__DIR__)))) .
"/lib/php/common-plugin.php";
plugin_load();
plugin_preinstall();
plugin_postinstall();

$folder = $request->getHeaderLine("folderId");
$upload = $request->getHeaderLine("uploadId");
if (is_numeric($folder) && is_numeric($upload) && $folder > 0 && $upload > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/www/ui/api/Controllers/SearchController.php
Expand Up @@ -74,8 +74,8 @@ public function performSearch($request, $response, $args)
/*
* check if filesizeMin && filesizeMax are numeric, if existing
*/
if ((! empty($filesizeMin) && (! is_int($filesizeMin) || $filesizeMin < 0)) ||
(! empty($filesizeMax) && (! is_int($filesizeMax) || $filesizeMax < 0))) {
if ((! empty($filesizeMin) && (! is_numeric($filesizeMin) || $filesizeMin < 0)) ||
(! empty($filesizeMax) && (! is_numeric($filesizeMax) || $filesizeMax < 0))) {
$returnVal = new Info(400,
"Bad Request. filesizemin and filesizemax need to be positive integers!",
InfoType::ERROR);
Expand Down
47 changes: 46 additions & 1 deletion src/www/ui/api/Controllers/UploadController.php
Expand Up @@ -29,6 +29,7 @@
use const Fossology\UI\Api\AUTH_METHOD;
use Fossology\UI\Api\Models\Info;
use Fossology\UI\Api\Models\InfoType;
use Fossology\UI\Api\Helper\UploadHelper;

/**
* @class UploadController
Expand Down Expand Up @@ -132,7 +133,7 @@ private function changeUpload($request, $response, $args, $isCopy)
{
$returnVal = null;
if ($request->hasHeader('folderId') &&
is_int($newFolderID = $request->getHeaderLine('folderId'))) {
is_numeric($newFolderID = $request->getHeaderLine('folderId'))) {
$id = intval($args['id']);
$returnVal = $this->restHelper->copyUpload($id, $newFolderID, $isCopy);
} else {
Expand All @@ -141,4 +142,48 @@ private function changeUpload($request, $response, $args, $isCopy)
}
return $response->withJson($returnVal->getArray(), $returnVal->getCode());
}

/**
* Get a new upload from the POST method
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param array $args
* @return ResponseInterface
*/
public function postUpload($request, $response, $args)
{
$uploadHelper = new UploadHelper();
if ($request->hasHeader('folderId') &&
is_numeric($folderId = $request->getHeaderLine('folderId')) && $folderId > 0) {

$allFolderIds = $this->container->get('dao.folder')->getAllFolderIds();
if(!in_array($folderId, $allFolderIds)) {
$error = new Info(404, "folderId $folderId does not exists!", InfoType::ERROR);
return $response->withJson($error->getArray(), $error->getCode());
}
if(!$this->container->get('dao.folder')->isFolderAccessible($folderId)) {
$error = new Info(403, "folderId $folderId is not accessible!", InfoType::ERROR);
return $response->withJson($error->getArray(), $error->getCode());
}

$description = $request->getHeaderLine('uploadDescription');
$public = $request->getHeaderLine('public');
$public = empty($public) ? 'protected' : $public;
list ($status, $message, $statusDescription, $uploadId) = $uploadHelper->createNewUpload(
$request, $folderId, $description, $public);
if (! $status) {
$info = new Info(500, [
$message,
$statusDescription
], InfoType::ERROR);
} else {
$info = new Info(201, intval($uploadId), InfoType::INFO);
}
return $response->withJson($info->getArray(), $info->getCode());
} else {
$error = new Info(400, "folderId must be a positive integer!", InfoType::ERROR);
return $response->withJson($error->getArray(), $error->getCode());
}
}
}
8 changes: 6 additions & 2 deletions src/www/ui/api/Helper/AuthHelper.php
Expand Up @@ -53,8 +53,12 @@ class AuthHelper
public function __construct()
{
$this->userDao = $GLOBALS["container"]->get('dao.user');
$this->session = new Session();
$this->session->save();
$this->session = $GLOBALS["container"]->get('session');
if (!$this->session->isStarted())
{
$this->session->setName('Login');
$this->session->start();
}
}

/**
Expand Down
71 changes: 71 additions & 0 deletions src/www/ui/api/Helper/UploadHelper.php
@@ -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);
}
}
51 changes: 51 additions & 0 deletions src/www/ui/api/Middlewares/PluginLoaderHelper.php
@@ -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;
}
}
Expand Up @@ -17,14 +17,17 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***************************************************************/
/**
* @dir
* @brief Middlewares for the Slim framework
* @file
* @brief Auth middleware for Slim
*/

namespace Fossology\UI\Api\Helper;
namespace Fossology\UI\Api\Middlewares;

use Fossology\UI\Api\Models\Info;
use Fossology\UI\Api\Models\InfoType;
use Fossology\UI\Api\Helper\AuthHelper;

/**
* @class RestAuthHelper
Expand Down

0 comments on commit 62b25d0

Please sign in to comment.