artincms/laravel_file_manager

Official ArtinCMS.com Laravel File Manager Package

Installs: 1 080

Dependents: 4

Suggesters: 0

Security: 0

Stars: 4

Watchers: 3

Forks: 3

Open Issues: 10

Language:JavaScript

dev-master 2021-05-30 07:45 UTC

This package is auto-updated.

Last update: 2024-03-29 03:50:51 UTC


README

laravel file manager is a package for

  • manage file uploaded by users with check ACL. you can upload file with custom size
  • generate download link
  • store file in Sotrage folder
  • optimize image
  • crop image when file upload , edit file or generate link with three kind of crop .

Requiments

  • PHP >= 7.0
  • Laravel 5.5|5.6
The package will use these optimizers if you wnat to enable optimization:

Installation

Quick installation

composer require "artincms/laravel_file_manager"
for laravel less than 5.5
Register provider and facade on your config/app.php file.
'providers' => [
    ...,
    ArtinCMS\LFM\LFMServiceProvider::class,
]
'aliases' => [
    ...,
    'FileManager' => ArtinCMS\LFM\Facades\FileManager::class,
]
publish vendor
 $ php artisan vendor:publish --provider="ArtinCMS\LFM\LFMServiceProvider"
if update package for publish vendor you should run :
 $ php artisan vendor:publish --provider="ArtinCMS\LFM\LFMServiceProvider" --force
migrate tabales
  $ php artisan migrate
  
seed data to lfm_file_mime_type table
  php artisan db:seed --class="ArtinCMS\LFM\Database\Seeds\FilemanagerTableSeeder"
  

more installation details

The package will use these optimizers if they are present on your system:

for install this package in your server :

Here's how to install all the optimizers on Ubuntu:

  sudo apt-get install jpegoptim
  sudo apt-get install optipng
  sudo apt-get install pngquant
  sudo npm install -g svgo
  sudo apt-get install gifsicle

And here's how to install the binaries on MacOS (using Homebrew):

  brew install jpegoptim
  brew install optipng
  brew install pngquant
  brew install svgo
  brew install gifsicle

for enable optimizer in package you shoul go config/laravel_file_manager.php and set 'Optimise_image'= true

for more information you can visit image-optimizer

usage

for use this package you should use bellow helper function anywhere in your project such as in your controller . this helper function
  • create html modal for show manager
  • create html modal button
  • set your options
    LFM_CreateModalFileManager($section, $options , $insert , $callback , $modal_id , $header , $button_id, $button_content)
  

that $section is Require and other input is optional .for use output of filemanager you should install jquery 3 and bootstrap 4 ;

  • $section = section name for example 'FileManager'; $options : is options you want to impplement in this package example :
    $options = ['size_file' => 100, 'max_file_number' => 30, 'true_file_extension' => ['png','jpg']];
    
$insert : with this option you can use this package as a file selector . default value is 'insert' ; $callback : this options provide javascript callback function name . for example you can get selected file and show it
  //the inserted file result is 
  {
    Manager :
        available:1,
        data :
        [{
            0 :
            {
                file :
                {
                    height : "0"
                    icon : "image"
                    id : 115
                    name : "photo2017-04-1512-45-2"
                    quality : "100"
                    size : 60187
                    type : "original"
                    user : "faramarz"
                    version : null
                    width:  "0"
                },
                full_url: "http://127.0.0.1:8000/LFM/DownloadFile/ID/115/original/404.png/100/0/0",
                message: "File with ID :115 Inserted",
                success:true
                url: "/LFM/DownloadFile/ID/115/original/404.png/100/0/0",
                full_url_large:"http://127.0.0.1:8000/LFM/DownloadFile/ID/24/small/404.png/100/300/180",
                full_url_medium:"http://127.0.0.1:8000/LFM/DownloadFile/ID/24/medium/404.png/100/300/180",
            }
        }],
        view :
        {
           'list' : 'html grid view code' ,
           'grid' : 'html grid view code' ,
           'small' :'html small view code' ,
           'medium' : 'html thumb view code' ,
           'large' : 'html large view code' 
        }
  }  
//to show above data to small view you can use this function  
function callback(result)
{
   $('#show_area_small').html(result.Manager.view.small) ;
}
  
Manager is section name . to show inserted file , this package provide 4 view . you can call these view in callback function as show in above example . inserted view is : list,grid,small,medium,large $modal_id : this option use to assign id to main modal and default is 'FileManager' . $header : this option use to set modal header .and default is 'File manager' . $button_id :this option use to assign id to button create modal and default is 'show_modal' . $button_content : this option use to assign name of button create modal and default is 'input file' .

you can access to file manager with :http://www.yourdomain.com/LFM/ShowCategories

Helper Functions

in this section we descripe some most helpfull function . you can use this functions in your project .

Save File

for save file in your project you can use LFM_SaveMultiFile and LFM_SaveSingleFile . in continue we explain two functions .

Save Multi File

this helpers use for save multi inserted files in fileable table :
  $res = LFM_SaveMultiFile($obj_model, $section, $type , $relation_name , $attach_type)
 
at first you should define your morph relation in your model . we create files relation and and put it in trait .for use this relation should use it in your model .
   public function files()
   {
          return $this->morphToMany('ArtinCMS\LFM\Models\File' , 'fileable','lfm_fileables','fileable_id','file_id')->withPivot('type')->withTimestamps() ;
   }
 
you can use this trait or create your morph relation . the $obj_model is name of your model , $section is name of your section , $type is type of file (music,picture,zip,..) its optional and default is null , $relation_name is name of morph relation and default is 'files' and with $attach_type you can select attach or sync file.

Save single File

if you want save file in one to many relation and save file_id in your table models you should use this helpers .
 LFM_SaveSingleFile($obj_model, $column_name, $section,$column_option_name)
 
The $obj_model is name of your model ($article = new Article), and column_name is name of column you want save inserted id for example 'default_img_file_id' , $section is name of section and $column_option_name is name of column that save options(for example save width,height,quality,.. in json format).

Direct upload

if you want upload file in specific path you can use this helpers .
  LFM_CreateModalUpload($section, $callback , $options, $result_area_id , $modal_id , $header , $button_id , $button_content)
  
the input helper functions is same LFM_CreateModalFileManager with diffrent $result_area_id that define area id when upload file . you should define your favorit path in options as below .
           $options = ['size_file' => 1000, 'max_file_number' =>5, 'min_file_number' => 2,'show_file_uploaded'=>'medium', 'true_file_extension' => ['png','jpg','zip'],'path'=>'myuploads/sdadeghi'];
   

load files

Load Multi Files

  for load files (for example you can use this helper functons when you want edit) you can use this helpers function
  LFM_LoadMultiFile($obj_model, $section, $type = null, $relation_name = 'files')
 
the input this helpers funciton is same to LFM_SaveMultiFile and you can get files .Remember you should use same section name when you save file and load it .you can delete our insert new file after load files .

Load Single Files

for load single file you can use this helpers as below :
 LFM_loadSingleFile($obj_model, $column_name, $section, $column_option_name = false)
 
the input this helpers function is same to LFM_SaveSingleFile . just remember you should same section name when you save and load file .you can delete our insert new file after load files .

Show Files

Show Multi File

for show inserted files you can use this helpers function .
    LFM_ShowMultiFile($obj_model, $type, $relation_name )  
 
The diffrent between this function and LFM_LoadMultiFile is , you cant delete files .

Show Single Files

for show single files you can this helpers function . remember you cant delete file .
LFM_ShowingleFile($obj_model, $column_name, $column_option_name)

Generate Link

Generate Download Link

whit this below helper function you can generate download link in anywhere of your project .
 LFM_GenerateDownloadLink($type, $id , $size_type, $default_img, $quality , $width, $height)
   
  • if you want generate file link with ID the $type = 'ID' and if you want generate by name you can use $type = 'Name'
  • $id : if you want generate link with id you should fill this item with file id and default is -1 that reffer no file id selected .
  • $size_type can pick one on of : original , large , medium , small that reffer which size you want to download
  • if image not found with $default_img you can choose image you want to show , default is '404.png'
  • $quality is quality of image , it is between 0 and 100 .
  • $width is width of result image
  • $height is height of result image

Generate Base64 Image

whit this below helper function you can create Base64 Image .
 LFM_GetBase64Image($file_id, $size_type, $not_found_img , $inline_content , $quality , $width , $height )    
this helpers config as above with different $inline_content that if it was true you can create base 64 Image .

Genrate public link

if you create shortcut from storage/public_folder in your public folder you can access file directly . as see in below box you can create public download path with this helper function
    LFM_GeneratePublicDownloadLink($path,$filename)     
that $psth is path to file and $filename is name of disc file . for example you want access the logo site picture you can upload this file in public folder and access directly with above helper function .

Custom config

if you want to have custom config you can chage config/laravel_file_manager.php file as you want .

  • 'private_middlewares' and 'public_middlewares' describe what middelware should assign to private and public route ,you can add auth and other middelware you want .
  • with 'private_route_prefix' and 'public_route_prefix' you can change prefix of private and public route .
  • 'allowed' reffer wich mime type files can upload
  • 'allowed_pic' is reffer wich mime type is recognize as picture
  • with 'size_large' ,'size_large' , 'size_large' and 'size_large' when upload file you can define size of upload deppend of type .
  • 'driver_disk' is deriver you use in your project
  • 'user_model' is path of your user model .
  • 'Optimise_image' if is true and do serve config as tell in installation section . all picture optimize before save in your storage .
  • with 'crop_chose' you can choose wich crop type use when upload file (when create large ,medium and small size of picture). you can set it one of : fit,resize and smart that 'fit' is default crop and resize resize image with due attention to config size you set and smart type is smart crop but it was slowler than other crop type .

Example

THe full Example :
Example of use LFM_SaveMultiFile and LFM_SaveSingleFile

in this example we assing zip file and user profile picture to article and save it . at first in Route :

 Route::get('/MultiFile', 'HomeController@multiSection')->name('multiSection');
 Route::get('/MultiFile/{id}', 'HomeController@multiSectionEdit')->name('multiSectionEdit');
 Route::get('/ShowMultiFile/{id}', 'HomeController@showMultiFile')->name('showMultiFile');
 Route::post('/StoreArticle', ['as' => 'StoreArticle', 'uses' => 'HomeController@StoreArticle']);
 Route::post('/StoreEditArticle', ['as' => 'StoreEditArticle', 'uses' => 'HomeController@StoreEditArticle']);
you should define your relation in article models :
use ArtinCMS\LFM\Traits\lfmFillable ;
use ArtinCMS\LFM\Models\File;
class Article extends Model
{
    use lfmFillable;
}
as you see multisection route use for save article with zip file and profile picture . in your controller :
    public function multiSection()
    {
            $optionsAttach = ['size_file' => 100, 'max_file_number' => 5, 'true_file_extension' => ['zip']];
            $option_single = ['size_file' => 100, 'max_file_number' => 1, 'true_file_extension' => ['png','jpg']];
            $attach =  LFM_CreateModalFileManager('Attach',$optionsAttach , 'insert','showAttach');
            $single = LFM_CreateModalFileManager('Single',$option_single , 'insert','showSingle');
            return view('multiSection',compact('attach','single'));
    }
    
     public function multiSectionEdit($id)
    {
        $optionsAttach = ['size_file' => 100, 'max_file_number' => 5, 'true_file_extension' => ['zip']];
        $option_single = ['size_file' => 100, 'max_file_number' => 1, 'true_file_extension' => ['png','jpg']];
        $attach =  LFM_CreateModalFileManager('Attach',$optionsAttach , 'insert','showAttach');
        $article = Article::find($id);
        $load_attch = LFM_LoadMultiFile($article,'Attach','zip','files') ;
        $single = LFM_CreateModalFileManager('Single',$option_single , 'insert','showSingle');
        $load_single = LFM_loadSingleFile($article,'default_img_file_id','Single');
        return view('multiSectionedit',compact('attach','single','load_attch','load_single','article'));
    }
    
    public function showMultiFile ($id)
    {
        $article = Article::find($id);
        $show_attch = LFM_ShowMultiFile($article,'zip','files') ;
        $show__single = LFM_ShowingleFile($article,'default_img_file_id','Single');
        return view('showMultiFile',compact('show_attch','show__single','article'));
    }
    
    public function StoreArticle(Request $request)
    {
        $article = new Article ;
        $article->title = $request->title ;
        $article->body = $request->body ;
        $article->save() ;
        $res['Attach'] = LFM_SaveMultiFile($article,'Attach','zip','files' , 'attach');
        $res['Single'] = LFM_SaveSingleFile($article,'default_img_file_id','Single','options');
        return $res ;
    }
    
    public function StoreEditArticle(Request $request)
    {
        $article =Article::find($request->id);
        $res['Attach'] = LFM_SaveMultiFile($article,'Attach','zip','files' , 'sync');
        $res['Single'] = LFM_SaveSingleFile($article,'default_img_file_id','Single','options');
        return $res ;
    }
    

and in multisection.blade.php

    {!! $attach['button'] !!}
    {!! $attach['modal_content'] !!}
   div#show_area_Attach_list
     function showAttach(res) {
        $('#show_area_Attach_list').html(res.Attach.view.list) ;
        $('#show_area_Attach_grod').html(res.Attach.view.grid) ;
        $('#show_area_Attach_small').html(res.Attach.view.small) ;
        $('#show_area_Attach_medium').html(res.Attach.view.medium) ;
        $('#show_area_Attach_large').html(res.Attach.view.large) ;
        }

and in multisectioneditl.blade.php

    {!! $attach['button'] !!}
    {!! $attach['modal_content'] !!}
    div#show_area_Attach  {!! $load_attch['view']['list'] !!}
  function showAttach(res) {
        $('#show_area_Attach').html(res.Attach.view.list,'slider') ;
    }