Ejemplo n.º 1
0
 public function it_can_handle_exception_during_request(RequestInterface $request, UploadedFileInterface $file)
 {
     $path = '/path/To/a';
     $file->getClientFilename()->willReturn($name = 'file.ext');
     $file->getClientMediaType()->willReturn($mime = 'text/xml');
     $file->getStream()->willReturn($stream = tmpfile());
     $files = [$file];
     $request->offsetGet('path')->willReturn($path);
     $request->offsetGet('files')->willReturn($files);
     $request->getAcceptContentType()->willReturn('application/json');
     $this->fileRepository->fromInput($name, $path, $mime, $stream)->willThrow(new \RuntimeException());
     $this->shouldThrow(ResponseException::class)->duringExecuteRequest($request);
 }
Ejemplo n.º 2
0
 /**
  * @param \Psr\Http\Message\UploadedFileInterface $file
  * @return string
  */
 public function uploadFile(UploadedFileInterface $file)
 {
     if ($file->getError() === UploadedFile::OK) {
         $fileName = pathinfo($file->getClientFilename());
         $dir = date("ymd");
         if (!file_exists("{$this->basePath}/{$dir}")) {
             if (false === @mkdir("{$this->basePath}/{$dir}", 0777, true)) {
                 throw new RuntimeException("fail to create directory.");
             }
         }
         do {
             $newFileName = sha1($fileName['filename'] . rand());
             $newFilePath = "{$this->basePath}/{$dir}/{$newFileName}.{$fileName['extension']}";
         } while (file_exists($newFilePath));
         $file->moveTo($newFilePath);
         return "{$dir}/{$newFileName}.{$fileName['extension']}";
     }
 }
Ejemplo n.º 3
0
 /**
  * Creates Symfony UploadedFile instance from PSR-7 ones.
  *
  * @param UploadedFileInterface $psrUploadedFile
  *
  * @return UploadedFile
  */
 private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
 {
     $temporaryPath = $this->getTemporaryPath();
     $psrUploadedFile->moveTo($temporaryPath);
     $clientFileName = $psrUploadedFile->getClientFilename();
     return new UploadedFile($temporaryPath, null === $clientFileName ? '' : $clientFileName, $psrUploadedFile->getClientMediaType(), $psrUploadedFile->getSize(), $psrUploadedFile->getError(), true);
 }
Ejemplo n.º 4
0
 /**
  * Checks if an error during upload occured
  *
  * @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file
  * @throws \Aimeos\Controller\Common\Exception If an error occured during upload
  */
 protected function checkFileUpload(\Psr\Http\Message\UploadedFileInterface $file)
 {
     if ($file->getError() !== UPLOAD_ERR_OK) {
         switch ($file->getError()) {
             case UPLOAD_ERR_INI_SIZE:
             case UPLOAD_ERR_FORM_SIZE:
                 throw new \Aimeos\Controller\Common\Exception('The uploaded file exceeds the max. allowed filesize');
             case UPLOAD_ERR_PARTIAL:
                 throw new \Aimeos\Controller\Common\Exception('The uploaded file was only partially uploaded');
             case UPLOAD_ERR_NO_FILE:
                 throw new \Aimeos\Controller\Common\Exception('No file was uploaded');
             case UPLOAD_ERR_NO_TMP_DIR:
                 throw new \Aimeos\Controller\Common\Exception('Temporary folder is missing');
             case UPLOAD_ERR_CANT_WRITE:
                 throw new \Aimeos\Controller\Common\Exception('Failed to write file to disk');
             case UPLOAD_ERR_EXTENSION:
                 throw new \Aimeos\Controller\Common\Exception('File upload stopped by extension');
             default:
                 throw new \Aimeos\Controller\Common\Exception('Unknown upload error');
         }
     }
 }
 /**
  * Handle the case where a file has been uploaded for a field, possibly replacing another already set on the field.
  *
  * @param Model                 $model
  * @param string                $fieldName
  * @param UploadedFileInterface $file
  */
 private function newUpload(Model $model, $fieldName, UploadedFileInterface $file)
 {
     $filename = $file->getClientFilename();
     $ext = strtolower(str_segmentsLast($filename, '.'));
     $name = str_segmentsStripLast($filename, '.');
     $id = uniqid();
     $mime = FileUtil::getUploadedFileMimeType($file);
     $isImage = FileUtil::isImageType($mime);
     $fileModel = $model->files()->create(['id' => $id, 'name' => $name, 'ext' => $ext, 'mime' => $mime, 'image' => $isImage, 'group' => str_segmentsLast($fieldName, '.')]);
     // Save the uploaded file.
     $path = "{$this->fileArchivePath}/{$fileModel->path}";
     $dir = dirname($path);
     if (!file_exists($dir)) {
         mkdir($dir, 0777, true);
     }
     $file->moveTo($path);
     // Delete the previous file for this field, if one exists.
     $prevFilePath = $model->getOriginal($fieldName);
     if (exists($prevFilePath)) {
         $this->deleteFile($prevFilePath);
     }
     $model->{$fieldName} = $fileModel->path;
     $model->save();
 }
Ejemplo n.º 6
0
 /**
  * Upload data to tracker
  *
  * @param string|StreamInterface|UploadedFileInterface $file
  * @param string $key
  * @param string $class
  * @param bool $use_file
  *
  * @return bool
  * @throws Exception
  */
 public function put($file, $key, $class, $use_file = true)
 {
     if ($key === null) {
         throw new InvalidArgumentException(get_class($this) . "::put key cannot be null");
     }
     if ($use_file) {
         if ($file instanceof UploadedFileInterface) {
             $fh = $file->getStream()->detach();
             $length = $file->getSize();
         } elseif ($file instanceof StreamInterface) {
             $fh = $file->detach();
             $length = $file->getSize();
         } elseif (is_resource($file) && get_resource_type($file) == 'stream') {
             $fh = $file;
         } else {
             $fh = fopen($file, 'r');
         }
         if (!$fh) {
             throw new RuntimeException(get_class($this) . "::put failed to open file");
         }
         if (!$length) {
             $length = filesize($file);
         }
     } else {
         $fh = fopen('php://memory', 'rw');
         if ($fh === false) {
             throw new RuntimeException(get_class($this) . "::put failed to open memory stream");
         }
         fwrite($fh, $file);
         rewind($fh);
         $length = strlen($file);
     }
     //CREATE_OPEN domain=%s&key=%s&class=%s&multi_dest=%d
     $location = $this->doRequest('CREATE_OPEN', ['domain' => $this->domain, 'key' => $key, 'class' => $class]);
     $uri = $location['path'];
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_INFILE, $fh);
     curl_setopt($ch, CURLOPT_INFILESIZE, $length);
     curl_setopt($ch, CURLOPT_TIMEOUT, $this->request_timeout);
     curl_setopt($ch, CURLOPT_PUT, $this->request_timeout);
     curl_setopt($ch, CURLOPT_URL, $uri);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, ['Expect: ']);
     $response = curl_exec($ch);
     fclose($fh);
     if ($response === false) {
         $error = curl_error($ch);
         curl_close($ch);
         throw new RuntimeException(get_class($this) . "::put {$error}");
     }
     curl_close($ch);
     $this->doRequest('CREATE_CLOSE', ['key' => $key, 'class' => $class, 'domain' => $this->domain, 'devid' => $location['devid'], 'fid' => $location['fid'], 'path' => urldecode($uri)]);
     return true;
 }
Ejemplo n.º 7
0
 /**
  * Stores the uploaded file in the "fs-secure" file system
  *
  * @param \Psr\Http\Message\UploadedFileInterface $file
  * @return string Path to the uploaded file
  */
 protected function storeFile(\Psr\Http\Message\UploadedFileInterface $file)
 {
     $ext = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
     $hash = md5($file->getClientFilename() . microtime(true));
     $path = sprintf('%s/%s/%s.%s', $hash[0], $hash[1], $hash, $ext);
     $fs = $this->getContext()->getFilesystemManager()->get('fs-secure');
     if (!$fs->isdir($hash[0] . '/' . $hash[1])) {
         $fs->mkdir($hash[0] . '/' . $hash[1]);
     }
     $fs->writes($path, $file->getStream()->detach());
     return $path;
 }
 /**
  * Convert a single file back into an array.
  *
  * @param \Psr\Http\Message\UploadedFileInterface $file The file to convert.
  * @return array
  */
 protected static function convertFile($file)
 {
     return ['name' => $file->getClientFilename(), 'type' => $file->getClientMediaType(), 'tmp_name' => $file->getStream()->getMetadata('uri'), 'error' => $file->getError(), 'size' => $file->getSize()];
 }
Ejemplo n.º 9
0
 /**
  * Convert a single file back into an array.
  *
  * @param \Psr\Http\Message\UploadedFileInterface $file The file to convert.
  * @return array
  */
 protected static function convertFile($file)
 {
     $error = $file->getError();
     $tmpName = '';
     if ($error === UPLOAD_ERR_OK) {
         $tmpName = $file->getStream()->getMetadata('uri');
     }
     return ['name' => $file->getClientFilename(), 'type' => $file->getClientMediaType(), 'tmp_name' => $tmpName, 'error' => $error, 'size' => $file->getSize()];
 }
 protected function uploadFile(UploadedFileInterface $content, $mime, $filePath)
 {
     $uri = $content->getStream()->getMetadata('uri');
     if (!$uri) {
         throw new SaveResourceErrorException('Unknown error: can\'t get uploaded file uri');
     }
     $uploadedMime = $this->filesystem->getMimetype($uri);
     if ($mime !== $uploadedMime) {
         /**
          * Try to remove unnecessary file because UploadFile object can be emulated
          * @see \Staticus\Middlewares\ActionPostAbstract::download
          */
         $this->filesystem->delete($uri);
         throw new WrongRequestException('Bad request: incorrect mime-type of the uploaded file');
     }
     $content->moveTo($filePath);
 }
Ejemplo n.º 11
0
 /**
  * Gets the path of an uploaded file, which usually points to a system temporary folder.
  *
  * @param UploadedFileInterface $file
  * @return string
  */
 static function getUploadedFilePath(UploadedFileInterface $file)
 {
     return $file->getStream()->getMetadata('uri');
 }