Get purged file upload in afterSave

Hello,

Does somebody know, how I can get a purged file upload in afterSave?

I would like to upload a ZIP file, unpack it and store it in my own location.

I checked the documentation here:
Traits - October CMS - 3.x

…and setup my code like this:

class Project extends Model
{
    use \October\Rain\Database\Traits\Purgeable;

    protected $purgeable = ['webproject_files'];

    // relations
    public $attachOne = [
        'webproject_files' => 'System\Models\File',
    ];

    /**
     * @var string The database table used by the model.
     */
    public $table = 'creationhandicap_project_project';

    /**
     * @var array Validation rules
     */
    public $rules = [
        'webproject_files'      => 'nullable|file|mimes:zip',
    ];

    // save webproject if there
    public function afterSave()
    {
        $webprojectZip = $this->getOriginalPurgeValue('webproject_files');
        dd($webprojectZip); // this is null
    }

}

But the value $webprojectZip is null in the dump.

Is it even possible the way I set it up?

After some researching, I checked that files somehow are only there, after creation of the model. So the approach from above doesn’t work, as long as I understood it.

I just don’t purge the field anymore and handle the ZIP after creation…

1 Like

Yes, I’m not sure attachments are compatible with the Purgeable trait… or even why you would need this.

I want to upload a ZIP-file, then unpack it and remove it. So if a new ZIP-file comes in, I know, I need to replace the unpacked files.

But I solved this with a checkbox. When the checkbox is active, then I know, I need to delete the already unpacked files and replace them with the new files in the new ZIP.

EDIT:
I now remove the ZIP-file from the field like this after extracting it:

$webprojectFileField = 'webproject_files';
if (file_exists($zipFilePath) && $this->$webprojectFileField instanceof File) {
    $this->webproject_files->delete();
    $this->webproject_files = null;
}

Like this the checkbox is not needed and, you’re right, the field doesn’t has to be purgeable.

1 Like