1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SaasReady\Services\FileManager; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
|
|
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
|
|
|
|
8
|
|
|
use SaasReady\Models\File; |
9
|
|
|
|
10
|
|
|
class FileManager |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Upload the file |
14
|
|
|
*/ |
15
|
|
|
public function upload(UploadOption $uploadOption): ?File |
16
|
|
|
{ |
17
|
|
|
$driver = $uploadOption->driver ?: Storage::getDefaultDriver(); |
18
|
|
|
$storage = Storage::disk($driver); |
19
|
|
|
|
20
|
|
|
$file = $uploadOption->file; |
21
|
|
|
$newFileName = $uploadOption->newFileName ?: Str::orderedUuid() . '.' . $file->getExtension(); |
22
|
|
|
$storagePath = $uploadOption->storePath ?: 'files/'; |
23
|
|
|
|
24
|
|
|
// upload |
25
|
|
|
$uploadResult = $storage->putFileAs( |
26
|
|
|
$storagePath, |
27
|
|
|
$file, |
28
|
|
|
$newFileName |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
if ($uploadResult === false) { |
32
|
|
|
return null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return File::create([ |
36
|
|
|
'category' => $uploadOption->category, |
37
|
|
|
'model_id' => $uploadOption->source?->getKey(), |
38
|
|
|
'model_type' => $uploadOption->source?->getMorphClass(), |
39
|
|
|
'mime_type' => $uploadOption->fileMimeType ?: $file->getType(), |
40
|
|
|
'path' => $storagePath . $newFileName, |
41
|
|
|
'filename' => $newFileName, |
42
|
|
|
'original_filename' => $uploadOption->originalFileName ?: '', |
43
|
|
|
'size' => $uploadOption->fileSize ?: $file->getSize(), |
44
|
|
|
'source' => $driver, |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the URL of the File |
50
|
|
|
*/ |
51
|
|
|
public function getUrl(File $file): ?string |
52
|
|
|
{ |
53
|
|
|
$disk = $file->getFilesystemDisk(); |
54
|
|
|
if (!$disk->exists($file->path)) { |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $disk->url($file->path); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the URL of the File |
63
|
|
|
* |
64
|
|
|
* Note: only s3 is supporting this |
65
|
|
|
*/ |
66
|
|
|
public function getTemporaryUrl(File $file, Carbon $expiredAt): ?string |
67
|
|
|
{ |
68
|
|
|
$disk = $file->getFilesystemDisk(); |
69
|
|
|
if (!$disk->exists($file->path)) { |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $disk->temporaryUrl($file->path, $expiredAt); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths