class FileManager

File Manager

Methods

__construct( array $supportedFileModels)

Create a new FileManager instance.

getModelInstance( string $fileType, string $parentType)

Create a new FileModelInterface instance, from the supportedFileModels table

addFileModel( string $fileType, string $parentType, string $fullyQualifiedClassName)

A a new FileModelInterface class name to the supported class list.

UploadedFile|null
copyUploadedFile( FileModelInterface $model, UploadedFile $uploadedFile, ConnectionInterface $con = null)

Copy UploadedFile into the server storage directory

int
saveImage( FileCreateOrUpdateEvent $event, FileModelInterface $imageModel)

Save file into the database

int
saveDocument( FileCreateOrUpdateEvent $event, FileModelInterface $documentModel)

Save file into the database

string
sanitizeFileName( string $string)

Sanitizes a filename replacing whitespace with dashes

deleteFile( FileModelInterface $model)

Delete image from file storage and database

string
renameFile( int $modelId, UploadedFile $uploadedFile)

Rename file with image model id

bool
isImage( string $mimeType)

Check if a file is an image Check based on mime type

Details

at line 37
__construct( array $supportedFileModels)

Create a new FileManager instance.

Parameters

array $supportedFileModels The key should have form type.parent, where type is the file type (document or image) and parent is the parent object of the file, form example product, brand, folder, etc.

at line 63
FileModelInterface getModelInstance( string $fileType, string $parentType)

Create a new FileModelInterface instance, from the supportedFileModels table

Parameters

string $fileType the file type, such as document, image, etc.
string $parentType the parent type, such as product, category, etc.

Return Value

FileModelInterface a file model interface instance

Exceptions

FileException if the file type is not supported, or if the class does not implements FileModelInterface

at line 96
addFileModel( string $fileType, string $parentType, string $fullyQualifiedClassName)

A a new FileModelInterface class name to the supported class list.

Parameters

string $fileType the file type, such as document, image, etc.
string $parentType the parent type, such as Product, Category, etc.
string $fullyQualifiedClassName the fully qualified class name

at line 111
UploadedFile|null copyUploadedFile( FileModelInterface $model, UploadedFile $uploadedFile, ConnectionInterface $con = null)

Copy UploadedFile into the server storage directory

Parameters

FileModelInterface $model Model saved
UploadedFile $uploadedFile Ready to be uploaded file
ConnectionInterface $con current transaction with database

Return Value

UploadedFile|null

Exceptions

ImageException

at line 176
int saveImage( FileCreateOrUpdateEvent $event, FileModelInterface $imageModel)

Save file into the database

Parameters

FileCreateOrUpdateEvent $event the event
FileModelInterface $imageModel the file model object (image or document) to save.

Return Value

int number of modified rows in database

at line 189
int saveDocument( FileCreateOrUpdateEvent $event, FileModelInterface $documentModel)

Save file into the database

Parameters

FileCreateOrUpdateEvent $event the event
FileModelInterface $documentModel the file model object (image or document) to save.

Return Value

int number of modified rows in database

at line 205
string sanitizeFileName( string $string)

Sanitizes a filename replacing whitespace with dashes

Removes special characters that are illegal in filenames on certain operating systems and special characters requiring special escaping to manipulate at the command line.

Parameters

string $string The filename to be sanitized

Return Value

string The sanitized filename

at line 215
deleteFile( FileModelInterface $model)

Delete image from file storage and database

Parameters

FileModelInterface $model File being deleted

at line 232
string renameFile( int $modelId, UploadedFile $uploadedFile)

Rename file with image model id

Parameters

int $modelId Model id
UploadedFile $uploadedFile File being saved

Return Value

string

at line 257
bool isImage( string $mimeType)

Check if a file is an image Check based on mime type

Parameters

string $mimeType File mime type

Return Value

bool