Beispiel #1
0
 /**
  * Upload a doc and save meta
  *
  * @TODO not completed
  *
  * @param array  $params
  * @param string $method
  *
  * @return int doc id
  */
 public function upload(array $params, $method = 'POST')
 {
     @ignore_user_abort(true);
     @set_time_limit(0);
     $options = Pi::service('media')->getOption('local', 'options');
     $rootUri = $options['root_uri'];
     $rootPath = $options['root_path'];
     $path = $options['locator']['path'];
     if ($path instanceof Closure) {
         $relativePath = $path();
     } else {
         $relativePath = $path;
     }
     $destination = $rootPath . '/' . $relativePath;
     Pi::service('file')->mkdir($destination);
     $rename = $options['locator']['file'];
     $success = false;
     switch (strtoupper($method)) {
         // For remote post
         case 'POST':
             $uploader = new Upload(array('destination' => $destination, 'rename' => $rename($params['filename'])));
             $maxSize = Pi::config('max_size', $this->module);
             if ($maxSize) {
                 $uploader->setSize($maxSize * 1024);
             }
             $result = $uploader->isValid();
             if ($result) {
                 $uploader->receive();
                 $filename = $uploader->getUploaded();
                 if (is_array($filename)) {
                     $filename = current($filename);
                 }
                 // Fetch file attributes
                 $fileinfoList = $uploader->getFileInfo();
                 $fileinfo = current($fileinfoList);
                 if (!isset($params['mimetype'])) {
                     $params['mimetype'] = $fileinfo['type'];
                 }
                 if (!isset($params['size'])) {
                     $params['size'] = $fileinfo['size'];
                 }
                 $success = true;
             }
             break;
             // For remote put
         // For remote put
         case 'PUT':
             $putdata = fopen('php://input', 'r');
             $filename = $rename($params['filename']);
             $target = $destination . '/' . $filename;
             $fp = fopen($target, 'w');
             while ($data = fread($putdata, 1024)) {
                 fwrite($fp, $data);
             }
             fclose($fp);
             fclose($putdata);
             $success = true;
             break;
             // For local
         // For local
         case 'MOVE':
             $filename = $rename($params['filename']);
             $target = $destination . '/' . $filename;
             Pi::service('file')->copy($params['file'], $target);
             unset($params['file']);
             $success = true;
             break;
         default:
             break;
     }
     if ($success) {
         $params['url'] = $rootUri . '/' . $relativePath . '/' . $filename;
         $params['path'] = $rootPath . '/' . $relativePath . '/' . $filename;
         $result = $this->add($params);
     } else {
         $result = 0;
     }
     return $result;
 }