custom-d / simple-file-storage
A simple file storage
v1.4.3
2022-09-16 00:30 UTC
Requires
- php: >=7.3
- illuminate/support: >=8.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-10-20 04:48:04 UTC
README
Package description: Allows using file storage on your models that are searchable
Installation
Install via composer
composer require custom-d/simple-file-storage
Publish package assets (optional)
php artisan vendor:publish --provider="CustomD\SimpleFileStorage\ServiceProvider"
or set env variable:
SFS_TEMP_URL_MINUTES=15
connection=${DB_CONNECTION}
Run Migrations
php artisan migrate
Usage
on the model you want to have file attachements add the following trait:
...
use CustomD\SimpleFileStorage\HasFileStorage;
class MyModel extends Model
{
use HasFileStorage;
...
you can also optionaly set the following constants:
const DEFAULT_STORAGE_DISK = ''; //default is your config(filesystems.default)
//or override method:
protected function getDefaultDisk(): string
{
return 'xxx';
}
const DEFAULT_STORAGE_COLLECTION = 'default'; //default = default
//or override the method
protected function getCollectionName(?string $collection): string
{
return 'xxx';
}
Uploading files
you can upload either a base64 encoded or a form files request by using:
$model->storeFileFromRequest($key [,?array $custom_properties = null[, ?string $collection = null[, ?string $disk = null]]]);
$model->storeFilesFromRequest($keys [,?array $custom_properties = null[, ?string $collection = null[, ?string $disk = null]]]);
$model->storeFileFromBase64($base64Data [,?array $custom_properties = null[, ?string $collection = null[, ?string $disk = null]]]);
though it will throw an exception if the file is not uploadable or the key is not set.
retrieving files:
$model->attachements()->get(); // to get all
$model->getAttachments()->get([$collection]); //will only get the default collection attachemnts
//available scopes
$model->withProperty('key','value'); // key value set vai the custom_properties when storing
$model->orWithProperty('key','value'); // key value set vai the custom_properties when storing
$model->withoutProperty('key','value'); // key value set vai the custom_properties when storing
$model->orWithoutProperty('key', 'value'); // key value set vai the custom_properties when storing
$model->withoutPropertyKey('key'); // key set via teh custom properties when storing (not set in this case)
$model->orWithoutPropertyKey('key'); // key set via teh custom properties when storing (not set in this case)
$model->inCollection('collection'); //can pass a single, or array of collection names
$model->orInCollection('collection'); //can pass a single, or array of collection names
$model->notInCollection('collection'); //can pass a single or array of collection names
$model->orNotInCollection('collection'); //can pass a single or array of collection names
File Methods
- getFileName - gets the stored filename
- getStorage - gets the storage instance (so you could do $item->getStorage()->Xxxx($filename))
- missing - checks if the file is missing
- exists - checks if the file exists
- get - gets the file content
- url - gets the file url
- temporaryUrl - gets temporary url (AWS only)
- delete - deletes the file and record.
- download - to trigger a streamed download
- stream - to trigger an inline stream
Security
If you discover any security related issues, please email instead of using the issue tracker.
Credits
This package is bootstrapped with the help of melihovv/laravel-package-generator.