From 97eb6026424863f98164ab0aa1ebc4da727f7acc Mon Sep 17 00:00:00 2001 From: kolirt Date: Sat, 26 Oct 2024 01:29:47 +0300 Subject: [PATCH] add saving files from third-party resources --- README.md | 25 +++++++++++++++++++ composer.json | 2 +- src/Traits/MasterModel.php | 51 +++++++++++++++++++++++++++++++++++--- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 43cbb89..4fa1e5c 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ code complexity and enhancing performance. - [Console commands](#console-commands) - [Use cases](#use-cases) - [Saving files](#saving-files) + - [Saving file from HTTP request](#saving-file-from-http-request) - [Deleting files](#deleting-files) - [Saving `HasOne`, `MorphOne` relations](#saving-hasone-morphone-relations) - [Saving `HasMany`, `MorphMany` relations](#saving-hasmany-morphmany-relations) @@ -123,6 +124,30 @@ class Item extends Model ``` +### Saving files from third-party resources +You no longer need to worry about saving files from third-party resources, just save the `response` and MasterModel will save everything for you + +```php +class ExampleController extends Controller +{ + public function index($id) + { + $file1_url = 'https://png.pngtree.com/png-clipart/20230126/original/pngtree-fresh-red-apple-png-image_8930987.png'; + $response = \Illuminate\Support\Facades\Http::get($file1_url); + + $file2_url = 'https://cubanvr.com/wp-content/uploads/2023/07/ai-image-generators.webp'; + $client = new \GuzzleHttp\Client(); + $response2 = $client->get($file2_url); + + Item::create([ + 'image' => $response, + 'image2' => $response2 + ]); + } +} +``` + + ### Deleting files You can delete files by setting the field to `null` diff --git a/composer.json b/composer.json index 46dd312..ccee4b8 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ ], "homepage": "https://github.com/kolirt/laravel-master-model", "license": "MIT", - "version": "4.0.4", + "version": "4.0.5", "authors": [ { "name": "kolirt" diff --git a/src/Traits/MasterModel.php b/src/Traits/MasterModel.php index 2720db9..125420b 100644 --- a/src/Traits/MasterModel.php +++ b/src/Traits/MasterModel.php @@ -3,7 +3,6 @@ namespace Kolirt\MasterModel\Traits; use Illuminate\Database\Eloquent\SoftDeletes; -use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; trait MasterModel @@ -111,11 +110,53 @@ public function fill(array $attributes) public function save(array $options = []) { - foreach ($this->fillableFromArray($this->getAttributes()) as $key => $value) { $origin_value = $this->getOriginal($key); - if ($value instanceof UploadedFile) { + + if ($value instanceof \Illuminate\Http\Client\Response) { + if ($value->successful()) { + $stream = tmpfile(); + fwrite($stream, $value->body()); + $uri = stream_get_meta_data($stream)['uri']; + + $value = new \Illuminate\Http\UploadedFile( + $uri, + basename($uri), + $value->getHeaderLine('Content-Type'), + null, + true + ); + + $this->setAttribute($key, $value); + } else { + $this->setAttribute($key, $origin_value); + $value = $origin_value; + } + } + + if ($value instanceof \GuzzleHttp\Psr7\Response) { + if ($value->getStatusCode() >= 200 && $value->getStatusCode() < 300) { + $stream = tmpfile(); + fwrite($stream, $value->getBody()->getContents()); + $uri = stream_get_meta_data($stream)['uri']; + + $value = new \Illuminate\Http\UploadedFile( + $uri, + basename($uri), + $value->getHeaderLine('Content-Type'), + null, + true + ); + + $this->setAttribute($key, $value); + } else { + $this->setAttribute($key, $origin_value); + $value = $origin_value; + } + } + + if ($value instanceof \Illuminate\Http\UploadedFile) { if ($value->isValid()) { $disk_name = $this->getDisk($key); @@ -127,9 +168,13 @@ public function save(array $options = []) 'value' => $stored_file_path, 'old_value' => $origin_value ]; + } else { + $this->setAttribute($key, $origin_value); + $value = $origin_value; } } + if ($value != $origin_value) { if (is_stored_file($origin_value)) { [$origin_disk_name, $origin_value] = explode(':', $origin_value);