Example #1
0
 /**
  * Store an upload from a Request object (supports chunked upload)
  * @param  RequestInterface $request the request to process
  * @param  string           $key      optional upload key, defaults to `"file"`
  * @param  string|null      $name     an optional name to store under (defaults to the upload name or the post field)
  * @param  mixed            $settings optional data to save along with the file
  * @return array                     an array consisting of the ID, name, path, hash and size of the copied file
  */
 public function fromRequest(RequestInterface $request, $key = 'file', $name = null, $settings = null)
 {
     if (!$request->hasUpload($key)) {
         throw new FileException('No valid input files', 400);
     }
     $upload = $request->getUpload($key);
     $name = $name ?: ($upload->getName() !== 'blob' ? $upload->getName() : $request->getPost("name", "blob"));
     $size = $request->getPost("size", 0);
     $chunk = $request->getPost('chunk', 0, 'int');
     $chunks = $request->getPost('chunks', 0, 'int');
     $done = $chunks <= 1 || $chunk >= $chunks - 1;
     $temp = $this->prefix . md5(implode('.', [$name, $chunks, $size, session_id(), isset($_SERVER) && isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '']));
     if ($chunk === 0 && is_file($this->baseDirectory . $temp)) {
         throw new FileException('The same file is already being uploaded', 400);
     }
     if ($chunk > 0 && !is_file($this->baseDirectory . $temp)) {
         throw new FileException('No previous file parts', 400);
     }
     if (!is_dir($this->baseDirectory . $this->prefix) && !mkdir($this->baseDirectory . $this->prefix, 0755, true)) {
         throw new FileException('Could not create upload directory');
     }
     if ($chunk === 0) {
         $upload->saveAs($this->baseDirectory . $temp);
     } else {
         $upload->appendTo($this->baseDirectory . $temp);
     }
     if ($done) {
         $data = $this->fromFile($this->baseDirectory . $temp, $name, $settings);
         unlink($this->baseDirectory . $temp);
         return $data;
     }
     return ['id' => $temp, 'name' => $name, 'path' => $this->baseDirectory . $temp, 'complete' => false, 'hash' => '', 'size' => 0];
 }