Skip to content

Events ​

The FileStorage behavior dispatches events while a file row is saved or deleted. The subject of every event is the file storage table. Register listeners on the global event manager:

php
use Cake\Event\EventInterface;
use Cake\Event\EventManager;

EventManager::instance()->on(
    'FileStorage.afterStoringFile',
    function (EventInterface $event): void {
        $entity = $event->getData('entity');
        $file = $event->getData('file');
        // ...
    },
);

Saving ​

In the order they fire for an upload:

EventDataWhen
FileStorage.beforeSaveentity, storageAdapterBefore the row is written.
FileStorage.beforeStoringFileentity, fileBefore the file is written to the adapter.
FileStorage.afterStoringFileentity, fileAfter the file was written.
FileStorage.blobReusedentity, fileInstead of the two storing events, when a deduplicated upload found its content already stored. Nothing is written then.
FileStorage.beforeFileProcessingentity, fileBefore variants are generated.
FileStorage.afterFileProcessingentity, fileAfter variants were generated.
FileStorage.afterSaveentity, storageAdapterAfter the row holds its final path and variants.

entity is the file_storage row, file the library's file object, storageAdapter the Flysystem adapter the row is stored on.

The two processing events also fire when variants are regenerated by the variant command or its queue task.

Listeners on the storing events and deduplication

A listener on FileStorage.afterStoringFile, a virus scan for example, does not run for an upload that reuses stored content. Listen on FileStorage.blobReused as well if it has to see every upload.

Attaching by hash ​

EventDataWhen
FileStorage.blobAttachedentityAfter BlobAttacher::attach() created a row for stored content. None of the saving events fire for such a row.

Deleting ​

EventDataWhen
FileStorage.afterDeleteentityAfter the row was deleted, before its files are removed.

Existing-file conversion ​

FileStorage.blobConverted fires once per committed conversion by file_storage deduplicate. Its subject is the file storage table. Data:

KeyValue
idConverted row id.
oldPathPrevious path, whose file is kept.
pathVerified blob path now stored on the row.
adapterRow's storage adapter name.
hashSHA-256 computed from source bytes.

The event fires after the database commit. Listener exceptions are reported as warnings; the row stays converted. Hash-only and dry runs do not fire it.

Stored-file registration ​

FileStorage.blobImported fires once per committed call to BlobImporter::import(). Its subject is the file storage table. Data:

KeyValue
adapterSource and blob storage adapter name.
hashComputed or trusted SHA-256 hash.
pathCommitted blob path.
sourcePathOriginal source object path.
reusedtrue when existing destination content was reused; false when copied.

The event fires after commit and optional source deletion. Listener exceptions are logged as warnings; the blob stays registered.

FileStorage.uploadCompleted ​

Dispatched after a resumable session becomes complete, including a zero-length POST. Event data contains the session array under upload, with its ID, model, collection, size, and server-computed SHA-256 hash. Delivery is at most once with no retry. Save the application entity and call ResumableUploads::consume() to create the file row. See resumable uploads.

Released under the MIT License.