You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6.5.7.x中fileSaver->persistFileToMedia失效求助

升级Shopware 6.5.7.x后插件停止工作,出现500错误

错误详情

{
  "code": "0",
  "status": "500",
  "title": "Internal Server Error",
  "detail": "Warning: getimagesize(/var/www/html/public/media/64/fd/a6/1701174382/localstorage im firefox.PNG): Failed to open stream: No such file or directory",
  "meta": {
    "file": "/var/www/html/vendor/shopware/core/Content/Media/Metadata/MetadataLoader/ImageMetadataLoader.php",
    "line": 24,
    "trace": []
  },
  "trace": [
    {"file": "/var/www/html/vendor/shopware/core/Content/Media/Metadata/MetadataLoader.php", "line": 33, "function": "extractMetadata"},
    {"file": "/var/www/html/vendor/shopware/core/Content/Media/File/FileSaver.php", "line": 84, "function": "loadFromFile"},
    {"file": "/var/www/html/custom/plugins/my_plugin/src/Services/ImageWatermarkService.php", "line": 341, "function": "persistFileToMedia"}
  ]
}

生成图片备份的代码

private function makeBackup(MediaEntity $image, string $suffix, Context $context)
{
    $oldMediaId = $image->getId();
    $newMediaId = $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($oldMediaId): string {
        $behavior = new CloneBehavior([
            'mimeType' => null,
            'fileExtension' => null,
            'uploadedAt' => null,
            'fileName' => null,
            'fileSize' => null,
            'metaData' => null,
        ]);

        $newMediaId = Uuid::randomHex();
        $this->mediaRepository->clone($oldMediaId, $context, $newMediaId, $behavior);

        return $newMediaId;
    });

    //$path = $this->urlGeneratorInterface->getRelativeMediaUrl($image); //this was working before 6.5.7.x

    $path = $image->getPath();

    try {
        $this->publicFileSystem->read($path);
        $stream = $this->publicFileSystem->readStream($path);
        $metaData = stream_get_meta_data($stream);
        $oldFileNameWithPath = $metaData['uri'];
    } catch (UnableToReadFile $e) {
        echo "[ERROR] File $path does not exists, can't backup.\n";
        return 1;
    }

    $mimeType = mime_content_type($path);
    $fileExtension = pathinfo($path, PATHINFO_EXTENSION);
    $fileSize = filesize($oldFileNameWithPath);
    $fileName = pathinfo($oldFileNameWithPath, PATHINFO_FILENAME) . $suffix . $newMediaId;
    $mediaFile = new MediaFile($oldFileNameWithPath, $mimeType, $fileExtension, $fileSize);

    /*            dd($mediaFile,
                $fileName,
                $newMediaId,
                $context);*/

    $this->fileSaver->persistFileToMedia(
        $mediaFile,
        $fileName,
        $newMediaId,
        $context
    );

    return $newMediaId;
}

调试输出(取消dd()注释后)

1 in ImageWatermarkService.php on line 335:
Shopware\Core\Content\Media\File\MediaFile {#1353
  -fileName: "/var/www/html/public/media/64/fd/a6/1701174382/localstorage im firefox.PNG"
  -mimeType: "image/png"
  -fileExtension: "PNG"
  -fileSize: 20884
  -hash: null
}

2 in ImageWatermarkService.php on line 335:
"localstorage im firefox_old018c15f0ec80707c96259735150e89fe"

3 in ImageWatermarkService.php on line 335:
"018c15f0ec80707c96259735150e89fe"

4 in ImageWatermarkService.php on line 335:
Shopware\Core\Framework\Context {#1252
  #extensions: []
[...]

问题分析

插件在Shopware 6.5.7.x之前版本正常工作,升级后出现上述错误。推测问题与Shopware 6.5.7版本的路径变更有关,已移除旧的UrlGeneratorInterface并改用$image->getPath()获取路径,但问题仍未解决。调试输出显示MediaFile的文件路径、名称等信息均正常,但调用persistFileToMedia时仍提示文件不存在。


内容的提问来源于stack exchange,提问作者user21851666

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 02:13:21