【问题标题】:October CMS extend System/Models/File十月 CMS 扩展系统/模型/文件
【发布时间】:2018-12-06 00:13:50
【问题描述】:

我在使用 System/Models/File 时尝试保留原始文件名,我得到以下代码来扩展此模型:

    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;

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

【问题讨论】:

  • but the problem is a link to an attached file is still being generated 表示您需要什么 - 停止生成链接或者它没有正确生成?
  • 我的意思是它仍然是由原始十月 CMS 方法生成的。例如,上传图片的文件路径为site.com/storage/app/uploads/public/5b3/74f/e7b/5b374fe7b9856296467891.png,但实际图片保存在site.com/storage/app/uploads/public/5b3 /74f/e7b/logo.png

标签: laravel extend octobercms


【解决方案1】:

哦,我看到它似乎尝试使用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_name 也与file_name 相同,因此当文件保存在磁盘上时,它将使用original name,并且在生成链接时,它也使用disk_name,这是原始file_name

现在您的链接和文件名已同步,并且始终相同。

如有任何疑问,请发表评论。

【讨论】:

  • 谢谢,就像一个魅力!但是还有 1 个问题,现在 10 月的图像路径非常有趣 /storage/app/uploads/public/log/op/ng/logo.png 也许可以将文件存储在 /storage/app/uploads/public/ 中?
  • 嗯需要检查一下,会检查并通知您
  • 嗨,你有什么解决办法吗?
  • 对不起,我没有得到改变来检查它,但我需要从核心调试什么是实际问题对不起,但这需要一点时间,因为我现在有很多工作所以:)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多