Ejemplo n.º 1
0
 /**
  * @param UploadedFile $file
  * @param StorageConfig $config
  * @param int $index
  * @throws \Exception
  * @return bool|string
  */
 public function saveFile(UploadedFile $file, StorageConfig $config, $index = 1)
 {
     if (!empty($config->filesystem)) {
         if ($file->error == UPLOAD_ERR_OK && is_uploaded_file($file->tempName)) {
             $filePath = $config->resolveFilePath($file->name, $this->owner, $index);
             $stream = fopen($file->tempName, 'r+');
             if ($config->filesystem->writeStream($filePath, $stream, ['visibility' => $config->visibility])) {
                 return $filePath;
             }
         }
     } else {
         $filePath = $config->resolveFilePath($file->name, $this->owner, $index);
         $dir = pathinfo($filePath, PATHINFO_DIRNAME);
         if (!is_dir($dir)) {
             if (!mkdir($dir, 0755, true)) {
                 throw new \Exception("Can't create the directory '{$dir}'");
             }
         }
         if ($file->saveAs($filePath)) {
             return $filePath;
         }
     }
     return false;
 }
Ejemplo n.º 2
0
 /**
  * @param UploadedFile $uploadedFile
  * @param StorageConfig $config
  * @return FileInterface|null
  */
 protected function saveUploadedFile(UploadedFile $uploadedFile, StorageConfig $config)
 {
     $relation = $this->owner->getRelation($config->relation);
     /** @var FileInterface $fileClass */
     $fileClass = $relation->modelClass;
     $file = $fileClass::getInstanceFromUploadedFile($uploadedFile);
     $filesystemComponent = $this->getFilesystemComponent();
     $path = $config->resolveFilePath($uploadedFile->name, $this->owner);
     if (isset($config->events['beforeSave']) && $config->events['beforeSave'] instanceof \Closure) {
         call_user_func($config->events['beforeSave'], $file, $path, $config->filesystem);
     }
     if ($path = $filesystemComponent->saveFile($file, $config->filesystem, $path, false, true)) {
         $file->setPath($path);
         $file->setFilesystemName($config->filesystem);
         return $file;
     } else {
         return null;
     }
 }