Exemplo n.º 1
0
 /**
  * Add a new file.
  *
  */
 public function uploadAction()
 {
     if (!$this->_getParam('updated')) {
         $this->_setParam('updated', date('Y-m-d H:i:s'));
     }
     $model = null;
     $parent = '';
     try {
         $params = $this->filterParams($this->_getAllParams());
         $doUpload = false;
         if (isset($_FILES['file']) && strlen(ifset($_FILES['file'], 'tmp_name', ''))) {
             $doUpload = true;
         }
         $filename = ifset($this->_getAllParams(), 'filename');
         if (!strlen($filename) && $doUpload) {
             $filename = $_FILES['file']['name'];
         }
         $parent = trim(base64_decode($this->_getParam('parent')), " /");
         $id = $this->_getParam('id');
         if ($id) {
             // updating
             $file = $this->fileService->getFile($id);
         } else {
             if ($doUpload == false) {
                 // if we're not doing an upload, but the file doesn't
                 // exist yet, lets bail
                 throw new Exception("You must upload a file");
             }
             $file = $this->fileService->createFile($filename, $parent);
         }
         if ($doUpload) {
             $this->fileService->setFile($file, $_FILES['file']['tmp_name']);
         }
         $params = $this->_getAllParams();
         $params['filename'] = $filename;
         if (!empty($parent)) {
             $params['path'] = $parent;
         }
         $file->bind($params);
         $this->fileService->saveFile($file);
         $this->cacheService->expire($parent);
     } catch (InvalidModelException $ime) {
         $this->flash("Invalid model: " . $ime->getMessages());
         $model = new File();
         $model->bind($this->_getAllParams());
         $this->editAction($model);
         return;
     } catch (Exception $e) {
         $this->flash(get_class($e) . ': ' . $e->getMessage());
         error_log(get_class($e) . ': ' . $e->getMessage());
         error_log($e->getTraceAsString());
         $model = new File();
         $model->bind($this->_getAllParams());
         $this->editAction($model);
         return;
     }
     $picker = $this->_getParam('picker');
     $returnUrl = $this->_getParam('returnurl');
     if (!empty($returnUrl)) {
         $this->redirect(base64_decode($this->_getParam('returnurl')), '', array('picker' => $picker));
     } else {
         $this->redirect('file', 'index', array('picker' => $picker, 'parent' => base64_encode($parent)));
     }
 }