10
10
PURPOSE. See the above copyright notices for more information.
11
11
=========================================================================*/
12
12
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
+
13
22
/**
14
23
* This component is used for large uploads and is used by
15
24
* the web api and the java uploader. It generates an authenticated
@@ -50,14 +59,17 @@ public function generateToken($args, $dirname = '')
50
59
{
51
60
if (!array_key_exists ('filename ' , $ args ))
52
61
{
53
- throw new Exception ('Parameter filename is not defined ' , - 150 );
62
+ throw new Exception ('Parameter filename is not defined ' , MIDAS_HTTPUPLOAD_FILENAME_PARAM_UNDEFINED );
54
63
}
55
64
$ dir = $ dirname == '' ? '' : '/ ' .$ dirname ;
56
65
$ dir = $ this ->tmpDirectory .$ dir ;
57
66
58
67
if (!file_exists ($ dir ))
59
68
{
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
+ }
61
73
}
62
74
// create a unique temporary file in the dirname directory
63
75
$ unique_identifier = basename (tempnam ($ dir , $ args ['filename ' ]));
@@ -68,7 +80,7 @@ public function generateToken($args, $dirname = '')
68
80
69
81
if (empty ($ unique_identifier ))
70
82
{
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 );
72
84
}
73
85
return array ('token ' => $ unique_identifier );
74
86
}
@@ -80,19 +92,19 @@ public function process($args)
80
92
81
93
if (!array_key_exists ('filename ' , $ args ))
82
94
{
83
- throw new Exception ('Parameter filename is not defined ' , - 150 );
95
+ throw new Exception ('Parameter filename is not defined ' , MIDAS_HTTPUPLOAD_PARAM_UNDEFINED );
84
96
}
85
97
$ filename = $ args ['filename ' ];
86
98
87
99
if (!array_key_exists ($ this ->tokenParamName , $ args ))
88
100
{
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 );
90
102
}
91
103
$ uploadToken = $ args [$ this ->tokenParamName ];
92
104
93
105
if (!array_key_exists ('length ' , $ args ))
94
106
{
95
- throw new Exception ('Parameter length is not defined ' , - 150 );
107
+ throw new Exception ('Parameter length is not defined ' , MIDAS_HTTPUPLOAD_PARAM_UNDEFINED );
96
108
}
97
109
$ length = (float )($ args ['length ' ]);
98
110
@@ -105,7 +117,7 @@ public function process($args)
105
117
$ pathTemporaryFilename = $ this ->tmpDirectory .'/ ' .$ uploadToken ;
106
118
if (!file_exists ($ pathTemporaryFilename ))
107
119
{
108
- throw new Exception ('Invalid upload token ' , - 141 );
120
+ throw new Exception ('Invalid upload token ' , MIDAS_HTTPUPLOAD_INVALID_UPLOAD_TOKEN );
109
121
}
110
122
else
111
123
{
@@ -127,14 +139,14 @@ public function process($args)
127
139
$ in = fopen ($ inputfile , 'rb ' ); // Stream (LocalServerFile -> Server) Mode: Read, Binary
128
140
if ($ in === false )
129
141
{
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 );
131
143
}
132
144
133
145
// open target output
134
146
$ out = fopen ($ pathTemporaryFilename , 'ab ' ); // Stream (Server -> TempFile) Mode: Append, Binary
135
147
if ($ out === false )
136
148
{
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 );
138
150
}
139
151
140
152
if ($ streamChecksum )
@@ -163,7 +175,7 @@ public function process($args)
163
175
164
176
if ($ uploadOffset < $ length )
165
177
{
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 );
167
179
}
168
180
169
181
$ data ['filename ' ] = $ filename ;
@@ -180,7 +192,7 @@ public function getOffset($args)
180
192
//check parameters
181
193
if (!array_key_exists ($ this ->tokenParamName , $ args ))
182
194
{
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 );
184
196
}
185
197
$ uploadToken = $ args [$ this ->tokenParamName ];
186
198
$ offset = filesize ($ this ->tmpDirectory .'/ ' .$ uploadToken );
0 commit comments