十月CMS扩展系统/模型/文件

时间:2018-06-27 07:34:42

标签: laravel extend octobercms

在使用系统/模型/文件时,我试图保留原始文件名,我得到了以下代码来扩展此模型:

    namespace System\Models;
class NewFile extends File { public function fromPost($uploadedFile) { if ($uploadedFile === null) { return; }

  $this->file_name = $uploadedFile->getClientOriginalName();
  $this->file_size = $uploadedFile->getClientSize();
  $this->content_type = $uploadedFile->getMimeType();
  $this->disk_name = $this->getDiskName();

  /*
   * getRealPath() can be empty for some environments (IIS)
   */
  $realPath = empty(trim($uploadedFile->getRealPath()))
      ? $uploadedFile->getPath() . DIRECTORY_SEPARATOR . $uploadedFile->getFileName()
      : $uploadedFile->getRealPath();

  //$this->putFile($realPath, $this->disk_name);
  $this->putFile($realPath, $this->file_name);

  return $this;

它与文件本身一起使用,保留了原始名称,但问题是仍在生成附加文件的链接。伤了我的心,但无法完成这项工作。谁能详细说明如何解决?

1 个答案:

答案 0 :(得分:0)

哦,我看到它似乎尝试使用disk_name来生成URL

所以您在保存图像方面做得很好

//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);

但是您只需要替换一行即可。.只需撤消更改并进行这一更改

 $this->file_name = $uploadedFile->getClientOriginalName();
 $this->file_size = $uploadedFile->getClientSize();
 $this->content_type = $uploadedFile->getMimeType();
 // $this->disk_name = $this->getDiskName();  
 $this->disk_name = $this->file_name; 
 // use same file_name for disk ^ HERE
  

链接逻辑(仅供参考vendor\october\rain\src\Database\Attach\File.phpmodules\system\models\File.php

/**
 * Returns the public address to access the file.
 */
public function getPath()
{
    return $this->getPublicPath() . $this->getPartitionDirectory() . $this->disk_name;
}

/**
* Define the public address for the storage path.
*/
public function getPublicPath()
{
    $uploadsPath = Config::get('cms.storage.uploads.path', '/storage/app/uploads');

    if ($this->isPublic()) {
        $uploadsPath .= '/public';
    }
    else {
        $uploadsPath .= '/protected';
    }

    return Url::asset($uploadsPath) . '/';
}

只需将disk_namefile_name相同,因此当文件保存在磁盘上时,它将使用original name,并且在生成链接时,它也会使用原始的disk_name {1}}

现在您的链接和文件名已同步,并且将始终保持不变。

如有疑问,请发表评论。