ebess / advanced-nova-media-library

A Laravel Nova field for displaying, creating, updating and ordering a Spatie Media Library model.
597 stars 297 forks source link

`Undefined array key "key"` on Vapor due to Octane #369

Open asugai opened 1 year ago

asugai commented 1 year ago

Laravel Framework v9.43.0 Nova v4.19.5 ebess/advanced-nova-media-library v4.0.5

Issue: When I try to upload a file on my local machine to S3 without using octane, it works, but when deployed to Vapor running with Octane, the exact same code gets this error:

vendor/ebess/advanced-nova-media-library/src/Fields/Media.php:385 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError

Undefined array key \"key\"

The file is saved correctly from the UI to the S3 tmp folder. The backend just can't seem to find the property in the $file var.

Sample payload

__media__[main_image][0][is_vapor_upload]: true
__media__[main_image][0][key]: tmp/9d7c5c1b-b324-4696-bd48-15303e8be01a
__media__[main_image][0][uuid]: 9d7c5c1b-b324-4696-bd48-15303e8be01a
__media__[main_image][0][file_name]: Screen-Shot-2022-09-09-at-12.53.49-PM (1).png
__media__[main_image][0][file_size]: 37451
__media__[main_image][0][mime_type]: image/png
/**
     * This creates a Media object from a previously, client-side, uploaded file.
     * The file is uploaded using a pre-signed S3 URL, via Vapor.store.
     * This method will use addMediaFromUrl(), passing it the
     * temporary location of the file.
     */
    private function makeMediaFromVaporUpload(array $file, HasMedia $model): FileAdder
    {
        $diskName = config('filesystems.default');
        $disk = config('filesystems.disks.' . $diskName . 'driver') === 's3' ? $diskName : 's3'; 
        $url = Storage::disk($disk)->temporaryUrl($file['key'], Carbon::now()->addHour()); // bug is here
        return $model->addMediaFromUrl($url)
            ->usingFilename($file['file_name']);
    }

.env

MEDIA_DISK=s3
ENABLE_MEDIA_LIBRARY_VAPOR_UPLOADS=true

Files field definition

Images::make('Main image', 'main_image')
                ->temporary(now()->addMinutes(10))
                ->uploadsToVapor(),

vapor.yml

development:
        timeout: 30
        cache: project-dev
        domain: dev.project.com
        database: project-dev
        octane: true
        octane-database-session-persist: true
        octane-database-session-ttl: 10
        storage: dev.project.com
        memory: 2048
        cli-memory: 512
        runtime: 'php-8.1:al2'
        gateway-version: 2
        warm: 10
        build:
            - 'composer install --no-dev'
            - 'php artisan event:cache'
            - 'npm run build'
        deploy:
            - 'php artisan migrate --force'

Commenting out the octane section fixes the error - but the app experience degrades.

numeralsix commented 1 year ago

Also experiencing this issue exactly as described - very similar build.

Removing octane solves it. Would be very good if we could use both simultaneously. Not sure if any maintainers have an inkling where the issue might originate or if solvable. Happy to help where I can and definitely test any solution