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

Commit 358ae08

Browse files
author
Michael Grauer
committed
BUG: refs #68. Changed error codes to constants, added mkdir error handling.
1 parent 8f01c13 commit 358ae08

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

core/controllers/components/HttpuploadComponent.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
PURPOSE. See the above copyright notices for more information.
1111
=========================================================================*/
1212

13+
// HTTPUPLOAD error codes
14+
define('MIDAS_HTTPUPLOAD_UPLOAD_FAILED', -105);
15+
define('MIDAS_HTTPUPLOAD_UPLOAD_TOKEN_GENERATION_FAILED', -140);
16+
define('MIDAS_HTTPUPLOAD_INVALID_UPLOAD_TOKEN', -141);
17+
define('MIDAS_HTTPUPLOAD_INPUT_FILE_OPEN_FAILED', -142);
18+
define('MIDAS_HTTPUPLOAD_OUTPUT_FILE_OPEN_FAILED', -143);
19+
define('MIDAS_HTTPUPLOAD_TMP_DIR_CREATION_FAILED', -144);
20+
define('MIDAS_HTTPUPLOAD_PARAM_UNDEFINED', -150);
21+
1322
/**
1423
* This component is used for large uploads and is used by
1524
* the web api and the java uploader. It generates an authenticated
@@ -50,14 +59,17 @@ public function generateToken($args, $dirname = '')
5059
{
5160
if(!array_key_exists('filename', $args))
5261
{
53-
throw new Exception('Parameter filename is not defined', -150);
62+
throw new Exception('Parameter filename is not defined', MIDAS_HTTPUPLOAD_FILENAME_PARAM_UNDEFINED);
5463
}
5564
$dir = $dirname == '' ? '' : '/'.$dirname;
5665
$dir = $this->tmpDirectory.$dir;
5766

5867
if(!file_exists($dir))
5968
{
60-
mkdir($dir, 0700, true);
69+
if(!mkdir($dir, 0700, true))
70+
{
71+
throw new Exception('Failed to create temporary upload dir', MIDAS_HTTPUPLOAD_TMP_DIR_CREATION_FAILED);
72+
}
6173
}
6274
// create a unique temporary file in the dirname directory
6375
$unique_identifier = basename(tempnam($dir, $args['filename']));
@@ -68,7 +80,7 @@ public function generateToken($args, $dirname = '')
6880

6981
if(empty($unique_identifier))
7082
{
71-
throw new Exception('Failed to generate upload token', -140);
83+
throw new Exception('Failed to generate upload token', MIDAS_HTTPUPLOAD_UPLOAD_TOKEN_GENERATION_FAILED);
7284
}
7385
return array('token' => $unique_identifier);
7486
}
@@ -80,19 +92,19 @@ public function process($args)
8092

8193
if(!array_key_exists('filename', $args))
8294
{
83-
throw new Exception('Parameter filename is not defined', -150);
95+
throw new Exception('Parameter filename is not defined', MIDAS_HTTPUPLOAD_PARAM_UNDEFINED);
8496
}
8597
$filename = $args['filename'];
8698

8799
if(!array_key_exists($this->tokenParamName, $args))
88100
{
89-
throw new Exception('Parameter '.$this->tokenParamName.' is not defined', -150);
101+
throw new Exception('Parameter '.$this->tokenParamName.' is not defined', MIDAS_HTTPUPLOAD_PARAM_UNDEFINED);
90102
}
91103
$uploadToken = $args[$this->tokenParamName];
92104

93105
if(!array_key_exists('length', $args))
94106
{
95-
throw new Exception('Parameter length is not defined', -150);
107+
throw new Exception('Parameter length is not defined', MIDAS_HTTPUPLOAD_PARAM_UNDEFINED);
96108
}
97109
$length = (float)($args['length']);
98110

@@ -105,7 +117,7 @@ public function process($args)
105117
$pathTemporaryFilename = $this->tmpDirectory.'/'.$uploadToken;
106118
if(!file_exists($pathTemporaryFilename))
107119
{
108-
throw new Exception('Invalid upload token', -141);
120+
throw new Exception('Invalid upload token', MIDAS_HTTPUPLOAD_INVALID_UPLOAD_TOKEN);
109121
}
110122
else
111123
{
@@ -127,14 +139,14 @@ public function process($args)
127139
$in = fopen($inputfile, 'rb'); // Stream (LocalServerFile -> Server) Mode: Read, Binary
128140
if($in === false)
129141
{
130-
throw new Exception('Failed to open ['.$inputfile.'] source', -142);
142+
throw new Exception('Failed to open ['.$inputfile.'] source', MIDAS_HTTPUPLOAD_INPUT_FILE_OPEN_FAILED);
131143
}
132144

133145
// open target output
134146
$out = fopen($pathTemporaryFilename, 'ab'); // Stream (Server -> TempFile) Mode: Append, Binary
135147
if($out === false)
136148
{
137-
throw new Exception('Failed to open output file ['.$pathTemporaryFilename.']', -143);
149+
throw new Exception('Failed to open output file ['.$pathTemporaryFilename.']', MIDAS_HTTPUPLOAD_OUTPUT_FILE_OPEN_FAILED);
138150
}
139151

140152
if($streamChecksum)
@@ -163,7 +175,7 @@ public function process($args)
163175

164176
if($uploadOffset < $length)
165177
{
166-
throw new Exception('Failed to upload file - '.$uploadOffset.'/'.$length.' bytes transferred', -105);
178+
throw new Exception('Failed to upload file - '.$uploadOffset.'/'.$length.' bytes transferred', MIDAS_HTTPUPLOAD_UPLOAD_FAILED);
167179
}
168180

169181
$data['filename'] = $filename;
@@ -180,7 +192,7 @@ public function getOffset($args)
180192
//check parameters
181193
if(!array_key_exists($this->tokenParamName, $args))
182194
{
183-
throw new Exception('Parameter '.$this->tokenParamName.' is not defined', -150);
195+
throw new Exception('Parameter '.$this->tokenParamName.' is not defined', MIDAS_HTTPUPLOAD_PARAM_UNDEFINED);
184196
}
185197
$uploadToken = $args[$this->tokenParamName];
186198
$offset = filesize($this->tmpDirectory.'/'.$uploadToken);

0 commit comments

Comments
 (0)