Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit fe41983

Browse files
author
Jamie Snape
committed
Add an API call for creating an item containing a link bitstream
1 parent 2d7324d commit fe41983

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

core/controllers/api/SystemController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function postAction()
7575
'default' => 'adminDatabaseCleanup',
7676
'databasecleanup' => 'adminDatabaseCleanup',
7777
'defaultapikey' => 'userApikeyDefault',
78+
'createlink' => 'linkCreate',
7879
'upload' => 'uploadPerform'
7980
);
8081
$this->_genericAction($this->_request->getParams(), $this->_request->getControllerName(), 'post', $apiFunctions);

core/controllers/components/ApisystemComponent.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,63 @@ function userApikeyDefault($args)
196196
}
197197
}
198198

199+
/**
200+
* Create a link bitstream. POST is required.
201+
* @path /system/createlink
202+
* @http POST
203+
* @param useSession (Optional) Authenticate using the current Midas session
204+
* @param token (Optional) Authentication token
205+
* @param folderid
206+
The id of the folder in which to create a new item that will
207+
contain the link. The new item will have the same name as
208+
<b>url</b> unless <b>itemname</b> is supplied.
209+
* @param url The URL of the link you will create, will be used as the name
210+
of the bitstream and of the item (unless <b>itemname</b> is
211+
supplied).
212+
* @param itemname (Optional)
213+
The name of the newly created item, if not supplied, the item will
214+
have the same name as <b>url</b>.
215+
* @return The item information of the item created.
216+
*/
217+
function linkCreate($args)
218+
{
219+
$request = Zend_Controller_Front::getInstance()->getRequest();
220+
if(!$request->isPost())
221+
{
222+
throw new Exception('POST method required', MIDAS_HTTP_ERROR);
223+
}
224+
$apihelperComponent = MidasLoader::loadComponent('Apihelper');
225+
$apihelperComponent->validateParams($args, array('folderid', 'url'));
226+
$apihelperComponent->requirePolicyScopes(array(MIDAS_API_PERMISSION_SCOPE_WRITE_DATA));
227+
$userDao = $apihelperComponent->getUser($args);
228+
if(!$userDao)
229+
{
230+
throw new Exception('Anonymous users may not create a link', MIDAS_INVALID_POLICY);
231+
}
232+
$folderModel = MidasLoader::loadModel('Folder');
233+
$folder = $folderModel->load($args['folderid']);
234+
if($folder === false)
235+
{
236+
throw new Exception('Folder corresponding to folderid does not exist', MIDAS_INVALID_PARAMETER);
237+
}
238+
if(!$folderModel->policyCheck($folder, $userDao, MIDAS_POLICY_WRITE))
239+
{
240+
throw new Exception('Invalid policy or folderid', MIDAS_INVALID_POLICY);
241+
}
242+
if(!filter_var($args['url'], FILTER_VALIDATE_URL))
243+
{
244+
throw new Exception('Invalid URL', MIDAS_INVALID_PARAMETER);
245+
}
246+
$itemname = isset($args['itemname']) ? $args['itemname'] : $args['url'];
247+
$uploadComponent = MidasLoader::loadComponent('Upload');
248+
$item = $uploadComponent->createLinkItem($userDao, $itemname, $args['url'], $folder);
249+
if(!$item)
250+
{
251+
throw new Exception('Link creation failed', MIDAS_INTERNAL_ERROR);
252+
}
253+
return $item->toArray();
254+
}
255+
199256
/**
200257
* Generate a unique upload token. Either <b>itemid</b> or <b>folderid</b> is required,
201258
but both are not allowed.

0 commit comments

Comments
 (0)