Skip to content

Commit

Permalink
add saving files from third-party resources
Browse files Browse the repository at this point in the history
  • Loading branch information
kolirt committed Oct 25, 2024
1 parent 1b67bc4 commit 97eb602
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
51 changes: 48 additions & 3 deletions src/Traits/MasterModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Kolirt\MasterModel\Traits;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

trait MasterModel
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit 97eb602

Please sign in to comment.