/** * @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'); }