From 206a64ed4b8cce4e6379f9c18c9bd0389a7efd54 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Sat, 4 Mar 2017 14:04:21 +0000 Subject: [PATCH 01/26] =?UTF-8?q?Remove=20spatie=E2=80=99s=20media=20libra?= =?UTF-8?q?ry=20package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Note.php | 5 +- composer.json | 1 - composer.lock | 451 +------------------------------------------------ config/app.php | 5 - 4 files changed, 2 insertions(+), 460 deletions(-) diff --git a/app/Note.php b/app/Note.php index fe574e33..6ee5e2ff 100644 --- a/app/Note.php +++ b/app/Note.php @@ -9,15 +9,12 @@ use Illuminate\Database\Eloquent\Model; use Jonnybarnes\EmojiA11y\EmojiModifier; use League\CommonMark\CommonMarkConverter; use Illuminate\Database\Eloquent\SoftDeletes; -use Spatie\MediaLibrary\HasMedia\HasMediaTrait; -use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia; use Illuminate\Database\Eloquent\ModelNotFoundException; -class Note extends Model implements HasMedia +class Note extends Model { use Searchable; use SoftDeletes; - use HasMediaTrait; /** * The database table used by the model. diff --git a/composer.json b/composer.json index 8eab83ba..6b281798 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,6 @@ "indieauth/client": "~0.1", "ezyang/htmlpurifier": "~4.6", "league/commonmark": "^0.15.0", - "spatie/laravel-medialibrary": "^4.0", "league/flysystem-aws-s3-v3": "^1.0", "phaza/laravel-postgis": "~3.1", "lcobucci/jwt": "^3.1", diff --git a/composer.lock b/composer.lock index ebdebdff..3b259129 100644 --- a/composer.lock +++ b/composer.lock @@ -4,58 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "f3b7bcd2d79e776a84c1387f2086a5cc", + "content-hash": "601dc56ebae7a60465cb336957ea4e38", "packages": [ - { - "name": "anahkiasen/underscore-php", - "version": "2.0.0", - "source": { - "type": "git", - "url": "https://github.com/Anahkiasen/underscore-php.git", - "reference": "48f97b295c82d99c1fe10d8b0684c43f051b5580" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/Anahkiasen/underscore-php/zipball/48f97b295c82d99c1fe10d8b0684c43f051b5580", - "reference": "48f97b295c82d99c1fe10d8b0684c43f051b5580", - "shasum": "" - }, - "require": { - "doctrine/inflector": "^1.0", - "patchwork/utf8": "^1.2", - "php": ">=5.4.0" - }, - "require-dev": { - "fabpot/php-cs-fixer": "2.0.*@dev", - "phpunit/phpunit": "^4.6" - }, - "type": "library", - "autoload": { - "psr-4": { - "Underscore\\": [ - "src", - "tests" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Maxime Fabre", - "email": "ehtnam6@gmail.com" - } - ], - "description": "A redacted port of Underscore.js for PHP", - "keywords": [ - "internals", - "laravel", - "toolkit" - ], - "time": "2015-05-16T19:24:58+00:00" - }, { "name": "aws/aws-sdk-php", "version": "3.18.23", @@ -1178,68 +1128,6 @@ ], "time": "2017-01-11T17:14:49+00:00" }, - { - "name": "intervention/image", - "version": "2.3.11", - "source": { - "type": "git", - "url": "https://github.com/Intervention/image.git", - "reference": "e8881fd99b9804b29e02d6d1c2c15ee459335cf1" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/Intervention/image/zipball/e8881fd99b9804b29e02d6d1c2c15ee459335cf1", - "reference": "e8881fd99b9804b29e02d6d1c2c15ee459335cf1", - "shasum": "" - }, - "require": { - "ext-fileinfo": "*", - "guzzlehttp/psr7": "~1.1", - "php": ">=5.4.0" - }, - "require-dev": { - "mockery/mockery": "~0.9.2", - "phpunit/phpunit": "3.*" - }, - "suggest": { - "ext-gd": "to use GD library based image processing.", - "ext-imagick": "to use Imagick based image processing.", - "intervention/imagecache": "Caching extension for the Intervention Image library" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.3-dev" - } - }, - "autoload": { - "psr-4": { - "Intervention\\Image\\": "src/Intervention/Image" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Oliver Vogel", - "email": "oliver@olivervogel.net", - "homepage": "http://olivervogel.net/" - } - ], - "description": "Image handling and manipulation library with support for Laravel integration", - "homepage": "http://image.intervention.io/", - "keywords": [ - "gd", - "image", - "imagick", - "laravel", - "thumbnail", - "watermark" - ], - "time": "2017-02-04T10:37:19+00:00" - }, { "name": "jakub-onderka/php-console-color", "version": "0.1", @@ -2019,67 +1907,6 @@ "description": "Flysystem adapter for the AWS S3 SDK v3.x", "time": "2016-06-21T21:34:35+00:00" }, - { - "name": "league/glide", - "version": "1.2.1", - "source": { - "type": "git", - "url": "https://github.com/thephpleague/glide.git", - "reference": "ae6351088b148ed73ebd1270e312b07fd35dd37d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/thephpleague/glide/zipball/ae6351088b148ed73ebd1270e312b07fd35dd37d", - "reference": "ae6351088b148ed73ebd1270e312b07fd35dd37d", - "shasum": "" - }, - "require": { - "intervention/image": "^2.1", - "league/flysystem": "^1.0", - "php": "^5.4 | ^7.0", - "psr/http-message": "^1.0" - }, - "require-dev": { - "mockery/mockery": "~0.9", - "phpunit/php-token-stream": "^1.4", - "phpunit/phpunit": "~4.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.1-dev" - } - }, - "autoload": { - "psr-4": { - "League\\Glide\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jonathan Reinink", - "email": "jonathan@reinink.ca", - "homepage": "http://reinink.ca" - } - ], - "description": "Wonderfully easy on-demand image manipulation library with an HTTP based API.", - "homepage": "http://glide.thephpleague.com", - "keywords": [ - "ImageMagick", - "editing", - "gd", - "image", - "imagick", - "league", - "manipulation", - "processing" - ], - "time": "2017-01-19T01:26:54+00:00" - }, { "name": "martinbean/laravel-sluggable-trait", "version": "0.2.0", @@ -2504,65 +2331,6 @@ ], "time": "2017-03-03T14:11:40+00:00" }, - { - "name": "patchwork/utf8", - "version": "v1.3.1", - "source": { - "type": "git", - "url": "https://github.com/tchwork/utf8.git", - "reference": "30ec6451aec7d2536f0af8fe535f70c764f2c47a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/tchwork/utf8/zipball/30ec6451aec7d2536f0af8fe535f70c764f2c47a", - "reference": "30ec6451aec7d2536f0af8fe535f70c764f2c47a", - "shasum": "" - }, - "require": { - "lib-pcre": ">=7.3", - "php": ">=5.3.0" - }, - "suggest": { - "ext-iconv": "Use iconv for best performance", - "ext-intl": "Use Intl for best performance", - "ext-mbstring": "Use Mbstring for best performance", - "ext-wfio": "Use WFIO for UTF-8 filesystem access on Windows" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.3-dev" - } - }, - "autoload": { - "psr-4": { - "Patchwork\\": "src/Patchwork/" - }, - "classmap": [ - "src/Normalizer.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "(Apache-2.0 or GPL-2.0)" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - } - ], - "description": "Portable and performant UTF-8, Unicode and Grapheme Clusters for PHP", - "homepage": "https://github.com/tchwork/utf8", - "keywords": [ - "grapheme", - "i18n", - "unicode", - "utf-8", - "utf8" - ], - "time": "2016-05-18T13:57:10+00:00" - }, { "name": "phaza/laravel-postgis", "version": "3.1.1", @@ -3014,223 +2782,6 @@ "description": "A security checker for your composer.lock", "time": "2017-02-18T17:53:25+00:00" }, - { - "name": "spatie/laravel-glide", - "version": "3.1.0", - "source": { - "type": "git", - "url": "https://github.com/spatie/laravel-glide.git", - "reference": "a75f9069a04a1337e7c5308fd4975821f8177f23" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/spatie/laravel-glide/zipball/a75f9069a04a1337e7c5308fd4975821f8177f23", - "reference": "a75f9069a04a1337e7c5308fd4975821f8177f23", - "shasum": "" - }, - "require": { - "illuminate/support": "~5.3.0|~5.4.0", - "league/glide": "^1.0", - "php": "^7.0" - }, - "require-dev": { - "orchestra/testbench": "~3.3.0|~3.4.0", - "phpunit/phpunit": "5.*" - }, - "type": "library", - "autoload": { - "psr-4": { - "Spatie\\Glide\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Freek Van der Herten", - "email": "freek@spatie.be" - } - ], - "description": "Easily convert images with Glide", - "homepage": "https://github.com/spatie/laravel-glide", - "time": "2017-01-24T10:51:47+00:00" - }, - { - "name": "spatie/laravel-medialibrary", - "version": "4.13.0", - "source": { - "type": "git", - "url": "https://github.com/spatie/laravel-medialibrary.git", - "reference": "89432693fa66aa101499f0f89cfcdec51a620471" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/spatie/laravel-medialibrary/zipball/89432693fa66aa101499f0f89cfcdec51a620471", - "reference": "89432693fa66aa101499f0f89cfcdec51a620471", - "shasum": "" - }, - "require": { - "illuminate/bus": "~5.2.0|~5.3.0|~5.4.0", - "illuminate/console": "~5.2.0|~5.3.0|~5.4.0", - "illuminate/container": "~5.2.0|~5.3.28|~5.4.0", - "illuminate/contracts": "~5.2.0|~5.3.28|~5.4.0", - "illuminate/database": "~5.2.0|~5.3.0|~5.4.0", - "illuminate/filesystem": "~5.2.0|~5.3.0|~5.4.0", - "illuminate/support": "~5.2.0|~5.3.0|~5.4.0", - "league/flysystem": "^1.0.13", - "php": "^7.0", - "spatie/laravel-glide": "^3.0.0", - "spatie/pdf-to-image": "^1.2", - "spatie/string": "^2.0.0" - }, - "conflict": { - "php-ffmpeg/php-ffmpeg": "<0.6.1" - }, - "require-dev": { - "doctrine/dbal": "^2.5.2", - "league/flysystem-aws-s3-v3": "^1.0.13", - "mockery/mockery": "^0.9.4", - "orchestra/testbench": "~3.2.0|~3.3.0|~3.4.0", - "phpunit/phpunit": "^5.7.0" - }, - "suggest": { - "league/flysystem-aws-s3-v3": "Required to use AWS S3 file storage", - "php-ffmpeg/php-ffmpeg": "Required for generating video thumbnails" - }, - "type": "library", - "autoload": { - "psr-4": { - "Spatie\\MediaLibrary\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Freek Van der Herten", - "email": "freek@spatie.be", - "homepage": "https://murze.be", - "role": "Developer" - } - ], - "description": "Associate files with Eloquent models", - "homepage": "https://github.com/spatie/laravel-medialibrary", - "keywords": [ - "cms", - "laravel", - "laravel-medialibrary", - "media", - "spatie" - ], - "time": "2017-01-30T13:33:03+00:00" - }, - { - "name": "spatie/pdf-to-image", - "version": "1.2.2", - "source": { - "type": "git", - "url": "https://github.com/spatie/pdf-to-image.git", - "reference": "9a5cb264a99e87e010c65d4ece03b51f821d55bd" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/spatie/pdf-to-image/zipball/9a5cb264a99e87e010c65d4ece03b51f821d55bd", - "reference": "9a5cb264a99e87e010c65d4ece03b51f821d55bd", - "shasum": "" - }, - "require": { - "php": ">=5.5.0" - }, - "require-dev": { - "phpunit/phpunit": "4.*", - "scrutinizer/ocular": "~1.1" - }, - "type": "library", - "autoload": { - "psr-4": { - "Spatie\\PdfToImage\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Freek Van der Herten", - "email": "freek@spatie.be", - "homepage": "https://spatie.be", - "role": "Developer" - } - ], - "description": "Convert a pdf to an image", - "homepage": "https://github.com/spatie/pdf-to-image", - "keywords": [ - "convert", - "image", - "pdf", - "pdf-to-image", - "spatie" - ], - "time": "2016-12-14T15:37:00+00:00" - }, - { - "name": "spatie/string", - "version": "2.2.1", - "source": { - "type": "git", - "url": "https://github.com/spatie/string.git", - "reference": "5435f8095a6c2f4ac67cbc9a1fbc116197863144" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/spatie/string/zipball/5435f8095a6c2f4ac67cbc9a1fbc116197863144", - "reference": "5435f8095a6c2f4ac67cbc9a1fbc116197863144", - "shasum": "" - }, - "require": { - "anahkiasen/underscore-php": "^2.0", - "php": ">=5.6.0" - }, - "require-dev": { - "phpunit/phpunit": "5.*", - "scrutinizer/ocular": "~1.1" - }, - "type": "library", - "autoload": { - "files": [ - "src/string_functions.php" - ], - "psr-4": { - "Spatie\\String\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Freek Van der Herten", - "email": "freek@spatie.be", - "homepage": "https://murze.be", - "role": "Developer" - } - ], - "description": "String handling evolved", - "homepage": "https://github.com/spatie/string", - "keywords": [ - "handling", - "handy", - "spatie", - "string" - ], - "time": "2016-12-01T13:46:28+00:00" - }, { "name": "swiftmailer/swiftmailer", "version": "v5.4.6", diff --git a/config/app.php b/config/app.php index 57d672d9..1b96380e 100644 --- a/config/app.php +++ b/config/app.php @@ -215,11 +215,6 @@ return [ */ Thujohn\Twitter\TwitterServiceProvider::class, - /* - * Laravel Medialibrary - */ - Spatie\MediaLibrary\MediaLibraryServiceProvider::class, - /* * Phaza’s Postgis library */ From 21cd7186049415ab0ad30d6acb91556a808cf406 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Tue, 7 Mar 2017 18:47:29 +0000 Subject: [PATCH 02/26] Add a media endpoint route --- app/Http/Controllers/MicropubController.php | 38 +++++++++++++++++++++ routes/web.php | 1 + 2 files changed, 39 insertions(+) diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index a110bc65..7c78111f 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -225,4 +225,42 @@ class MicropubController extends Controller 'error_description' => 'No token provided with request', ], 400); } + + /** + * Process a media item posted to the media endpoint. + * + * @param Illuminate\Http\Request $request + * @return Illuminate\Http\Response + */ + public function media(Request $request) + { + //can this go in middleware + $httpAuth = $request->header('Authorization'); + if (preg_match('/Bearer (.+)/', $httpAuth, $match)) { + $token = $match[1]; + $valid = $this->tokenService->validateToken($token); + + if ($valid === null) { + return response()->json([ + 'response' => 'error', + 'error' => 'invalid_token', + 'error_description' => 'The provided token did not pass validation', + ], 400); + } + + //check post scope + + //check media valid + + //save media + + //return URL for media + } + + return response()->json([ + 'response' => 'error', + 'error' => 'no_token', + 'error_description' => 'There was no token provided with the request' + ], 400); + } } diff --git a/routes/web.php b/routes/web.php index 4b36bc18..a8268e27 100644 --- a/routes/web.php +++ b/routes/web.php @@ -115,6 +115,7 @@ Route::group(['domain' => config('url.longurl')], function () { // Micropub Endpoint Route::get('api/post', 'MicropubController@get'); Route::post('api/post', 'MicropubController@post'); + Route::post('api/media', 'MicropubController@media'); //webmention Route::get('webmention', 'WebMentionsController@get'); From bd57ce56d430bdbb2b2daa094af8ebc314f5df99 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Thu, 9 Mar 2017 22:00:29 +0000 Subject: [PATCH 03/26] Starting work on adding a media endpoint --- app/Http/Controllers/MicropubController.php | 35 +++++++++++++--- app/Media.php | 30 ++++++++++++++ app/Note.php | 10 +++++ composer.json | 27 +++++++------ composer.lock | 2 +- ..._09_155908_create_media_endpoint_table.php | 40 +++++++++++++++++++ 6 files changed, 125 insertions(+), 19 deletions(-) create mode 100644 app/Media.php create mode 100644 database/migrations/2017_03_09_155908_create_media_endpoint_table.php diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 7c78111f..5c4503a7 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -3,11 +3,13 @@ namespace App\Http\Controllers; use App\Place; +use Ramsey\Uuid\Uuid; use Illuminate\Http\Request; use App\Services\NoteService; use Illuminate\Http\Response; use App\Services\PlaceService; use App\Services\TokenService; +use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; class MicropubController extends Controller { @@ -238,9 +240,9 @@ class MicropubController extends Controller $httpAuth = $request->header('Authorization'); if (preg_match('/Bearer (.+)/', $httpAuth, $match)) { $token = $match[1]; - $valid = $this->tokenService->validateToken($token); + $tokenData = $this->tokenService->validateToken($token); - if ($valid === null) { + if ($tokenData === null) { return response()->json([ 'response' => 'error', 'error' => 'invalid_token', @@ -249,10 +251,33 @@ class MicropubController extends Controller } //check post scope + if ($tokenData->hasClaim('scope')) { + $scopes = explode(' ', $tokenData->getClaim('scope')); + if (array_search('post', $scopes) !== false) { + //check media valid + if ($request->file('file')->isValid()) { + //save media + try { + $filename = Uuid::uuid4() . $request->file->extension(); + } catch (UnsatisfiedDependencyException $e) { + return response()->json([ + 'response' => 'error', + 'error' => 'internal_server_error', + 'error_description' => 'A problem occured handling your request' + ], 500) + } + try { + $path = $request->file->storeAs('media', $filename, 's3'); + } catch(Excetion $e) { // which exception? + return response()->json([ + 'response' => 'error', + 'error' => 'service_unavailable', + 'error_description' => 'Unable to save media to S3' + ], 503) + } - //check media valid - - //save media + return $path; + } //return URL for media } diff --git a/app/Media.php b/app/Media.php new file mode 100644 index 00000000..61f94160 --- /dev/null +++ b/app/Media.php @@ -0,0 +1,30 @@ +belongsTo('App\Note'); + } +} diff --git a/app/Note.php b/app/Note.php index 6ee5e2ff..6a51a896 100644 --- a/app/Note.php +++ b/app/Note.php @@ -53,6 +53,16 @@ class Note extends Model return $this->belongsTo('App\Place'); } + /** + * Define the relationship with media. + * + * @return void + */ + public function media() + { + return $this->hasMany('App\Media'); + } + /** * We shall set a blacklist of non-modifiable model attributes. * diff --git a/composer.json b/composer.json index 6b281798..0c57c02e 100644 --- a/composer.json +++ b/composer.json @@ -6,25 +6,26 @@ "type": "project", "require": { "php": ">=7.1.0", - "laravel/framework": "5.4.*", + "ezyang/htmlpurifier": "~4.6", + "guzzlehttp/guzzle": "~6.0", + "indieauth/client": "~0.1", + "jonnybarnes/emoji-a11y": "^0.2", "jonnybarnes/indieweb": "dev-master", "jonnybarnes/webmentions-parser": "0.4.*", - "guzzlehttp/guzzle": "~6.0", - "predis/predis": "~1.0", - "thujohn/twitter": "~2.0", - "mf2/mf2": "~0.3", - "martinbean/laravel-sluggable-trait": "0.2.*", - "indieauth/client": "~0.1", - "ezyang/htmlpurifier": "~4.6", + "laravel/framework": "5.4.*", + "laravel/scout": "^3.0", + "laravel/tinker": "^1.0", + "lcobucci/jwt": "^3.1", "league/commonmark": "^0.15.0", "league/flysystem-aws-s3-v3": "^1.0", + "martinbean/laravel-sluggable-trait": "0.2.*", + "mf2/mf2": "~0.3", "phaza/laravel-postgis": "~3.1", - "lcobucci/jwt": "^3.1", - "sensiolabs/security-checker": "^4.0", - "laravel/scout": "^3.0", "pmatseykanets/laravel-scout-postgres": "^0.5.0", - "jonnybarnes/emoji-a11y": "^0.2", - "laravel/tinker": "^1.0" + "predis/predis": "~1.0", + "ramsey/uuid": "^3.5", + "sensiolabs/security-checker": "^4.0", + "thujohn/twitter": "~2.0" }, "require-dev": { "barryvdh/laravel-debugbar": "~2.0", diff --git a/composer.lock b/composer.lock index 3b259129..95aa6499 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "601dc56ebae7a60465cb336957ea4e38", + "content-hash": "3a440750c3e5403668fd82dbe7c54de0", "packages": [ { "name": "aws/aws-sdk-php", diff --git a/database/migrations/2017_03_09_155908_create_media_endpoint_table.php b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php new file mode 100644 index 00000000..92789dc0 --- /dev/null +++ b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php @@ -0,0 +1,40 @@ +uuid('id'); + $table->varchar('client_id')->nullable(); + $table->varchar('filetype'); + $table->unsignedInteger('note_id')->nullable(); + $table->timestamps(); + + $table->primary('id'); + $table->foreign('note_id')->references('id')->on('notes'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Scheme::table('media_endpoint', function (Blueprint $table) { + $table->dropForeign(['note_id']); + }); + Schema::dropIfExists('media_endpoint'); + } +} From 2c5c6e7753c1cc4c1c11b19a4df4b7a714f455cd Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 10 Mar 2017 09:58:35 +0000 Subject: [PATCH 04/26] Save uploaded files to S3 and generate a URL --- app/Http/Controllers/MicropubController.php | 12 ++++++++---- app/Media.php | 17 ++++++++++------- config/filesystems.php | 1 + ...03_09_155908_create_media_endpoint_table.php | 7 ++++--- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 5c4503a7..8b1847e4 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -258,7 +258,7 @@ class MicropubController extends Controller if ($request->file('file')->isValid()) { //save media try { - $filename = Uuid::uuid4() . $request->file->extension(); + $filename = Uuid::uuid4() . $request->file('file')->extension(); } catch (UnsatisfiedDependencyException $e) { return response()->json([ 'response' => 'error', @@ -267,16 +267,20 @@ class MicropubController extends Controller ], 500) } try { - $path = $request->file->storeAs('media', $filename, 's3'); - } catch(Excetion $e) { // which exception? + $path = $request->file('file')->storeAs('media', $filename, 's3'); + } catch (Exception $e) { // which exception? return response()->json([ 'response' => 'error', 'error' => 'service_unavailable', 'error_description' => 'Unable to save media to S3' ], 503) } + $media = new Media(); + $media->token = $token; + $media->path = $path; + $media->save(); - return $path; + return $media->url; } //return URL for media diff --git a/app/Media.php b/app/Media.php index 61f94160..31ea3a1b 100644 --- a/app/Media.php +++ b/app/Media.php @@ -13,13 +13,6 @@ class Media extends Model */ protected $table = 'media_endpoint'; - /** - * Our primaru key is a UUID value, therefore not incrementing. - * - * @var boolean - */ - protected $incrementing = false; - /** * Get the note that owns this media. */ @@ -27,4 +20,14 @@ class Media extends Model { return $this->belongsTo('App\Note'); } + + /** + * Get the URL for an S3 media file. + * + * @return string + */ + public function getUrlAttribute() + { + return config('filesystems.s3.url') . '/' . $this->path; + } } diff --git a/config/filesystems.php b/config/filesystems.php index 66120369..38b34b97 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -61,6 +61,7 @@ return [ 'secret' => env('AWS_S3_SECRET'), 'region' => env('AWS_S3_REGION'), 'bucket' => env('AWS_S3_BUCKET'), + 'url' => env('AWS_S3_URL'), ], 'media' => [ diff --git a/database/migrations/2017_03_09_155908_create_media_endpoint_table.php b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php index 92789dc0..e885881f 100644 --- a/database/migrations/2017_03_09_155908_create_media_endpoint_table.php +++ b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php @@ -14,13 +14,14 @@ class CreateMediaEndpointTable extends Migration public function up() { Schema::create('media_endpoint', function (Blueprint $table) { - $table->uuid('id'); - $table->varchar('client_id')->nullable(); - $table->varchar('filetype'); + $table->increments('id'); + $table->text('token')->nullable(); + $table->varchar('path'); $table->unsignedInteger('note_id')->nullable(); $table->timestamps(); $table->primary('id'); + $table->index('token'); $table->foreign('note_id')->references('id')->on('notes'); }); } From d33c43958a34f34e446644f92638c66be4e7d68a Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 10 Mar 2017 10:05:49 +0000 Subject: [PATCH 05/26] Use the correct method for a VARCHAR column --- .../2017_03_09_155908_create_media_endpoint_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2017_03_09_155908_create_media_endpoint_table.php b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php index e885881f..7283ae6e 100644 --- a/database/migrations/2017_03_09_155908_create_media_endpoint_table.php +++ b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php @@ -16,7 +16,7 @@ class CreateMediaEndpointTable extends Migration Schema::create('media_endpoint', function (Blueprint $table) { $table->increments('id'); $table->text('token')->nullable(); - $table->varchar('path'); + $table->string('path'); $table->unsignedInteger('note_id')->nullable(); $table->timestamps(); From 46ebd38d6ba92b6db0b19e037f294bfd1643cc70 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 10 Mar 2017 10:06:41 +0000 Subject: [PATCH 06/26] Was trying to define the primary key twice --- .../migrations/2017_03_09_155908_create_media_endpoint_table.php | 1 - 1 file changed, 1 deletion(-) diff --git a/database/migrations/2017_03_09_155908_create_media_endpoint_table.php b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php index 7283ae6e..a2388065 100644 --- a/database/migrations/2017_03_09_155908_create_media_endpoint_table.php +++ b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php @@ -20,7 +20,6 @@ class CreateMediaEndpointTable extends Migration $table->unsignedInteger('note_id')->nullable(); $table->timestamps(); - $table->primary('id'); $table->index('token'); $table->foreign('note_id')->references('id')->on('notes'); }); From 63a8f49b644d4863c5a011125a978e2e3a3f987b Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 10 Mar 2017 10:21:51 +0000 Subject: [PATCH 07/26] Remove more media-library routines that I missed earlier --- app/Http/Controllers/NotesController.php | 13 ------------- resources/views/templates/note.blade.php | 5 ----- 2 files changed, 18 deletions(-) diff --git a/app/Http/Controllers/NotesController.php b/app/Http/Controllers/NotesController.php index 45915796..06a68e4c 100644 --- a/app/Http/Controllers/NotesController.php +++ b/app/Http/Controllers/NotesController.php @@ -58,12 +58,6 @@ class NotesController extends Controller $note->place->icon ); } - $photoURLs = []; - $photos = $note->getMedia(); - foreach ($photos as $photo) { - $photoURLs[] = $photo->getUrl(); - } - $note->photoURLs = $photoURLs; } $homepage = ($request->path() == '/'); @@ -164,13 +158,6 @@ class NotesController extends Controller ); } - $photoURLs = []; - $photos = $note->getMedia(); - foreach ($photos as $photo) { - $photoURLs[] = $photo->getUrl(); - } - $note->photoURLs = $photoURLs; - return view('notes.show', compact('note', 'replies', 'reposts', 'likes')); } diff --git a/resources/views/templates/note.blade.php b/resources/views/templates/note.blade.php index d5ff1ae7..625af985 100644 --- a/resources/views/templates/note.blade.php +++ b/resources/views/templates/note.blade.php @@ -8,11 +8,6 @@
{!! $note->note !!} - @if(count($note->photoURLs) > 0) - @foreach($note->photoURLs as $photoURL) - - @endforeach - @endif