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

Commit d0d7bb1

Browse files
committed
ENH: refs #0306. Clean up Httpupload component, and make the web api use it
1 parent 517662d commit d0d7bb1

File tree

5 files changed

+55
-51
lines changed

5 files changed

+55
-51
lines changed

core/controllers/UploadController.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,16 @@ function processjavauploadAction()
256256
echo '[ERROR]User id does not match upload token user id';
257257
throw new Zend_Exception('User id does not match upload token user id');
258258
}
259-
$expectedParentId = $this->userSession->JavaUpload->parent ?
260-
$this->userSession->JavaUpload->parent :
261-
$this->userSession->Dao->getPrivatefolderId();
259+
260+
if($this->userSession->JavaUpload->parent)
261+
{
262+
$expectedParentId = $this->userSession->JavaUpload->parent;
263+
}
264+
else
265+
{
266+
$expectedParentId = $this->userSession->Dao->getPrivatefolderId();
267+
}
268+
262269
if($parentId != $expectedParentId)
263270
{
264271
echo '[ERROR]You are attempting to upload into the incorrect parent folder';

core/controllers/components/HttpuploadComponent.php

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ class HttpuploadComponent extends AppComponent
1919
{
2020

2121
var $tmpDirectory = '';
22-
var $tokenParamName = '';
22+
var $tokenParamName = 'uploadtoken';
2323
var $testingEnable = false;
2424

25-
var $logFile = '';
26-
var $logType = 0;
27-
2825
/** Set the upload temporary directory */
2926
public function setTmpDirectory($dir)
3027
{
@@ -81,25 +78,20 @@ public function process($args)
8178
{
8279
$uploadOffset = (float)0; // bytes received
8380

84-
//check parameters
8581
if(!array_key_exists('filename', $args))
8682
{
87-
error_log(__FILE__.":".__FUNCTION__.":".__LINE__." - "."Parameter filename is not defined", $this->logType, $this->logFile);
8883
throw new Exception('Parameter filename is not defined', -150);
8984
}
85+
$filename = $args['filename'];
9086

91-
$filename = $args['filename']; // XXXX.ISP
92-
93-
if (!array_key_exists($this->tokenParamName, $args))
87+
if(!array_key_exists($this->tokenParamName, $args))
9488
{
95-
error_log(__FILE__.":".__FUNCTION__.":".__LINE__." - "."Parameter ".$this->tokenParamName." is not defined", $this->logType, $this->logFile);
9689
throw new Exception('Parameter '.$this->tokenParamName.' is not defined', -150);
9790
}
98-
$uploadToken = $args[$this->tokenParamName]; //XXX123.TMP
91+
$uploadToken = $args[$this->tokenParamName];
9992

100-
if (!array_key_exists('length', $args))
93+
if(!array_key_exists('length', $args))
10194
{
102-
error_log(__FILE__.":".__FUNCTION__.":".__LINE__." - "."Parameter length is not defined", $this->logType, $this->logFile);
10395
throw new Exception('Parameter length is not defined', -150);
10496
}
10597
$length = (float)($args['length']);
@@ -113,7 +105,6 @@ public function process($args)
113105
$pathTemporaryFilename = $this->tmpDirectory.'/'.$uploadToken;
114106
if(!file_exists($pathTemporaryFilename))
115107
{
116-
error_log(__FILE__.':'.__FUNCTION__.':'.__LINE__.' - '.'Invalid upload token', $this->logType, $this->logFile);
117108
throw new Exception('Invalid upload token', -141);
118109
}
119110
else
@@ -125,28 +116,26 @@ public function process($args)
125116
$streamChecksum = $uploadOffset == 0;
126117

127118
set_time_limit(0); // Timeout of the PHP script set to Infinite
128-
ignore_user_abort(TRUE);
119+
ignore_user_abort(true);
129120

130121
$inputfile = 'php://input'; // Stream (Client -> Server) Mode: Read, Binary
131-
if ($this->testingEnable && array_key_exists('localinput', $args))
122+
if($this->testingEnable && array_key_exists('localinput', $args))
132123
{
133124
$inputfile = $localinput; // Stream (LocalServerFile -> Server) Mode: Read, Binary
134125
}
135126

136-
$in = fopen($inputfile, 'rb'); // Stream (LocalServerFile -> Server) Mode: Read, Binary
137-
if($in === FALSE )
138-
{
139-
error_log(__FILE__.':'.__FUNCTION__.':'.__LINE__.' - '."Failed to open source:$inputfile", $this->logType, $this->logFile);
140-
throw new Exception("Failed to open [$inputfile] source", -142);
141-
}
127+
$in = fopen($inputfile, 'rb'); // Stream (LocalServerFile -> Server) Mode: Read, Binary
128+
if($in === false)
129+
{
130+
throw new Exception('Failed to open ['.$inputfile.'] source', -142);
131+
}
142132

143133
// open target output
144134
$out = fopen($pathTemporaryFilename, 'ab'); // Stream (Server -> TempFile) Mode: Append, Binary
145-
if ($out === false)
146-
{
147-
error_log(__FILE__.':'.__FUNCTION__.':'.__LINE__.' - '."Failed to open output file:$pathTemporaryFilename", $this->logType, $this->logFile);
148-
throw new Exception("Failed to open output file [$pathTemporaryFilename]", -143);
149-
}
135+
if($out === false)
136+
{
137+
throw new Exception('Failed to open output file ['.$pathTemporaryFilename.']', -143);
138+
}
150139

151140
if($streamChecksum)
152141
{
@@ -174,8 +163,7 @@ public function process($args)
174163

175164
if($uploadOffset < $length)
176165
{
177-
error_log(__FILE__.':'.__FUNCTION__.':'.__LINE__.' - '."Failed to upload file - {$uploadOffset}/{$length} bytes transferred", $this->logType, $this->logFile);
178-
throw new Exception("Failed to upload file - {$uploadOffset}/{$length} bytes transferred", -105);
166+
throw new Exception('Failed to upload file - '.$uploadOffset.'/'.$length.' bytes transferred', -105);
179167
}
180168

181169
$data['filename'] = $filename;

modules/api/controllers/IndexController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Api_IndexController extends Api_AppController
2323

2424
// Config parameters
2525
var $apiEnable = '';
26-
var $apiSetup = array();
26+
var $apiSetup = array();
2727

2828
/** Before filter */
2929
function preDispatch()
@@ -32,9 +32,9 @@ function preDispatch()
3232
$this->apiEnable = true;
3333

3434
// define api parameters
35-
$this->apiSetup['testing'] = Zend_Registry::get('configGlobal')->environment == "testing";
36-
$this->apiSetup['tmp_directory'] = $this->getTempDirectory();
3735
$modulesConfig = Zend_Registry::get('configsModules');
36+
$this->apiSetup['testing'] = Zend_Registry::get('configGlobal')->environment == 'testing';
37+
$this->apiSetup['tmpDirectory'] = $this->getTempDirectory();
3838
$this->apiSetup['apiMethodPrefix'] = $modulesConfig['api']->methodprefix;
3939

4040
$this->action = $actionName = Zend_Controller_Front::getInstance()->getRequest()->getActionName();

modules/api/controllers/components/ApiComponent.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,11 @@ function uploadGeneratetoken($args)
258258
}
259259
}
260260
//we don't already have this content, so create the token
261-
$uploadApi = new KwUploadAPI($this->apiSetup);
262-
return $uploadApi->generateToken($args, $userDao->getKey().'/'.$item->getKey());
261+
$componentLoader = new MIDAS_ComponentLoader();
262+
$uploadComponent = $componentLoader->loadComponent('Httpupload');
263+
$uploadComponent->setTestingMode($this->apiSetup['testing']);
264+
$uploadComponent->setTmpDirectory($this->apiSetup['tmpDirectory']);
265+
return $uploadComponent->generateToken($args, $userDao->getKey().'/'.$item->getKey());
263266
}
264267

265268
/**
@@ -269,8 +272,11 @@ function uploadGeneratetoken($args)
269272
*/
270273
function uploadGetoffset($args)
271274
{
272-
$uploadApi = new KwUploadAPI($this->apiSetup);
273-
return $uploadApi->getOffset($args);
275+
$componentLoader = new MIDAS_ComponentLoader();
276+
$uploadComponent = $componentLoader->loadComponent('Httpupload');
277+
$uploadComponent->setTestingMode($this->apiSetup['testing']);
278+
$uploadComponent->setTmpDirectory($this->apiSetup['tmpDirectory']);
279+
return $uploadComponent->getOffset($args);
274280
}
275281

276282
/**
@@ -380,18 +386,22 @@ function uploadPerform($args)
380386
}
381387

382388
$mode = array_key_exists('mode', $args) ? $args['mode'] : 'stream';
383-
$uploadApi = new KwUploadAPI($this->apiSetup);
389+
390+
$componentLoader = new MIDAS_ComponentLoader();
391+
$httpUploadComponent = $componentLoader->loadComponent('Httpupload');
392+
$httpUploadComponent->setTestingMode($this->apiSetup['testing']);
393+
$httpUploadComponent->setTmpDirectory($this->apiSetup['tmpDirectory']);
384394

385395
if(array_key_exists('testingmode', $args))
386396
{
387-
$uploadApi->testing_enable = true;
388-
$args['localinput'] = $this->apiSetup['tmp_directory'].'/'.$args['filename'];
397+
$httpUploadComponent->setTestingMode(true);
398+
$args['localinput'] = $this->apiSetup['tmpDirectory'].'/'.$args['filename'];
389399
}
390400

391-
// Use KWUploadApi to handle the actual file upload
401+
// Use the Httpupload component to handle the actual file upload
392402
if($mode == 'stream')
393403
{
394-
$result = $uploadApi->process($args);
404+
$result = $httpUploadComponent->process($args);
395405

396406
$filename = $result['filename'];
397407
$filepath = $result['path'];
@@ -416,7 +426,6 @@ function uploadPerform($args)
416426
throw new Exception('Invalid upload mode', MIDAS_INVALID_PARAMETER);
417427
}
418428

419-
$componentLoader = new MIDAS_ComponentLoader();
420429
$uploadComponent = $componentLoader->loadComponent('Upload');
421430
if(isset($folder))
422431
{

modules/api/library/KwUploadAPI.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ function setTempDirectory($tmp_directory)
4747
$this->tmp_directory = $tmp_directory;
4848
}
4949

50-
/**
51-
* Generate an upload token that will act as the authentication token for the upload.
52-
* This token is the filename of a guaranteed unique file which will be placed under the
53-
* directory specified by the dirname parameter, which should be used to ensure that
54-
* the user can only write into a certain logical space.
55-
*/
50+
/**
51+
* Generate an upload token that will act as the authentication token for the upload.
52+
* This token is the filename of a guaranteed unique file which will be placed under the
53+
* directory specified by the dirname parameter, which should be used to ensure that
54+
* the user can only write into a certain logical space.
55+
*/
5656
function generateToken($args, $dirname = '')
5757
{
5858
if(!array_key_exists('filename', $args))

0 commit comments

Comments
 (0)