public function uploadAction() { $request = $this->getRequest(); if (!$request->isPost()) { return; } $name = $this->_getParam('name', false); $upload = new Zend_File_Transfer(); $upload->addValidator('Count', false, 1); if (!$upload->isValid()) { return; } $upload->receive(); $file = $upload->getFileName(); $fp = fopen($file, "r"); if (!$fp) { return; } $mime = $upload->getMimeType(); if (!$name) { // get short name $name = $upload->getFileName(null, false); } $this->_storage->storeItem($name, $fp, array(Zend_Cloud_StorageService_Adapter_S3::METADATA => array("type" => $mime))); try { $this->_storage->storeMetadata($name, array("type" => $mime)); } catch (Zend_Cloud_OperationNotAvailableException $e) { // ignore it } return $this->_helper->redirector('index'); }
/** * 上传用户文件的action */ public function uploadUserDocAction() { $this->_helper->layout->disableLayout(); $doctype = $this->request->getParam('doctype'); $this->view->doctype = $doctype; $result = 0; if ($this->request->isPost()) { $upload = new Zend_File_Transfer(); $upload->addValidator('Size', false, 10240000); //10M $utilService = $this->_container->get('util'); $fileService = $this->_container->get('file'); $filename = $utilService->getFilename($upload->getFileName()); $extension = $fileService->getExtensionByFilename($filename); $destination = $utilService->getTmpDirectory() . DIRECTORY_SEPARATOR . uniqid(); $upload->addFilter('Rename', $destination); if ($upload->isValid()) { if ($upload->receive()) { $userModel = $this->getModel('user'); $mimetype = $upload->getMimeType(); if ($fileService->isAcceptedDocument($mimetype, $extension)) { $user = $userModel->getUserById($this->me->getId()); if ($user) { $doc = null; if ($doctype == \Angel_Model_User::FILETYPE_IDENTITY_FRONT || $doctype == \Angel_Model_User::FILETYPE_IDENTITY_BACK) { $doc = $userModel->addUserDoc($user, $doctype, $destination, $filename, $mimetype); } if ($doc) { $result = 1; $this->view->filename = $doc->filename; $this->view->path = $this->view->url(array('doctype' => $doctype, 'user_id' => $user->id, 'doc_id' => $doc->id), 'user-doc'); } } } else { // 上传的文件格式不接受 $result = 2; } } } } $this->view->result = $result; }
public function filesAction() { if (!$this->getRequest()->isPost()) { throw new AppEx\ForbiddenException("Files action must be a post request."); } $front = Zend_Controller_Front::getInstance(); $front->registerPlugin(new \Tid_Zend_Controller_Plugin_UploadMax()); try { $upload = new Zend_File_Transfer('App_File_Transfer_Adapter_HttpMultipartMixed', false, array('ignoreNoFile' => true)); } catch (Zend_File_Transfer_Exception $e) { throw new AppEx\InvalidArgumentException($e->getMessage()); } $upload->addValidator('Count', true, array('min' => 1, 'max' => 1))->addValidator('Extension', true, array('xml', 'csv', 'txt'))->addValidator('MimeType', true, array('application/xml', 'text/plain', 'headerCheck' => true)); if ($upload->isValid()) { if ($upload->receive()) { try { $fileinfo = current($upload->getFileInfo()); $filename = $fileinfo['tmp_name']; // Attempt to parse data from file $parseResult = $this->_stockSrv->getData($filename, $upload->getMimeType()); $data = $parseResult['data']; $errors = $parseResult['errors']; if (!empty($errors) && is_array($errors)) { foreach ($errors as $errMess) { require_once APPLICATION_PATH . '/modules/default/controllers/ErrorController.php'; $errMess->code = ErrorController::finishErrorCode($errMess->code); } } $method = 'create' . ucfirst($data['_type']); if (!empty($data['_type']) && is_callable(array($this->_stockSrv, $method))) { // Check permissions according to the data type $dumbSim = new Application\Model\SimModel(); $this->_helper->allowed($data['_perm'], $dumbSim); try { $watcher = $this->_stockSrv->{$method}($parseResult); } catch (AppEx\GlobalServiceException $ex) { $ex->addErrorMessages($errors); throw $ex; } $txId = uniqid('parser'); WatcherService::getInstance()->pushEntityId($watcher, $txId); $event = new EventModel(); $event->entityId = $txId; $event->entityType = 'transaction'; $event->namespace = 'connectivity'; $event->eventData = $errors; $event->created = time(); $event->forceFinish = true; WatcherService::getInstance()->publishEvent($event); // WatcherService::getInstance()->setStatus($watcher->id, WatcherModel::STATUS_FINISHED); $errors_ex = $this->_loadErrorsFromWatcher($watcher); if (!empty($errors_ex)) { $errors = Zend_Json::encode($errors_ex); App::log()->warn("Error on file upload in stock:\n" . $errors); throw new AppEx\StockParserException("Some errors uploading file to stock.", array('errorMessages' => $errors_ex)); } } else { throw new AppEx\UnexpectedException('Unknown data type (' . $data['_type'] . ')'); } } catch (PermissionException $e) { throw $e; } catch (StockParserException $e) { throw $e; } catch (GlobalServiceException $e) { $txId = uniqid('parser'); if (!isset($watcher)) { $watcher = $this->_stockSrv->createFileWatcher(); $watcher->entityIds = array($txId); $watcher->params->type = 'sim'; $watcher->params->action = 'stockUpload'; $watcher->save(); } else { WatcherService::getInstance()->pushEntityId($watcher, $txId); } $event = new EventModel(); $event->entityId = $txId; $event->entityType = 'transaction'; $event->namespace = 'connectivity'; $event->created = time(); $event->modified = time(); $event->pushEventData = true; $eventData = array(); $errors = $e->getErrorMessages(); $eventData['hasFailures'] = true; if (!empty($errors) && is_array($errors)) { require_once APPLICATION_PATH . '/modules/default/controllers/ErrorController.php'; foreach ($errors as $errMess) { if ($errMess instanceof ErrorModel) { $errMess->code = ErrorController::finishErrorCode($errMess->code); } } $eventData['message'] = array('failed' => $errors); } $event->eventData = $eventData; $event->forceFinish = true; $compressor = new ErrorModelCompressEvent(); $compressor->compress($event); WatcherService::getInstance()->publishEvent($event); // WatcherService::getInstance()->setStatus($watcher->id, WatcherModel::STATUS_FINISHED); $errors = $this->_loadErrorsFromWatcher($watcher); if (!empty($errors)) { App::log()->warn("Error on file upload in stock:\n" . Zend_Json::encode($errors)); throw new AppEx\StockParserException("Some errors uploading file to stock.", array('errorMessages' => $errors)); } } } else { throw new AppEx\InvalidArgumentException('Could not receive file'); } } else { throw new AppEx\InvalidArgumentException('Invalid file: ' . implode(', ', $upload->getMessages())); } }