PHP Класс Plank\Mediable\MediaUploader

Validates files, uploads them to disk and generates Media
Автор: Sean Fraser ([email protected])
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__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.

Приватные методы

Метод Описание
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.

Описание методов

__construct() публичный Метод

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() публичный Метод

Set the source for the file.
public fromSource ( mixed $source ) : static
$source mixed
Результат static

getOnDuplicateBehavior() публичный Метод

Get current behavior when duplicate file is uploaded.
public getOnDuplicateBehavior ( ) : string
Результат string

import() публичный Метод

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
Результат Media

importPath() публичный Метод

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
Результат Media

inferAggregateType() публичный Метод

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
Результат string

onDuplicateError() публичный Метод

Throw an exception when file already exists at the destination.
public onDuplicateError ( ) : static
Результат static

onDuplicateIncrement() публичный Метод

Append incremented counter to file name when file already exists at destination.
public onDuplicateIncrement ( ) : static
Результат static

onDuplicateReplace() публичный Метод

Overwrite existing file when file already exists at destination.
public onDuplicateReplace ( ) : static
Результат static

possibleAggregateTypesForExtension() публичный Метод

Determine the aggregate type of the file based on the extension.
public possibleAggregateTypesForExtension ( string $extension ) : string | null
$extension string
Результат string | null

possibleAggregateTypesForMimeType() публичный Метод

Determine the aggregate type of the file based on the MIME type.
public possibleAggregateTypesForMimeType ( string $mime ) : string
$mime string
Результат string

setAllowUnrecognizedTypes() публичный Метод

Change whether files not matching any aggregate types are allowed.
public setAllowUnrecognizedTypes ( boolean $allow ) : static
$allow boolean
Результат static

setAllowedAggregateTypes() публичный Метод

Set a list of aggregate types that the source file must be restricted to.
public setAllowedAggregateTypes ( array $allowed_types ) : static
$allowed_types array
Результат static

setAllowedExtensions() публичный Метод

Set a list of file extensions that the source file must be restricted to.
public setAllowedExtensions ( array $allowed_extensions ) : static
$allowed_extensions array
Результат static

setAllowedMimeTypes() публичный Метод

Set a list of MIME types that the source file must be restricted to.
public setAllowedMimeTypes ( array $allowed_mimes ) : static
$allowed_mimes array
Результат static

setMaximumSize() публичный Метод

Change the maximum allowed filesize.
public setMaximumSize ( integer $size ) : static
$size integer
Результат static

setModelClass() публичный Метод

Change the class to use for generated Media.
public setModelClass ( string $class ) : static
$class string
Результат static

setOnDuplicateBehavior() публичный Метод

Change the behaviour for when a file already exists at the destination.
public setOnDuplicateBehavior ( string $behavior ) : static
$behavior string
Результат static

setStrictTypeChecking() публичный Метод

Change whether both the MIME type and extensions must match the same aggregate type.
public setStrictTypeChecking ( boolean $strict ) : static
$strict boolean
Результат static

setTypeDefinition() публичный Метод

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
Результат static

toDestination() публичный Метод

Set the filesystem disk and relative directory where the file will be saved.
public toDestination ( string $disk, string $directory ) : static
$disk string
$directory string
Результат static

toDirectory() публичный Метод

Set the directory relative to the filesystem disk at which the file will be saved.
public toDirectory ( string $directory ) : static
$directory string
Результат static

toDisk() публичный Метод

Set the filesystem disk on which the file will be saved.
public toDisk ( string $disk ) : static
$disk string
Результат static

update() публичный Метод

Reanalyze a media record's file and adjust the aggregate type and size, if necessary.
public update ( Media $media ) : boolean
$media Media
Результат boolean Whether the model was modified

upload() публичный Метод

Validates the source, then stores the file onto the disk and creates and stores a new Media instance.
public upload ( ) : Media
Результат Media

useFilename() публичный Метод

Specify the filename to copy to the file to.
public useFilename ( string $filename ) : static
$filename string
Результат static

useHashForFilename() публичный Метод

Indicates to the uploader to generate a filename using the file's MD5 hash.
public useHashForFilename ( ) : static
Результат static

useOriginalFilename() публичный Метод

Restore the default behaviour of using the source file's filename.
public useOriginalFilename ( ) : static
Результат static