PHP Class Plank\Mediable\MediaUploader

Validates files, uploads them to disk and generates Media
Author: Sean Fraser ([email protected])
Show file Open project: plank/laravel-mediable Class Usage Examples

Public Methods

Method Description
__construct ( FileSystemManager $filesystem, Plank\Mediable\SourceAdapters\SourceAdapterFactory $factory, array | null $config = null ) Constructor.
fromSource ( mixed $source ) : static Set the source for the file.
getOnDuplicateBehavior ( ) : string Get current behavior when duplicate file is uploaded.
import ( string $disk, string $directory, string $filename, string $extension ) : Media Create a Media record for a file already on a disk.
importPath ( string $disk, string $path ) : Media Create a Media record for a file already on a disk.
inferAggregateType ( string $mime_type, string $extension ) : string Determine the aggregate type of the file based on the MIME type and the extension.
onDuplicateError ( ) : static Throw an exception when file already exists at the destination.
onDuplicateIncrement ( ) : static Append incremented counter to file name when file already exists at destination.
onDuplicateReplace ( ) : static Overwrite existing file when file already exists at destination.
possibleAggregateTypesForExtension ( string $extension ) : string | null Determine the aggregate type of the file based on the extension.
possibleAggregateTypesForMimeType ( string $mime ) : string Determine the aggregate type of the file based on the MIME type.
setAllowUnrecognizedTypes ( boolean $allow ) : static Change whether files not matching any aggregate types are allowed.
setAllowedAggregateTypes ( array $allowed_types ) : static Set a list of aggregate types that the source file must be restricted to.
setAllowedExtensions ( array $allowed_extensions ) : static Set a list of file extensions that the source file must be restricted to.
setAllowedMimeTypes ( array $allowed_mimes ) : static Set a list of MIME types that the source file must be restricted to.
setMaximumSize ( integer $size ) : static Change the maximum allowed filesize.
setModelClass ( string $class ) : static Change the class to use for generated Media.
setOnDuplicateBehavior ( string $behavior ) : static Change the behaviour for when a file already exists at the destination.
setStrictTypeChecking ( boolean $strict ) : static Change whether both the MIME type and extensions must match the same aggregate type.
setTypeDefinition ( string $type, array $mime_types, array $extensions ) : static Add or update the definition of a aggregate type.
toDestination ( string $disk, string $directory ) : static Set the filesystem disk and relative directory where the file will be saved.
toDirectory ( string $directory ) : static Set the directory relative to the filesystem disk at which the file will be saved.
toDisk ( string $disk ) : static Set the filesystem disk on which the file will be saved.
update ( Media $media ) : boolean Reanalyze a media record's file and adjust the aggregate type and size, if necessary.
upload ( ) : Media Process the file upload.
useFilename ( string $filename ) : static Specify the filename to copy to the file to.
useHashForFilename ( ) : static Indicates to the uploader to generate a filename using the file's MD5 hash.
useOriginalFilename ( ) : static Restore the default behaviour of using the source file's filename.

Private Methods

Method Description
deleteExistingMedia ( Media $model ) : void Delete the media that previously existed at a destination.
generateFilename ( ) : string Generate the model's filename.
generateHash ( ) : string Calculate hash of source contents.
generateUniqueFilename ( Media $model ) : void Increment model's filename until one is found that doesn't already exist.
handleDuplicate ( Media $model ) : void Decide what to do about duplicated files.
makeModel ( ) : Media Generate an instance of the Media class.
sanitizeFileName ( string $file ) : string Remove any disallowed characters from a filename.
sanitizePath ( string $path ) : string Remove any disallowed characters from a directory value.
verifyDestination ( Media $model ) : void Verify that the intended destination is available and handle any duplications.
verifyDisk ( string $disk ) : string Ensure that the provided filesystem disk name exists and is allowed.
verifyExtension ( string $extension ) : string Ensure that the file's extension is allowed.
verifyFileSize ( integer $size ) : integer Verify that the file being uploaded is not larger than the maximum.
verifyMimeType ( string $mime_type ) : string Ensure that the file's mime type is allowed.
verifySource ( ) : void Ensure that a valid source has been provided.

Method Details

__construct() public method

Constructor.
public __construct ( FileSystemManager $filesystem, Plank\Mediable\SourceAdapters\SourceAdapterFactory $factory, array | null $config = null )
$filesystem FileSystemManager
$factory Plank\Mediable\SourceAdapters\SourceAdapterFactory
$config array | null

fromSource() public method

Set the source for the file.
public fromSource ( mixed $source ) : static
$source mixed
return static

getOnDuplicateBehavior() public method

Get current behavior when duplicate file is uploaded.
public getOnDuplicateBehavior ( ) : string
return string

import() public method

Create a Media record for a file already on a disk.
public import ( string $disk, string $directory, string $filename, string $extension ) : Media
$disk string
$directory string
$filename string
$extension string
return Media

importPath() public method

Create a Media record for a file already on a disk.
public importPath ( string $disk, string $path ) : Media
$disk string
$path string Path to file, relative to disk root
return Media

inferAggregateType() public method

Determine the aggregate type of the file based on the MIME type and the extension.
public inferAggregateType ( string $mime_type, string $extension ) : string
$mime_type string
$extension string
return string

onDuplicateError() public method

Throw an exception when file already exists at the destination.
public onDuplicateError ( ) : static
return static

onDuplicateIncrement() public method

Append incremented counter to file name when file already exists at destination.
public onDuplicateIncrement ( ) : static
return static

onDuplicateReplace() public method

Overwrite existing file when file already exists at destination.
public onDuplicateReplace ( ) : static
return static

possibleAggregateTypesForExtension() public method

Determine the aggregate type of the file based on the extension.
public possibleAggregateTypesForExtension ( string $extension ) : string | null
$extension string
return string | null

possibleAggregateTypesForMimeType() public method

Determine the aggregate type of the file based on the MIME type.
public possibleAggregateTypesForMimeType ( string $mime ) : string
$mime string
return string

setAllowUnrecognizedTypes() public method

Change whether files not matching any aggregate types are allowed.
public setAllowUnrecognizedTypes ( boolean $allow ) : static
$allow boolean
return static

setAllowedAggregateTypes() public method

Set a list of aggregate types that the source file must be restricted to.
public setAllowedAggregateTypes ( array $allowed_types ) : static
$allowed_types array
return static

setAllowedExtensions() public method

Set a list of file extensions that the source file must be restricted to.
public setAllowedExtensions ( array $allowed_extensions ) : static
$allowed_extensions array
return static

setAllowedMimeTypes() public method

Set a list of MIME types that the source file must be restricted to.
public setAllowedMimeTypes ( array $allowed_mimes ) : static
$allowed_mimes array
return static

setMaximumSize() public method

Change the maximum allowed filesize.
public setMaximumSize ( integer $size ) : static
$size integer
return static

setModelClass() public method

Change the class to use for generated Media.
public setModelClass ( string $class ) : static
$class string
return static

setOnDuplicateBehavior() public method

Change the behaviour for when a file already exists at the destination.
public setOnDuplicateBehavior ( string $behavior ) : static
$behavior string
return static

setStrictTypeChecking() public method

Change whether both the MIME type and extensions must match the same aggregate type.
public setStrictTypeChecking ( boolean $strict ) : static
$strict boolean
return static

setTypeDefinition() public method

Add or update the definition of a aggregate type.
public setTypeDefinition ( string $type, array $mime_types, array $extensions ) : static
$type string the name of the type
$mime_types array list of MIME types recognized
$extensions array list of file extensions recognized
return static

toDestination() public method

Set the filesystem disk and relative directory where the file will be saved.
public toDestination ( string $disk, string $directory ) : static
$disk string
$directory string
return static

toDirectory() public method

Set the directory relative to the filesystem disk at which the file will be saved.
public toDirectory ( string $directory ) : static
$directory string
return static

toDisk() public method

Set the filesystem disk on which the file will be saved.
public toDisk ( string $disk ) : static
$disk string
return static

update() public method

Reanalyze a media record's file and adjust the aggregate type and size, if necessary.
public update ( Media $media ) : boolean
$media Media
return boolean Whether the model was modified

upload() public method

Validates the source, then stores the file onto the disk and creates and stores a new Media instance.
public upload ( ) : Media
return Media

useFilename() public method

Specify the filename to copy to the file to.
public useFilename ( string $filename ) : static
$filename string
return static

useHashForFilename() public method

Indicates to the uploader to generate a filename using the file's MD5 hash.
public useHashForFilename ( ) : static
return static

useOriginalFilename() public method

Restore the default behaviour of using the source file's filename.
public useOriginalFilename ( ) : static
return static