Passed
Branch feature/cropping (36d499)
by Philippe
43:28
created

AssetUploader::uploadFromBase64()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.3755

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 6
cts 11
cp 0.5455
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
crap 2.3755
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\File;
7
use Illuminate\Http\UploadedFile;
8
9
class AssetUploader extends Model
10
{
11
    /**
12
     * Uploads the file/files or asset by creating the
13
     * asset that is needed to upload the files too.
14
     *
15
     * @param $files
16
     * @param bool $keepOriginal
17
     * @return \Illuminate\Support\Collection|null|Asset
18
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
19
     */
20 42
    public static function upload($files, $keepOriginal = false)
21
    {
22 42
        $list = collect([]);
23
24 42
        if ($files instanceof Asset) {
25 5
            return $files;
26 42
        } elseif (is_array($files)) {
27 6
            collect($files)->each(function ($file) use ($list, $keepOriginal) {
28 6
                if ($file instanceof Asset) {
29 2
                    $list->push($file);
30
                } else {
31 5
                    $asset = new Asset();
32 5
                    $asset->save();
33 5
                    $list->push(self::uploadToAsset($file, $asset, $keepOriginal));
34
                }
35 6
            });
36
37 6
            return $list;
38
        }
39
40 38
        $asset = new Asset();
41 38
        $asset->save();
42
43 38
        if (! ($files instanceof File) && ! ($files instanceof UploadedFile)) {
44 1
            return;
45
        }
46
47 37
        return self::uploadToAsset($files, $asset, $keepOriginal);
48
    }
49
50
    /**
51
     * Uploads the file/files or asset by creating the
52
     * asset that is needed to upload the files too.
53
     *
54
     * @param $files
55
     * @param null $filename
56
     * @param bool $keepOriginal
57
     * @return \Illuminate\Support\Collection|null|Asset
58
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
59
     */
60 1
    public static function uploadFromBase64($files, $filename = null, $keepOriginal = false)
61
    {
62 1
        $list = collect([]);
63
64 1
        if (is_array($files)) {
65
            collect($files)->each(function ($file) use ($list, $filename, $keepOriginal) {
66
                $asset = Asset::create();
0 ignored issues
show
Bug introduced by
The method create() does not exist on Thinktomorrow\AssetLibrary\Models\Asset. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67
68
                $list->push(self::uploadBase64ToAsset($file, $asset, $filename, $keepOriginal));
69
            });
70
71
            return $list;
72
        }
73
74 1
        $asset = new Asset();
75 1
        $asset->save();
76
77 1
        return self::uploadBase64ToAsset($files, $asset, $filename, $keepOriginal);
78
    }
79
80
    /**
81
     * Uploads the given file to this instance of asset
82
     * and sets the dimensions as a custom property.
83
     *
84
     * @param $files
85
     * @param Asset $asset
86
     * @param null $filename
87
     * @param bool $keepOriginal
88
     * @return null|Asset
89
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
90
     */
91 41
    public static function uploadToAsset($files, $asset, $filename = null, $keepOriginal = false): ?Asset
92
    {
93 41
        $customProps = [];
94 41
        if (self::isImage($files)) {
95 38
            $customProps['dimensions'] = getimagesize($files)[0].' x '.getimagesize($files)[1];
96
        }
97
98 41
        $fileAdd    = $asset->addMedia($files)->withCustomProperties($customProps);
99 41
        if ($keepOriginal) {
100
            $fileAdd = $fileAdd->preservingOriginal();
101
        }
102
103 41
        if($filename)
104
        {
105 1
            $fileAdd->setName(substr($filename, 0, strpos($filename, '.')));
106 1
            $fileAdd->setFileName($filename);
107
        }
108
109 41
        $fileAdd->toMediaCollection();
110
111 41
        return $asset->load('media');
112
    }
113
114
    /**
115
     * Uploads the given file to this instance of asset
116
     * and sets the dimensions as a custom property.
117
     *
118
     * @param $file
119
     * @param Asset $asset
120
     * @param null $filename
121
     * @param bool $keepOriginal
122
     * @return null|Asset
123
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
124
     * @internal param $files
125
     */
126 1
    public static function uploadBase64ToAsset($file, $asset, $filename = null, $keepOriginal = false): ?Asset
127
    {
128
        //TODO find a way to save the dimensions for base64 uploads
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
129
//        $customProps = [];
130
//        $customProps['dimensions'] = getimagesize($file)[0].' x '.getimagesize($file)[1];
131
132
//        $fileAdd    = $asset->addMediaFromBase64($file)->withCustomProperties($customProps);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
133 1
        $fileAdd    = $asset->addMediaFromBase64($file);
134 1
        if ($keepOriginal) {
135
            $fileAdd = $fileAdd->preservingOriginal();
136
        }
137
138 1
        if($filename)
139
        {
140 1
            $fileAdd->setName(substr($filename, 0, strpos($filename, '.')));
141 1
            $fileAdd->setFileName($filename);
142
        }
143
144 1
        $fileAdd->toMediaCollection();
145
146 1
        return $asset->load('media');
147
    }
148
149
    /**
150
     * @param $file
151
     * @return bool
152
     */
153 41
    private static function isImage($file): bool
154
    {
155 41
        return str_before($file->getMimetype(), '/') === 'image';
156
    }
157
}
158