|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Media\Application; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Thinktomorrow\AssetLibrary\Asset; |
|
8
|
|
|
use Thinktomorrow\AssetLibrary\HasAsset; |
|
9
|
|
|
use Thinktomorrow\Chief\Fields\Types\MediaField; |
|
10
|
|
|
|
|
11
|
|
|
class ImageFieldHandler extends AbstractMediaFieldHandler |
|
12
|
|
|
{ |
|
13
|
|
|
public function handle(HasAsset $model, MediaField $field, Request $request): void |
|
14
|
|
|
{ |
|
15
|
|
|
foreach ($request->input('images.' . $field->getName(), []) as $locale => $values) { |
|
16
|
|
|
$this->handlePayload($model, $field, $locale, $values); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
$this->sort($model, $field, $request); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
protected function new(HasAsset $model, string $locale, string $type, $value): Asset |
|
23
|
|
|
{ |
|
24
|
|
|
if ($this->looksLikeAnAssetId($value)) { |
|
25
|
|
|
return $this->newExistingAsset($model, $locale, $type, $value); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$value = json_decode($value); |
|
29
|
|
|
|
|
30
|
|
|
// Slim can sometimes sent us the ajax upload response instead of the asset id. Let's make sure this is being dealt with. |
|
31
|
|
|
if(isset($value->id) && $this->looksLikeAnAssetId($value->id)) { |
|
32
|
|
|
return $this->newExistingAsset($model, $locale, $type, $value->id); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Inputted value is expected to be a slim specific json string with output of base64. |
|
36
|
|
|
$base64FileString = $value->output->image; |
|
37
|
|
|
|
|
38
|
|
|
$filename = $value->output->name; |
|
39
|
|
|
|
|
40
|
|
|
return $this->addAsset->add($model, $base64FileString, $type, $locale, $this->sluggifyFilename($filename)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function createNewAsset(HasAsset $model, string $locale, string $type, $value): Asset |
|
44
|
|
|
{ |
|
45
|
|
|
// Inputted value is expected to be a slim specific json string. |
|
46
|
|
|
$file = json_decode($value)->output->image; |
|
47
|
|
|
|
|
48
|
|
|
$filename = json_decode($value)->output->name; |
|
49
|
|
|
|
|
50
|
|
|
return $this->assetUploader->uploadFromBase64($file, $filename); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|