Skip to content

Asset meta lost on move / rename #3896

@RafaelKr

Description

@RafaelKr

Bug Description

I have a form with a file upload. After uploading the form it uses the SubmissionCreated event to rename the file. But after calling $asset->rename() the meta-yaml-file loses properties.

Screenshot-from-2021-06-16-13-18-42.png.yaml

data: {  }
size: 6294
last_modified: 1624437139
width: 620
height: 84
mime_type: image/png

Doe_John_Screenshot-from-2021-06-16-13-18-42.png.yaml

data: {  }

When viewing the form submission in the CP the meta is recreated.

How to Reproduce

Easy way:
Probably just add an asset and call rename() on it

The way in my project:

Extra Detail

app/Listeners/RenameFormAssets.php
<?php

namespace App\Listeners;

use Statamic\Facades\Asset;
use Statamic\Events\SubmissionCreated;

class RenameFormAssets
{
    /**
     * Handle the event.
     *
     * @param  SubmissionCreated  $event
     * @return void
     */
    public function handle(SubmissionCreated $event)
    {
        $submission = $event->submission;

        $form = $submission->form();
        $fileField = $form->blueprint()->fields()->get('file');
        $assetContainer = $fileField->config()['container'];

        $asset = Asset::find($assetContainer . '::' . $submission->get('file'));

        $newFileName = implode('_', [
            $submission->get('lastname'),
            $submission->get('firstname'),
            $asset->filename(),
        ]);
        $asset->rename($newFileName);

        $submission->set('file', $asset->path());
        $submission->save();
    }
}

Environment

Statamic version: 3.1.26

PHP version: 7.4

Install method (choose one):

  • Fresh install from statamic/statamic

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions