首页 > 解决方案 > Laravel 媒体库 - singleFile 方法无法正常工作

问题描述

我有一个带有FTP文件系统的相册模型来存储媒体图像,为了添加singleFile方法来为模型提供媒体,我有这个错误。如果我不使用该singleFile方法,它可以正常工作。

模型

class Album extends Model implements HasMedia, Auditable {

   ...

    public function registerMediaCollections(): void
    {
        $this
            ->addMediaCollection('image')
            ->singleFile();
    }

    ...

}

控制器

class AlbumImageController extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param AlbumImageRequest $request
     * @param Album $album
     * @return \Illuminate\Http\JsonResponse
     * @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\FileDoesNotExist
     * @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\FileIsTooBig
     */
    public function __invoke(AlbumImageRequest $request, Album $album)
    {
        $album
            ->addMediaFromRequest('image')
            ->toMediaCollection('image');

        return response()->json([
            'data'    => $album->getFirstMediaUrl('image'),
            'message' => Lang::get('messages.album.image')
        ], 200);
    }
}

错误

{
    "message": "The file  cannot be opened.",
    "exception": "RuntimeException",
    "file": "/var/www/vendor/nyholm/psr7/src/Factory/Psr17Factory.php",
    "line": 46,
    "trace": [
        {
            "file": "/var/www/vendor/symfony/psr-http-message-bridge/Factory/PsrHttpFactory.php",
            "line": 114,
            "function": "createStreamFromFile",
            "class": "Nyholm\\Psr7\\Factory\\Psr17Factory",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/symfony/psr-http-message-bridge/Factory/PsrHttpFactory.php",
            "line": 96,
            "function": "createUploadedFile",
            "class": "Symfony\\Bridge\\PsrHttpMessage\\Factory\\PsrHttpFactory",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/symfony/psr-http-message-bridge/Factory/PsrHttpFactory.php",
            "line": 68,
            "function": "getFiles",
            "class": "Symfony\\Bridge\\PsrHttpMessage\\Factory\\PsrHttpFactory",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php",
            "line": 137,
            "function": "createRequest",
            "class": "Symfony\\Bridge\\PsrHttpMessage\\Factory\\PsrHttpFactory",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php",
            "line": 829,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\RoutingServiceProvider",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php",
            "line": 714,
            "function": "build",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
            "line": 841,
            "function": "resolve",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php",
            "line": 652,
            "function": "resolve",
            "class": "Illuminate\\Foundation\\Application",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
            "line": 826,
            "function": "make",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/sentry/sentry-laravel/src/Sentry/Laravel/Http/LaravelRequestFetcher.php",
            "line": 32,
            "function": "make",
            "class": "Illuminate\\Foundation\\Application",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/sentry/sentry/src/Integration/RequestIntegration.php",
            "line": 119,
            "function": "fetchRequest",
            "class": "Sentry\\Laravel\\Http\\LaravelRequestFetcher",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/sentry/sentry/src/Integration/RequestIntegration.php",
            "line": 111,
            "function": "processEvent",
            "class": "Sentry\\Integration\\RequestIntegration",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/sentry/sentry/src/State/Scope.php",
            "line": 370,
            "function": "Sentry\\Integration\\{closure}",
            "class": "Sentry\\Integration\\RequestIntegration",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/sentry/sentry/src/Client.php",
            "line": 263,
            "function": "applyToEvent",
            "class": "Sentry\\State\\Scope",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/sentry/sentry/src/Client.php",
            "line": 154,
            "function": "prepareEvent",
            "class": "Sentry\\Client",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/sentry/sentry/src/Client.php",
            "line": 146,
            "function": "captureEvent",
            "class": "Sentry\\Client",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/sentry/sentry/src/State/Hub.php",
            "line": 140,
            "function": "captureException",
            "class": "Sentry\\Client",
            "type": "->"
        },
        {
            "file": "/var/www/app/Exceptions/Handler.php",
            "line": 40,
            "function": "captureException",
            "class": "Sentry\\State\\Hub",
            "type": "->"
        },
        {
            "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php",
            "line": 88,
            "function": "report",
            "class": "App\\Exceptions\\Handler",
            "type": "->"
        },
        {
            "function": "handleException",
            "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
            "type": "->"
        }
    ]
}

标签: phplaravelftpfilesystemslaravel-medialibrary

解决方案


推荐阅读