Completed
Push — master ( 0ba88a...6928ec )
by Lorenzo
02:23
created

UploadOptions::setCreateDirModeMask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Padosoft\Uploadable;
4
5
use Illuminate\Support\Facades\Storage;
6
use Padosoft\Io\DirHelper;
7
8
class UploadOptions
9
{
10
    /**
11
     * upload Create Dir Mode file Mask (default '0755')
12
     * @var string
13
     */
14
    public $uploadCreateDirModeMask = '0755';
15
16
    /**
17
     * If set to true, a $model->uploadFileNameSuffixSeparator.$model->id suffx are added to upload file name.
18
     * Ex.:
19
     * original uploaded file name: pippo.jpg
20
     * final name: 'pippo'.$model->uploadFileNameSuffixSeparator.$model->id.'jpg'
21
     * @var bool
22
     */
23
    public $appendModelIdSuffixInUploadedFileName = true;
24
25
    /**
26
     * Suffix separator to generate new file name
27
     * @var string
28
     */
29
    public $uploadFileNameSuffixSeparator = '_';
30
31
    /**
32
     * Array of upload attributes
33
     * Ex.:
34
     * public $uploads = ['image', 'image_mobile'];
35
     * @var array
36
     */
37
    public $uploads = [];
38
39
    /**
40
     * Array of Mime type string.
41
     * Ex. (default):
42
     * public $uploadsMimeType = [
43
     * 'image/gif',
44
     * 'image/jpeg',
45
     * 'image/png',
46
     * ];
47
     * A full listing of MIME types and their corresponding extensions may be found
48
     * at the following location: http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
49
     * @var array
50
     */
51
    public $uploadsMimeType = [
52
        'image/gif',
53
        'image/jpeg',
54
        'image/png',
55
    ];
56
57
    /**
58
     * upload base path relative to $storageDiskName root folder
59
     * @var string
60
     */
61
    public $uploadBasePath;
62
63
    /**
64
     * The default storage disk name
65
     * used for default upload base path
66
     * default: public
67
     * @var string
68
     */
69
    public $storageDiskName = 'local';
70
71
    /**
72
     * An associative array of 'attribute_name' => 'uploadBasePath'
73
     * set an attribute name here to override its default upload base path
74
     * The path is relative to $storageDiskName root folder.
75
     * Ex.:
76
     * public $uploadPaths = ['image' => 'product', 'image_thumb' => 'product/thumb'];
77
     * @var array
78
     */
79
    public $uploadPaths = [];
80
81
    /**
82
     * The laravel storage disk to store uploaded files
83
     * @var \Illuminate\Filesystem\FilesystemAdapter
84
     */
85
    public $storage;
86
87
88
    /**
89
     * @return UploadOptions
90
     */
91
    public static function create(): UploadOptions
92
    {
93
        return new static();
94
    }
95
96
    /**
97
     * upload Create Dir Mode file Mask (Ex.: '0755')
98
     * @param string $mask
99
     * @return UploadOptions
100
     */
101
    public function setCreateDirModeMask(string $mask): UploadOptions
102
    {
103
        $this->uploadCreateDirModeMask = $mask;
104
105
        return $this;
106
    }
107
108
    /**
109
     * $model->uploadFileNameSuffixSeparator.$model->id suffx are added to upload file name.
110
     * Ex.:
111
     * original uploaded file name: pippo.jpg
112
     * final name: 'pippo'.$model->uploadFileNameSuffixSeparator.$model->id.'jpg'
113
     */
114
    public function appendModelIdSuffixInFileName(): UploadOptions
115
    {
116
        $this->appendModelIdSuffixInUploadedFileName = true;
117
118
        return $this;
119
    }
120
121
    /**
122
     */
123
    public function dontAppendModelIdSuffixInFileName(): UploadOptions
124
    {
125
        $this->appendModelIdSuffixInUploadedFileName = false;
126
127
        return $this;
128
    }
129
130
    /**
131
     * Suffix separator to generate new file name
132
     * @param string $suffix
133
     * @return UploadOptions
134
     */
135
    public function setFileNameSuffixSeparator(string $suffix): UploadOptions
136
    {
137
        $this->uploadFileNameSuffixSeparator = $suffix;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Array of upload attributes
144
     * Ex.: ['image', 'image_mobile'];
145
     * @param array $attributes
146
     * @return UploadOptions
147
     */
148
    public function setUploadsAttributes(array $attributes): UploadOptions
149
    {
150
        $this->uploads = $attributes;
151
152
        return $this;
153
    }
154
155
    /**
156
     * Array of Mime type string.
157
     * Ex.:
158
     * [
159
     * 'image/gif',
160
     * 'image/jpeg',
161
     * 'image/png',
162
     * ];
163
     * A full listing of MIME types and their corresponding extensions may be found
164
     * at the following location: http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
165
     * @param array $mime
166
     * @return UploadOptions
167
     */
168
    public function setMimeType(array $mime): UploadOptions
169
    {
170
        $this->uploadsMimeType = $mime;
171
172
        return $this;
173
    }
174
175
    /**
176
     * upload base path.
177
     * Ex.: public/upload/news
178
     * @param string $path
179
     * @return UploadOptions
180
     */
181
    public function setUploadBasePath(string $path): UploadOptions
182
    {
183
        $this->uploadBasePath = DirHelper::canonicalize($path);
184
185
        return $this;
186
    }
187
188
    /**
189
     * An associative array of 'attribute_name' => 'uploadBasePath'
190
     * set an attribute name here to override its default upload base path
191
     * The path is absolute or relative to public_folder() folder.
192
     * Ex.:
193
     * public $uploadPaths = ['image' => 'product', 'image_thumb' => 'product/thumb'];
194
     * @param array $attributesPaths
195
     * @return UploadOptions
196
     */
197
    public function setUploadPaths(array $attributesPaths): UploadOptions
198
    {
199
        array_map(function ($v) {
200
            return $v == '' ? $v : DirHelper::canonicalize($v);
201
        }, $attributesPaths);
202
203
        $this->uploadPaths = $attributesPaths;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Set a Storage Disk name
210
     * @param string $diskName
211
     * @return UploadOptions
212
     */
213
    public function setStorageDisk(string $diskName)
214
    {
215
        $this->storage = Storage::disk($diskName);
216
217
        return $this;
218
    }
219
220
    /**
221
     * Get a Storage Disk
222
     * @return \Illuminate\Filesystem\FilesystemAdapter
223
     */
224
    public function getStorage() : \Illuminate\Filesystem\FilesystemAdapter
225
    {
226
        return $this->storage;
227
    }
228
229
    /**
230
     * Get the options for generating the upload.
231
     */
232
    public function getUploadOptionsDefault(): UploadOptions
233
    {
234
        return UploadOptions::create()
235
            ->setCreateDirModeMask('0755')
236
            ->appendModelIdSuffixInFileName()
237
            ->setFileNameSuffixSeparator('_')
238
            ->setUploadsAttributes(['image', 'image_mobile'])
239
            ->setMimeType([
240
                'image/gif',
241
                'image/jpeg',
242
                'image/png',
243
            ])
244
            ->setUploadBasePath( 'upload/')
245
            ->setUploadPaths([])
246
            ->setStorageDisk('');
247
    }
248
}
249