/**
  * @param File $file
  * @param string $name
  *
  * @return File
  *
  * @throws FileManagerException
  */
 public function renameFile(File $file, $name)
 {
     $name = pathinfo(basename($name), PATHINFO_FILENAME);
     $new_file = app(File::class)->setPath($file->getPath())->existing($file->fullName())->setName($name);
     $new_file->setName($name);
     $old_file_path = $file->fullPath();
     $new_file_path = $new_file->fullPath();
     if (!FileSystem::exists($old_file_path)) {
         throw new FileManagerException($this, 'err_file_not_found');
     }
     // if old name is the same as new one, just return existing file info
     if ($old_file_path === $new_file_path) {
         return $file->details();
     }
     if (FileSystem::exists($new_file_path)) {
         $new_file = $this->uniqueName->file($new_file);
     }
     if (rename($old_file_path, $new_file->fullPath())) {
         if ($file->isImage()) {
             $this->thumb->rename($file, $new_file);
         }
         return $file->setName($new_file->name())->setFileThumb()->details();
     }
     throw new FileManagerException($this, 'err_file_cant_rename');
 }
Example #2
0
 /**
  * Create thumbs for file
  *
  * @param File $file
  * @return array
  * @throws \Crip\FileManager\Exceptions\FileManagerException
  */
 public function create(File $file)
 {
     foreach ($this->sizes as $size_key => $size) {
         $img = app(ImageManager::class)->make($file->fullPath());
         $new_path = $file->getPath()->thumbPath($size_key);
         FileSystem::mkdir($new_path, 777, true);
         switch ($size[2]) {
             case 'width':
                 // resize the image to a width of $sizes[ 0 ] and constrain aspect ratio (auto height)
                 $img->resize($size[0], null, function ($constraint) {
                     $constraint->aspectRatio();
                 });
                 break;
             case 'height':
                 // resize the image to a height of $sizes[ 1 ] and constrain aspect ratio (auto width)
                 $img->resize(null, $size[1], function ($constraint) {
                     $constraint->aspectRatio();
                 });
                 break;
                 // 'resize'
             // 'resize'
             default:
                 $img->fit($size[0], $size[1]);
                 break;
         }
         $img->save($file->getPath()->thumbPath($size_key, $file));
     }
     return $this->details($file);
 }