Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sopamo committed Feb 28, 2019
0 parents commit be2e530
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
composer.phar
composer.lock
.DS_Store
.idea
20 changes: 20 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

<h1 align="center">
Laravel FilePond Backend
</h1>

<p align="center">
<strong>An all in one Laravel backend for <a href="https://pqina.nl/filepond/" target="_blank">FilePond</a></strong><br>
</p>
<p>
We currently support the `process` and `revert` methods and are securing those via the Laravel encryption/decryption methods.
</p>

## :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',
})
```

37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"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
}
24 changes: 24 additions & 0 deletions config/filepond.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| API middleware
|--------------------------------------------------------------------------
|
| The middleware to append to the filepond API routes
|
*/
'middleware' => 'api',

/*
|--------------------------------------------------------------------------
| Local Temporary Path
|--------------------------------------------------------------------------
|
| When initially uploading the files we store them in this path
|
*/
'temporary_files_path' => sys_get_temp_dir(),

];
7 changes: 7 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
use Illuminate\Support\Facades\Route;

Route::prefix('api')->group(function () {
Route::post('/process', 'FilepondController@upload')->name('filepond.upload');
Route::delete('/process', 'FilepondController@delete')->name('filepond.delete');
});
16 changes: 16 additions & 0 deletions src/Exceptions/InvalidPathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Sopamo\LaravelFilepond\Exceptions;

class InvalidPathException extends \InvalidArgumentException implements LaravelFilepondException {
/**
* @param string $message
* @param int $code
*/
public function __construct(
$message = 'The given file path was invalid',
$code = 400
) {
parent::__construct($message, $code);
}
}
9 changes: 9 additions & 0 deletions src/Exceptions/LaravelFilepondException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Sopamo\LaravelFilepond\Exceptions;

use Throwable;

interface LaravelFilepondException extends Throwable
{
}
36 changes: 36 additions & 0 deletions src/Filepond.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Sopamo\LaravelFilepond;

use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Str;
use Sopamo\LaravelFilepond\Exceptions\InvalidPathException;

class Filepond {
/**
* Converts the given path into a filepond server id
*
* @param string $path
* @return string
*/
public function getServerIdFromPath($path) {
return Crypt::encryptString($path);
}

/**
* Converts the given filepond server id into a path
*
* @param string $serverId
* @return string
*/
public function getPathFromServerId($serverId) {
if(!trim($serverId)) {
throw new InvalidPathException();
}
$filePath = Crypt::decryptString($serverId);
if(!Str::startsWith($filePath, config('filepond.temporary_files_path'))) {
throw new InvalidPathException();
}
return $filePath;
}
}
58 changes: 58 additions & 0 deletions src/Http/Controllers/FilepondController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Sopamo\LaravelFilepond\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Sopamo\LaravelFilepond\Filepond;

class FilepondController extends BaseController
{

/**
* @var Filepond
*/
private $filepond;

public function __construct(Filepond $filepond)
{
$this->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);
}
}
}
48 changes: 48 additions & 0 deletions src/LaravelFilepondServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Sopamo\LaravelFilepond;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;

class LaravelFilepondServiceProvider extends ServiceProvider
{
public function boot() {
$this->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';
}
}

0 comments on commit be2e530

Please sign in to comment.