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

Commit be290f5

Browse files
author
Charles Marion
committed
ENH : Added upload test
Translated upload Impprove upload process
1 parent 1de93d0 commit be290f5

26 files changed

+727
-128
lines changed

core/constant/notification.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717

1818
/** visualization*/
1919
define("MIDAS_NOTIFY_CAN_VISUALIZE", 100);
20+
define("MIDAS_NOTIFY_CREATE_THUMBNAIL", 101);
21+
2022
?>

core/controllers/InstallController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function indexAction()
5454
$this->_redirect("/install/step2");
5555
}
5656
} // end method indexAction
57-
57+
5858

5959
/**
6060
* @method step2Action()

core/controllers/UploadController.php

Lines changed: 91 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
/** Upload Controller */
1414
class UploadController extends AppController
1515
{
16-
public $_models = array('User', 'Item', 'ItemRevision', 'Folder', 'Itempolicyuser', "ItemKeyword", 'Itempolicygroup', 'Group', 'Feed', "Feedpolicygroup", "Feedpolicyuser", 'Bitstream', 'Assetstore');
17-
public $_daos = array('User', 'Item', 'ItemRevision', 'Bitstream', 'Folder', "ItemKeyword");
16+
public $_models = array('Assetstore', 'User', 'Item', 'ItemRevision', 'Folder', 'Itempolicyuser', "ItemKeyword", 'Itempolicygroup', 'Group', 'Feed', "Feedpolicygroup", "Feedpolicyuser", 'Bitstream', 'Assetstore');
17+
public $_daos = array('Assetstore', 'User', 'Item', 'ItemRevision', 'Bitstream', 'Folder', "ItemKeyword");
1818
public $_components = array('Httpupload', 'Upload');
1919
public $_forms = array('Upload');
2020

@@ -28,11 +28,32 @@ function init()
2828
$maxPost = str_replace("M", "", ini_get('post_max_size'));
2929
if($maxFile < $maxPost)
3030
{
31-
$this->view->maxSizeFile = $maxFile;
31+
$this->view->maxSizeFile = $maxFile * 1024 * 1024;
3232
}
3333
else
3434
{
35-
$this->view->maxSizeFile = $maxPost;
35+
$this->view->maxSizeFile = $maxPost * 1024 * 1024;
36+
}
37+
38+
if($this->isTestingEnv())
39+
{
40+
$assetstores = $this->Assetstore->getAll();
41+
if(empty($assetstores))
42+
{
43+
$assetstoreDao = new AssetstoreDao();
44+
$assetstoreDao->setName('Default');
45+
$assetstoreDao->setPath(BASE_PATH.'/data/assetstore');
46+
$assetstoreDao->setType(MIDAS_ASSETSTORE_LOCAL);
47+
$this->Assetstore = new AssetstoreModel(); //reset Database adapter
48+
$this->Assetstore->save($assetstoreDao);
49+
}
50+
else
51+
{
52+
$assetstoreDao = $assetstores[0];
53+
}
54+
$config = Zend_Registry::get('configGlobal');
55+
$config->defaultassetstore->id = $assetstoreDao->getKey();
56+
Zend_Registry::set('configGlobal', $config);
3657
}
3758
}
3859

@@ -43,11 +64,11 @@ public function simpleuploadAction()
4364
{
4465
throw new Zend_Exception("You have to be logged in to do that");
4566
}
46-
if(!$this->getRequest()->isXmlHttpRequest())
67+
if(!$this->getRequest()->isXmlHttpRequest()&& !$this->isTestingEnv())
4768
{
4869
throw new Zend_Exception("Error, should be an ajax action.");
4970
}
50-
$this->_helper->layout->disableLayout();
71+
$this->disableLayout();
5172
$this->view->form = $this->getFormAsArray($this->Form->Upload->createUploadLinkForm());
5273
$this->userSession->uploaded = array();
5374
$this->view->selectedLicense = Zend_Registry::get('configGlobal')->defaultlicense;
@@ -65,6 +86,7 @@ public function simpleuploadAction()
6586
$this->view->defaultUploadLocationText = $parent->getName();
6687
}
6788
}
89+
6890
}//end simple upload
6991

7092
/** upload new revision*/
@@ -74,11 +96,11 @@ public function revisionAction()
7496
{
7597
throw new Zend_Exception("You have to be logged in to do that");
7698
}
77-
if(!$this->getRequest()->isXmlHttpRequest())
99+
if(!$this->getRequest()->isXmlHttpRequest() && !$this->isTestingEnv())
78100
{
79101
throw new Zend_Exception("Error, should be an ajax action.");
80102
}
81-
$this->_helper->layout->disableLayout();
103+
$this->disableLayout();
82104
$itemId = $this->_getParam('itemId');
83105
$item = $this->Item->load($itemId);
84106

@@ -103,13 +125,13 @@ public function savelinkAction()
103125
{
104126
throw new Zend_Exception("You have to be logged in to do that");
105127
}
106-
if(!$this->getRequest()->isXmlHttpRequest())
128+
if(!$this->getRequest()->isXmlHttpRequest() && !$this->isTestingEnv())
107129
{
108130
throw new Zend_Exception("Error, should be an ajax action.");
109131
}
110-
111-
$this->_helper->layout->disableLayout();
112-
$this->_helper->viewRenderer->setNoRender();
132+
133+
$this->disableLayout();
134+
$this->disableView();
113135
$parent = $this->_getParam("parent");
114136
$name = $this->_getParam("name");
115137
$url = $this->_getParam("url");
@@ -136,13 +158,25 @@ public function javauploadAction()
136158
$this->_helper->layout->disableLayout();
137159
$this->view->protocol = "http";
138160
$this->view->host = empty($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['HTTP_X_FORWARDED_HOST'];
161+
$this->view->selectedLicense = Zend_Registry::get('configGlobal')->defaultlicense;
162+
163+
$parent = $this->_getParam('parent');
164+
$license = $this->_getParam('license');
165+
if(!empty ($parent) && !empty($license))
166+
{
167+
$this->disableView();
168+
$this->userSession->JavaUpload->parent = $parent;
169+
$this->userSession->JavaUpload->license = $license;
170+
}
139171
}//end java upload
140172

141173

142174
/** used to see how much of a file made it to the server during an
143175
* interrupted upload attempt **/
144176
function gethttpuploadoffsetAction()
145177
{
178+
$this->disableLayout();
179+
$this->disableView();
146180
$params = $this->_getAllParams();
147181
$url = $this->view->url();
148182
$url = substr($url, 0, strrpos($url, '/'));
@@ -153,6 +187,8 @@ function gethttpuploadoffsetAction()
153187
/** java upload function, didn 't check what it does :-) */
154188
function gethttpuploaduniqueidentifierAction()
155189
{
190+
$this->disableLayout();
191+
$this->disableView();
156192
$params = $this->_getAllParams();
157193
$this->Component->Httpupload->get_http_upload_unique_identifier($params);
158194
} //end get_http_upload_unique_identifier
@@ -166,15 +202,31 @@ function processjavauploadAction()
166202
{
167203
throw new Zend_Exception("You have to be logged in to do that");
168204
}
169-
$this->_helper->layout->disableLayout();
170-
$this->_helper->viewRenderer->setNoRender();
205+
$this->disableLayout();
206+
$this->disableView();
171207

172208
$TMP_DIR = BASE_PATH.'/tmp/misc/';
173209
list ($filename, $path, $length) = $this->Component->Httpupload->process_http_upload($params);
174210

175211
if(!empty($path) && file_exists($path) && $length > 0)
176212
{
177-
$item = $this->Component->Upload->createUploadedItem($this->userSession->Dao, $filename, $path);
213+
if(isset($this->userSession->JavaUpload->parent))
214+
{
215+
$parent = $this->userSession->JavaUpload->parent;
216+
}
217+
else
218+
{
219+
$parent = null;
220+
}
221+
if(isset($this->userSession->JavaUpload->license))
222+
{
223+
$license = $this->userSession->JavaUpload->license;
224+
}
225+
else
226+
{
227+
$license = null;
228+
}
229+
$item = $this->Component->Upload->createUploadedItem($this->userSession->Dao, $filename, $path, $parent, $license);
178230
$this->userSession->uploaded[] = $item->getKey();
179231
}
180232
} //end processjavaupload
@@ -187,30 +239,44 @@ public function saveuploadedAction()
187239
{
188240
throw new Zend_Exception("You have to be logged in to do that");
189241
}
190-
$this->_helper->layout->disableLayout();
191-
$this->_helper->viewRenderer->setNoRender();
192-
$upload = new Zend_File_Transfer();
193-
$upload->receive();
194-
$path = $upload->getFileName();
195-
$file_size = filesize($path);
242+
243+
$this->disableLayout();
244+
$this->disableView();
245+
246+
if($this->isTestingEnv())
247+
{
248+
//simulate file upload
249+
$path = BASE_PATH.'/tests/testfiles/search.png';
250+
$file_size = filesize($path);
251+
$filename = 'search.png';
252+
}
253+
else
254+
{
255+
$upload = new Zend_File_Transfer();
256+
$upload->receive();
257+
$path = $upload->getFileName();
258+
$file_size = filesize($path);
259+
$filename = $upload->getFilename(null, false);
260+
}
261+
196262
$parent = $this->_getParam("parent");
197263
$license = $this->_getParam("license");
198-
if(!empty($path) && file_exists($path) && $upload->getFileSize() > 0)
264+
if(!empty($path) && file_exists($path) && $file_size > 0)
199265
{
200266
$tmp = explode('-', $parent);
201267
if(count($tmp) == 2) //means we upload a new revision
202268
{
203269
$changes = $this->_getParam("changes");
204-
$this->Component->Upload->createNewRevision($this->userSession->Dao, $upload->getFilename(null, false), $upload->getFilename(), $tmp, $changes, $license);
270+
$this->Component->Upload->createNewRevision($this->userSession->Dao, $filename, $path, $tmp, $changes, $license);
205271
}
206272
else
207273
{
208-
$item = $this->Component->Upload->createUploadedItem($this->userSession->Dao, $upload->getFilename(null, false), $upload->getFilename(), $parent, $license);
274+
$item = $this->Component->Upload->createUploadedItem($this->userSession->Dao, $filename, $path, $parent, $license);
209275
$this->userSession->uploaded[] = $item->getKey();
210276
}
211277

212278
$info = array();
213-
$info['name'] = basename($upload->getFileName());
279+
$info['name'] = basename($path);
214280
$info['size'] = $file_size;
215281
echo json_encode($info);
216282
}

core/controllers/components/HttpuploadComponent.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,34 @@ function process_http_upload($params)
2222
if(!isset($params['filename']) || empty($params['filename']))
2323
{
2424
$this->getLogger()->crit("[ERROR]'filename' parameter is not set");
25-
exit ("[ERROR]'filename' parameter is not set");
25+
echo "[ERROR]'filename' parameter is not set";
26+
return;
2627
}
2728
$filename = $params['filename'];
2829
$this->getLogger()->info(__METHOD__."filename: ".$filename);
2930

3031
if(!isset($params['length']) || empty($params['length']))
3132
{
3233
$this->getLogger()->crit(__METHOD__."[ERROR]'length' parameter is not set");
33-
exit ("[ERROR]'length' parameter is not set");
34+
echo "[ERROR]'length' parameter is not set";
35+
return;
3436
}
3537
$length = $params['length'];
3638

3739
if(!isset($params['uploadUniqueIdentifier']) || empty($params['uploadUniqueIdentifier']))
3840
{
3941
$this->getLogger()->crit(__METHOD__."[ERROR]'uploadUniqueIdentifier' parameter is not set");
40-
exit ("[ERROR]'uploadUniqueIdentifier' parameter is not set");
42+
echo "[ERROR]'uploadUniqueIdentifier' parameter is not set";
43+
return;
4144
}
4245
$uploadUniqueIdentifier = $params['uploadUniqueIdentifier'];
4346
//check ifthe temporary file exists
4447
$pathTemporaryFilename = BASE_PATH.'/tmp/misc/'.$uploadUniqueIdentifier;
4548
if(!file_exists($pathTemporaryFilename))
4649
{
4750
$this->getLogger()->crit(__METHOD__."[ERROR]'uploadUniqueIdentifier' parameter is incorrect: ".$uploadUniqueIdentifier);
48-
exit ("[ERROR]'uploadUniqueIdentifier' parameter is incorrect: ".$uploadUniqueIdentifier);
51+
echo "[ERROR]'uploadUniqueIdentifier' parameter is incorrect: ".$uploadUniqueIdentifier;
52+
return;
4953
}
5054
else
5155
{
@@ -65,7 +69,8 @@ function process_http_upload($params)
6569
if(!isset($params['path']) || empty($params['path']))
6670
{
6771
$this->getLogger()->crit(__METHOD__."[ERROR]'path' parameter is not set");
68-
exit ("[ERROR]'path' parameter is not set");
72+
echo "[ERROR]'path' parameter is not set";
73+
return;
6974
}
7075
$in = fopen($params['path'], "rb"); // Stream (Applet -> Server) Mode: Read, Binary
7176
}
@@ -102,7 +107,8 @@ function process_http_upload($params)
102107
if($uploadOffset < $length)
103108
{
104109
$this->getLogger()->crit(__METHOD__."[ERROR]Failed to upload file (".($uploadOffset / $length)." bytes)");
105-
exit ("[ERROR]Failed to upload file (".($uploadOffset / $length)." bytes)");
110+
echo "[ERROR]Failed to upload file (".($uploadOffset / $length)." bytes)";
111+
return;
106112
}
107113

108114
return array ($filename, $pathTemporaryFilename, $length);
@@ -115,17 +121,19 @@ function get_http_upload_unique_identifier($params)
115121
if(!isset($params['filename']) || empty($params['filename']))
116122
{
117123
$this->getLogger()->crit(__METHOD__."[ERROR]"."get_http_upload_unique_identifier: 'filename' parameter is missing");
118-
exit ("[ERROR]"."get_http_upload_unique_identifier: 'filename' parameter is missing");
124+
echo "[ERROR]"."get_http_upload_unique_identifier: 'filename' parameter is missing";
125+
return;
119126
}
120127
$filename = $params['filename'];
121128
$unique_identifier = basename(tempnam(BASE_PATH.'/tmp/misc/', $filename));
122129
if(!$unique_identifier)
123130
{
124131
$this->getLogger()->crit(__METHOD__."[ERROR]"."get_http_upload_unique_identifier: Failed to create unique identifier");
125-
exit ("[ERROR]"."get_http_upload_unique_identifier: Failed to create unique identifier");
132+
echo "[ERROR]"."get_http_upload_unique_identifier: Failed to create unique identifier";
133+
return;
126134
}
127135
$this->getLogger()->info(__METHOD__."[OK]".$unique_identifier);
128-
exit ("[OK]".$unique_identifier);
136+
echo "[OK]".$unique_identifier;
129137
}
130138

131139
/** used to see how much of a file made it to the server during an
@@ -135,10 +143,11 @@ function get_http_upload_offset($params)
135143
//check parameters
136144
if(!isset($params['uploadUniqueIdentifier']) || empty($params['uploadUniqueIdentifier']))
137145
{
138-
exit ("[ERROR]'uploadUniqueIdentifier' parameter is not set");
146+
echo "[ERROR]'uploadUniqueIdentifier' parameter is not set";
147+
return;
139148
}
140149
$uploadUniqueIdentifier = $params['uploadUniqueIdentifier'];
141150

142-
exit (sprintf("[OK]%u", filesize(BASE_PATH.'/tmp/misc/'."$uploadUniqueIdentifier")));
151+
echo sprintf("[OK]%u", filesize(BASE_PATH.'/tmp/misc/'."$uploadUniqueIdentifier"));
143152
}
144153
}

core/controllers/components/NotifyErrorComponent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ public function warningError($errno, $errstr, $errfile, $errline)
142142
/** Page url*/
143143
public function curPageURL()
144144
{
145+
if(Zend_Registry::get('configGlobal')->environment == 'testing')
146+
{
147+
return 'http://localhost';
148+
}
145149
$pageURL = 'http';
146150
if(isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
147151
{

0 commit comments

Comments
 (0)