ijin82 / flysystem-webdav
Laravel 5 service provider for Flysystem Webdav adapter (league/flysystem-webdav)
Installs: 5 147
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.5.0
- league/flysystem-webdav: ^1.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
README
Flysystem Webdav service provider for Laravel 5
What is it for?
That is a ready-to-use service provider for flysystem-webdav
Why i need that?
You don't need that for sure, you can change service provider by yourself whatever you like (with installed flysystem-webdav)
Okay, I'm quite lazy guy, how to install this solution?
Now you have to be patient. It takes a couple minutes.
- This is nginx-related solution (if someone ready to test Apache or lighttpd or etc. - welcome, let's extend description), so we have to set up nginx at first
sudo apt-get install nginx nginx-common nginx-extras
- Set up nginx webdav host at first.
v1. Host for webdav server
server { # ssl #listen 443; #ssl on; #ssl_certificate /path/to/pem/file.pem; #ssl_certificate_key /path/to/key/file.key; listen 80; server_name host.name.com; location / { limit_except GET { auth_basic "Auth"; auth_basic_user_file /path/to/htpasswd/file/.htpasswd; } # root folder root /path/to/webdav/root; # max upload file size client_max_body_size 100m; # chmod for uploaded files dav_access group:rw all:r; # methods for upload dav_methods PUT DELETE MKCOL COPY MOVE; dav_ext_methods PROPFIND OPTIONS; # create full file path on upload (no need to create folders) create_full_put_path on; # autoindex for webdav autoindex off; autoindex_exact_size off; autoindex_localtime on; charset utf-8; } }
Here we have nginx config (v1) for webdav host without subfolder for upload. Create path whatever you like from root. For all http requests that is not GET
type, service user have to be authorized (limit_except directive).
REM
If you have never used .htpasswd
before, you have to install apache utils
sudo apt-get install apache2-utils
And then go to manual page
v2. Host for webdav server with subfolder
server { # ssl #listen 443; #ssl on; #ssl_certificate /path/to/pem/file.pem; #ssl_certificate_key /path/to/key/file.key; listen 80; server_name host.name.com; location / { root /path/to/webdav/root/or/another/folder; } location /upload { limit_except GET { auth_basic "Auth"; auth_basic_user_file /path/to/htpasswd/file/.htpasswd; } # root folder root /path/to/webdav/root; # max upload file size client_max_body_size 100m; # chmod for uploaded files dav_access group:rw all:r; # methods for upload dav_methods PUT DELETE MKCOL COPY MOVE; dav_ext_methods PROPFIND OPTIONS; # create full file path on upload (no need to create folders) create_full_put_path on; # autoindex for webdav autoindex off; autoindex_exact_size off; autoindex_localtime on; charset utf-8; } }
Here we have nginx config (v2) for webdav host with subfolder for upload. Create path whatever you like from subfolder (upload
folder name is not needed, change that if you like).
- Now we have to create folder for webdav service with same access rights as nginx have
mkdir /path/to/webdav/root chown -R www-data:www-data /path/to/webdav/root
- Ok, now test your nginx config and restart when ready
sudo nginx -t sudo nginx -s relaod
- We've done with nginx, let's get deal with PHP side. Let's install this module for your Laravel app.
composer require ijin82/flysystem-webdav
- Now we have to set up service provider. Open your
config/app.php
and add new provider to providers section.
return [ //... 'providers' => [ //... Ijin82\Flysystem\Webdav\WebdavServiceProvider::class, //... ], //... ];
- Let's configure new filesystem. Open your
config/filesystems.php
and add new fs config like this.
return [ //... 'avatars' => [ 'driver' => 'webdav', 'baseUri' => 'http://host.name.com', 'path_prefix' => 'avatar/', 'path_alias' => '', 'userName' => 'webdav_user_login', 'password' => 'webdav_user_password', ], //... ];
For sure, you have to use env
hepler for config, here is just an example config without it, just fyi.
Example description:
driver
- new webdav driver (check service provider source code)baseUri
- your webdav host namepath_prefix
- files folder prefix. If you plan to upload files for nginx configv2
, then you have to prefix your folder name with upload folder name, meaningupload/avatar/
forv2
nginx config, and simplyavatar/
forv1
config, because this parameter using for file upload and inv2
config we have to hit upload folder according to your nginx config.path_alias
- OPTIONAL parameter, in case if you like to access your upladed files by short path, for example your original upload prefix isupload/client/avatars/
and you want client-side file path looks like/av/file1.jpg
. Then you have to create symlink fromupload/client/avatars/
toav/
folder on your server (under your nginx root) and set uppath_alias
parameter asav/
. By default, yourpath_prefix
parameter will be user for uri generation.userName
- user name from your.htpasswd
used for file uploadpassword
- user password from your.htpasswd
used for file upload
Upload example
public function avatarUpload(Request $request) { //... $file = $request->file('avatar_file'); //... check file type, build name, save name in DB, whatever you like $fileName = $file->getClientOriginalName(); // for example // https://laravel.com/docs/5.5/requests#storing-uploaded-files $file->storeAs('subfolder-name-or-empty', $fileName, ['disk' => 'avatars']); //... // save file name or logic to build that //... }
Get file url, blade example
<a href="{{ Storage::disk('avatars')->url($fileName) }}" target="_blank">.{{ $ext }}</a>
Get file url, code example
//... fileName logic $fileUrl = Storage::disk('avatars')->url($fileName);
Delete file example
Storage::disk('avatars')->delete($fileName);
Delete folder example
// WARNING, all files inside that also will be deleted Storage::disk('avatars')->deleteDir('dir-name/or/path');
Check out original flysystem docs
Feel free to send pull requests and bug reports.