diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..38cdbd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/vendor +composer.phar +composer.lock +.DS_Store +.idea \ No newline at end of file diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..12c482d --- /dev/null +++ b/.styleci.yml @@ -0,0 +1,20 @@ +preset: laravel + +risky: false + +enabled: + - align_double_arrow + - align_equals + - concat_with_spaces + - ordered_class_elements + +disabled: + - concat_without_spaces + - not_operator_with_successor_space + - unalign_equals + +finder: + not-name: + - "*.md" + not-path: + - ".github" \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0c9caf2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) Sopamo GmbH + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2c0431c --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ + +

+ Laravel FilePond Backend +

+ +

+ An all in one Laravel backend for FilePond
+

+

+ We currently support the `process` and `revert` methods and are securing those via the Laravel encryption/decryption methods. +

+ +## :rocket: Be up and running in 2 minutes + +### Laravel setup + +Require this package in the `composer.json` of your Laravel project. + +```php +composer require sopamo/laravel-filepond +``` + +If you need to edit the configuration, you can publish it with: + +```php +php artisan vendor:publish --provider="Sopamo\LaravelFilepond\LaravelFilepondServiceProvider" +``` + +When you receive the serverId from Filepond (that's the value which you get via the hidden input fields) in your controller you can decode it via: + +```php +// Get the temporary path +$filepond = app(Sopamo\LaravelFilepond\Filepond::class); +$path = $filepond->getPathFromServerId($serverId); + +// Move the file from the temporary path to the final location +$finalLocation = public_path('output.jpg'); +\File::move($path, $finalLocation); +``` + +### Filepond setup + +Set at least the following Filepond configuration: + +```javascript +FilePond.setOptions({ + name: 'file', + server: 'https://yourdomain.com/filepond/api/process', +}) +``` + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9d87637 --- /dev/null +++ b/composer.json @@ -0,0 +1,37 @@ +{ + "name": "sopamo/laravel-filepond", + "description": "Laravel backend module for filepond uploads", + "license": "MIT", + "keywords": [ + "laravel", + "php", + "filepond", + "upload", + "image" + ], + "authors": [ + { + "name": "Paul Mohr", + "email": "p.mohr@sopamo.de" + } + ], + "require": { + "php": "^7.0", + "illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0", + "illuminate/contracts": "~5.5.0|~5.6.0|~5.7.0|~5.8.0" + }, + "autoload": { + "psr-4": { + "Sopamo\\LaravelFilepond\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Sopamo\\LaravelFilepond\\LaravelFilepondServiceProvider" + ] + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/config/filepond.php b/config/filepond.php new file mode 100644 index 0000000..ac98e4b --- /dev/null +++ b/config/filepond.php @@ -0,0 +1,24 @@ + 'api', + + /* + |-------------------------------------------------------------------------- + | Local Temporary Path + |-------------------------------------------------------------------------- + | + | When initially uploading the files we store them in this path + | + */ + 'temporary_files_path' => sys_get_temp_dir(), + +]; diff --git a/routes/web.php b/routes/web.php new file mode 100644 index 0000000..bad280b --- /dev/null +++ b/routes/web.php @@ -0,0 +1,7 @@ +group(function () { + Route::post('/process', 'FilepondController@upload')->name('filepond.upload'); + Route::delete('/process', 'FilepondController@delete')->name('filepond.delete'); +}); diff --git a/src/Exceptions/InvalidPathException.php b/src/Exceptions/InvalidPathException.php new file mode 100644 index 0000000..796fd18 --- /dev/null +++ b/src/Exceptions/InvalidPathException.php @@ -0,0 +1,16 @@ +filepond = $filepond; + } + + /** + * Uploads the file to the temporary directory + * and returns an encrypted path to the file + * + * @param Request $request + * @return \Illuminate\Http\Response + */ + public function upload(Request $request) + { + $file = $request->file('file'); + + $filePath = tempnam(config('filepond.temporary_files_path'), "laravel-filepond"); + $filePathParts = pathinfo($filePath); + + if(!$file->move($filePathParts['dirname'], $filePathParts['basename'])) { + return Response::make('Could not save file', 500); + } + return Response::make($this->filepond->getServerIdFromPath($filePath), 200); + } + + /** + * Takes the given encrypted filepath and deletes + * it if it hasn't been tampered with + * + * @param Request $request + * @return mixed + */ + public function delete(Request $request) { + $filePath = $this->filepond->getPathFromServerId($request->getContent()); + if(unlink($filePath)) { + return Response::make('', 200); + } else { + return Response::make('', 500); + } + } +} \ No newline at end of file diff --git a/src/LaravelFilepondServiceProvider.php b/src/LaravelFilepondServiceProvider.php new file mode 100644 index 0000000..b964e54 --- /dev/null +++ b/src/LaravelFilepondServiceProvider.php @@ -0,0 +1,48 @@ +registerRoutes(); + } + + /** + * {@inheritdoc} + */ + public function register() + { + $this->mergeConfigFrom( + $this->getConfigFile(), + 'filepond' + ); + } + + /** + * Register the Horizon routes. + * + * @return void + */ + protected function registerRoutes() + { + Route::group([ + 'prefix' => 'filepond', + 'namespace' => 'Sopamo\LaravelFilepond\Http\Controllers', + 'middleware' => config('filepond.middleware', null), + ], function () { + $this->loadRoutesFrom(__DIR__.'/../routes/web.php'); + }); + } + + /** + * @return string + */ + protected function getConfigFile(): string + { + return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'filepond.php'; + } +}