Passed
Push — dependabot/composer/thinktomor... ( 01bc9e...e09717 )
by
unknown
09:01
created

UploadMedia::sortFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
namespace Thinktomorrow\Chief\Media;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\UploadedFile;
7
use Thinktomorrow\AssetLibrary\Asset;
8
use Thinktomorrow\AssetLibrary\HasAsset;
9
use Thinktomorrow\AssetLibrary\Application\AddAsset;
10
use Thinktomorrow\AssetLibrary\Application\SortAssets;
11
use Thinktomorrow\AssetLibrary\Application\DetachAsset;
12
use Thinktomorrow\AssetLibrary\Application\ReplaceAsset;
13
use Thinktomorrow\AssetLibrary\Application\AssetUploader;
14
15
class UploadMedia
16
{
17
    /**
18
     * Upload from base64encoded files, usually
19
     * coming from slim upload component
20
     *
21
     * @param HasAsset $model
22
     * @param array $files_by_type
23
     * @param array $files_order_by_type
24
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
25
     */
26
    public function fromUploadComponent(HasAsset $model, array $files_by_type, array $files_order_by_type)
27
    {
28
        ini_set('memory_limit', '256M');
29
        $files_by_type = $this->sanitizeFilesParameter($files_by_type);
30
        $files_order_by_type = $this->sanitizeFilesOrderParameter($files_order_by_type);
31
        $this->validateParameters($files_by_type, $files_order_by_type);
32
33
        // When no files are uploaded, we still would like to sort our assets duh
34
        if (empty($files_by_type)) {
35
            foreach ($files_order_by_type as $type => $files) {
36
                app(SortAssets::class)->handle($model, $type, $files);
37
            }
38
39
            return;
40
        }
41
42
        foreach ($files_by_type as $type => $files_by_locale) {
43
            foreach ($files_by_locale as $locale => $files) {
44
                $this->validateFileUploads($files);
45
                
46
                $fileIdsCollection = $files_order_by_type[$type] ?? [];
47
48
                $this->addFiles($model, $type, $files, $fileIdsCollection, $locale);
49
                $this->replaceFiles($model, $files);
50
                $this->removeFiles($model, $files);
51
            }
52
            app(SortAssets::class)->handle($model, $type, $fileIdsCollection ?? []);
53
        }
54
    }
55
56
    private function addFiles(HasAsset $model, string $type, array $files, array &$files_order, string $locale = null)
57
    {
58
        if (!$this->actionExists($files, 'new')) {
59
            return;
60
        }
61
62
        foreach ($files['new'] as $id => $file) {
63
            if (!$file) {
64
                continue;
65
            }
66
            
67
            $this->addFile($model, $type, $file, $files_order, $locale);
68
        }
69
    }
70
71
    /**
72
     * @param HasAsset $model
73
     * @param array $files
74
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
75
     */
76
    private function replaceFiles(HasAsset $model, array $files)
77
    {
78
        if (!$this->actionExists($files, 'replace')) {
79
            return;
80
        }
81
82
        foreach ($files['replace'] as $id => $file) {
83
            if (!$file) {
84
                continue;
85
            }
86
87
            $asset = AssetUploader::uploadFromBase64(json_decode($file)->output->image, json_decode($file)->output->name);
88
            app(ReplaceAsset::class)->handle($model, $id, $asset->id);
89
        }
90
    }
91
92
    /**
93
     * @param HasAsset $model
94
     * @param array $files
95
     */
96
    private function removeFiles(HasAsset $model, array $files)
97
    {
98
        if (!$this->actionExists($files, 'delete')) {
99
            return;
100
        }
101
102
        app(DetachAsset::class)->detach($model, $files['delete']);
103
    }
104
105
    private function actionExists(array $files, string $action)
106
    {
107
        return (isset($files[$action]) && is_array($files[$action]) && !empty($files[$action]));
108
    }
109
110
    private function addFile(HasAsset $model, string $type, $file, array &$files_order, $locale = null)
111
    {
112
        if (isset(json_decode($file)->output)) {
113
            $image_name = json_decode($file)->output->name;
114
            $asset      = app(AddAsset::class)->add($model, json_decode($file)->output->image, $type, $locale, $this->sluggifyFilename($image_name));
0 ignored issues
show
Unused Code introduced by
The assignment to $asset is dead and can be removed.
Loading history...
115
        } else {
116
            if ($file instanceof UploadedFile) {
117
                $image_name = $file->getClientOriginalName();
118
                $asset      = app(AddAsset::class)->add($model, $file, $type, $locale, $this->sluggifyFilename($image_name));
119
120
                // New files are passed with their filename (instead of their id)
121
                // For new files we will replace the filename with the id.
122
                if (false !== ($key = array_search($image_name, $files_order))) {
123
                    $files_order[$key] = (string) $asset->id;
124
                }
125
            } else {
126
                $file       = Asset::findOrFail($file);
127
                $asset      = app(AddAsset::class)->add($model, $file, $type, $locale);
128
            }
129
        }
130
    }
131
132
    /**
133
     * @param $filename
134
     * @return string
135
     */
136
    private function sluggifyFilename($filename): string
137
    {
138
        $extension = substr($filename, strrpos($filename, '.') + 1);
139
        $filename  = substr($filename, 0, strrpos($filename, '.'));
140
        $filename  = Str::slug($filename) . '.' . $extension;
141
142
        return $filename;
143
    }
144
145
    /**
146
     * @param $files
147
     * @throws FileTooBigException
148
     */
149
    private function validateFileUploads($files): void
150
    {
151
        foreach ($files as $_files) {
152
            foreach ($_files as $file) {
153
                if ($file instanceof UploadedFile && !$file->isValid()) {
154
                    if ($file->getError() == UPLOAD_ERR_INI_SIZE) {
155
                        throw new FileTooBigException(
0 ignored issues
show
Bug introduced by
The type Thinktomorrow\Chief\Media\FileTooBigException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
156
                            'Cannot upload file because it exceeded the allowed upload_max_filesize: upload_max_filesize is smaller than post size. ' .
157
                            'upload_max_filesize: ' . (int)ini_get('upload_max_filesize') . 'MB, ' .
158
                            'post_max_size: ' . (int)(ini_get('post_max_size')) . 'MB'
159
                        );
160
                    }
161
                }
162
            }
163
        }
164
    }
165
166
    private function validateParameters(array $files_by_type, array $files_order_by_type)
0 ignored issues
show
Unused Code introduced by
The parameter $files_order_by_type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

166
    private function validateParameters(array $files_by_type, /** @scrutinizer ignore-unused */ array $files_order_by_type)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168
        $actions = ['new', 'replace', 'delete'];
169
        foreach ($files_by_type as $type => $files) {
170
            foreach ($files as $locale => $_files) {
171
                if (!in_array($locale, config('translatable.locales'))) {
172
                    throw new \InvalidArgumentException('Corrupt file payload. key is expected to be a valid locale [' . implode(',', config('translatable.locales', [])). ']. Instead [' . $locale . '] is given.');
173
                }
174
175
                if (!is_array($_files)) {
176
                    throw new \InvalidArgumentException('A valid files entry should be an array of files, key with either [new, replace or delete]. Instead a ' . gettype($_files) . ' is given.');
177
                }
178
179
                foreach ($_files as $action => $file) {
180
                    if (!in_array($action, $actions)) {
181
                        throw new \InvalidArgumentException('A valid files entry should have a key of either ['.implode(',', $actions).']. Instead ' . $action . ' is given.');
182
                    }
183
                }
184
            }
185
        }
186
    }
187
188
    private function sanitizeFilesParameter(array $files_by_type): array
189
    {
190
        $defaultLocale = config('app.fallback_locale');
191
192
        foreach ($files_by_type as $type => $files) {
193
            foreach ($files as $locale => $_files) {
194
                if (!in_array($locale, config('translatable.locales'))) {
195
                    unset($files_by_type[$type][$locale]);
196
197
                    if (!isset($files_by_type[$type][$defaultLocale])) {
198
                        $files_by_type[$type][$defaultLocale] = [];
199
                    }
200
201
                    $files_by_type[$type][$defaultLocale][$locale] = $_files;
202
                }
203
            }
204
        }
205
206
        return $files_by_type;
207
    }
208
209
    private function sanitizeFilesOrderParameter(array $files_order_by_locale): array
210
    {
211
        foreach ($files_order_by_locale as $locale => $fileIdsCollection) {
212
            foreach ($fileIdsCollection as $type => $commaSeparatedFileIds) {
213
                $type = str_replace("files-", "", $type);
214
                $files_order_by_type[$type][] = explode(',', $commaSeparatedFileIds);
215
                $files_order_by_type[$type] = collect($files_order_by_type)->flatten()->unique()->toArray();
216
            }
217
        }
218
219
        return $files_order_by_type ?? $files_order_by_locale;
220
    }
221
}
222