For handle S3 and file using drupal's file approach
ygthor/laravel-file-handler is a Laravel package for for handle s3 and file using drupal's file approach.
It currently has 0 GitHub stars and 28 downloads on Packagist.
Install it with composer require ygthor/laravel-file-handler.
Discover more Laravel packages by ygthor
or browse all Laravel packages to compare alternatives.
Last updated
You can install the package via composer:
composer require ygthor/laravel-file-handler
CREATE TABLE `file_handles` (
`fid` bigint(20) UNSIGNED NOT NULL,
`filename` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`filepath` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`file_ext` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`filemine` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`filesize` int(11) DEFAULT NULL,
`disk` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(500) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`model_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`model_primary_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`model_column` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`uploader_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_by` int(11) DEFAULT NULL,
`updated_by` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE `file_handles` ADD PRIMARY KEY (`fid`);
ALTER TABLE `file_handles` MODIFY `fid` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;
AWS_ACCESS_KEY_ID="GET_FROM_OBJECT_STORAGE"
AWS_SECRET_ACCESS_KEY="GET_FROM_OBJECT_STORAGE"
AWS_DEFAULT_REGION="ap-south-1"
AWS_BUCKET="my-bucket"
AWS_ENDPOINT="https://ap-south-1.linodeobjects.com"
AWS_USE_PATH_STYLE_ENDPOINT=false
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'bucket_endpoint' => false,
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],
function setDisk($disk='s3'){}
function setFilename($value){}
function setContent($value){}
# set all variable object, use together with upload() function
function set($arr){}
$arr = [
'disk' => '', //default s3
'filename' => '', //required
'content' => '', //required, data from file_get_contents()
'model_name' => '', // optional, for ease in reverse checking
'model_primary_key' => '', // optional, for ease in reverse checking
'model_column' => '', // optional, for ease in reverse checking
'uploader_name' => '', // optional, for ease in reverse checking
'status' => '', // default permanent, if is temporary, file will delete automatically after specific time (feature still In DEV)
];
# use after set()
function upload($filename=null, $content=null){}
# check file exist in S3 server
function exists($filename){}
# delete file in file_handles and S3 server
function delete($fid){}
function getS3Path($fid=5){}
function getS3Url($fid=5){}
function getS3UrlByPath('path/to/file'){}
...
use LaravelFileHandler;
...
class TestController{
...
function save(){
$profile_image = $request->file('profile_image');
$user_id = auth()->id();
$file_handle = LaravelFileHandler::set([
'filename' => 'optional_image_path/' . $user_id . '/profile',
'content' => $profile_image,
'model_name' => '\App\Models\User',
'model_primary_key' => 'id',
'model_column' => 'profile_photo_fid',
'uploader_name' => auth()->user()->name,
])->upload();
User::find($user_id)->fill(['photo_fid' => $file_handle->fid])->save();
}
...
}
# Helper Function to retrieve file on S3
$path = LaravelFileHandler::getS3Path($fid=5);
$s3_file_url = LaravelFileHandler::getS3Url($fid=5);
$s3_file_url = LaravelFileHandler::getS3UrlByPath('path/to/file');
$is_exists = LaravelFileHandler::exists($file_path);
//return true if delete success
//retrun false if fid not found / path not found
$status = LaravelFileHandler::delete($fid=5);
# if you are not using 's3' in config
# you need to use setDisk function
$s3_file_url = LaravelFileHandler::setDisk('s3')->getS3Path($fid=5)
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.