Example #1
0
 public function indexAction()
 {
     $request = $this->getRequest();
     if (!$request->isPost()) {
         $this->getHelper('Redirector')->goto('index', 'index');
         // action, controller
     }
     $c = Zend_Registry::get('config');
     $apiKey = Zend_Filter::get($request->getPost('apikey'), 'Alnum');
     try {
         $user = User::findByApiKey($apiKey);
         if ($user === null) {
             $this->view->assign('response', 'Invalid API key.');
         } else {
             $user->setIp($request->getServer('REMOTE_ADDR'));
         }
     } catch (Zend_Db_Adapter_Exception $e) {
         $this->view->assign('response', $e->getMessage());
     }
     if (isset($user)) {
         if ($_FILES['file']['error'] === 0) {
             $file = new File();
             $file->setFileName($_FILES['file']['name']);
             $file->setFileSize($_FILES['file']['size']);
             $file->setTmpName($_FILES['file']['tmp_name']);
             $file->setUploadedBy($user);
             try {
                 $url = $file->save();
                 $this->view->assign('response', $url . "\n");
             } catch (Exception $e) {
                 $this->view->assign('response', $e->getMessage());
             }
         } else {
             switch ($_FILES['file']['error']) {
                 case UPLOAD_ERR_OK:
                     break;
                 case UPLOAD_ERR_INI_SIZE:
                     throw new Exception('The uploaded file exceeds the upload_max_filesize directive (' . ini_get('upload_max_filesize') . ') in php.ini.');
                     break;
                 case UPLOAD_ERR_FORM_SIZE:
                     throw new Exception('The uploaded file exceeds the MAX_FILE_SIZE directive' . 'that was specified in the HTML form.');
                     break;
                 case UPLOAD_ERR_PARTIAL:
                     throw new Exception('The uploaded file was only partially uploaded.');
                     break;
                 case UPLOAD_ERR_NO_FILE:
                     throw new Exception('No file was uploaded.');
                     break;
                 case UPLOAD_ERR_NO_TMP_DIR:
                     throw new Exception('Missing a temporary folder.');
                     break;
                 case UPLOAD_ERR_CANT_WRITE:
                     throw new Exception('Failed to write file to disk.');
                     break;
                 default:
                     throw new Exception('Unknown File Error.');
             }
         }
     }
 }
Example #2
0
 /**
  * Deletes a file.
  *
  * Called when this component receives an HTTP DELETE request to
  * /zip/$a/$b/$c/$file.
  *
  * @param string $hash The hash of the file which should be deleted.
  */
 public function deleteZip($callName, $input, $params = array())
 {
     $path = array(self::getBaseDir(), $params['a'], $params['b'], $params['c'], $params['file']);
     $filePath = implode('/', array_slice($path, 0));
     if (strlen($filePath) > 0 && file_exists($this->config['DIR']['files'] . '/' . $filePath)) {
         // after the successful deletion, we want to return the file data
         $file = new File();
         $file->setAddress($filePath);
         $file->setFileSize(filesize($this->config['DIR']['files'] . '/' . $filePath));
         $file->setHash(sha1_file($this->config['DIR']['files'] . '/' . $filePath));
         $file->setMimeType("application/zip");
         // removes the file
         unlink($this->config['DIR']['files'] . '/' . $filePath);
         // the removing/unlink process failed, if the file still exists.
         if (file_exists($this->config['DIR']['files'] . '/' . $filePath)) {
             return Model::isProblem(new File());
         }
         // the file is removed
         return Model::isCreated($file);
     } else {
         // file does not exist
         return Model::isProblem(new File());
     }
 }