-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathUploader.php
More file actions
82 lines (61 loc) · 2.13 KB
/
Uploader.php
File metadata and controls
82 lines (61 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Statamic\Assets;
use Facades\Statamic\Imaging\ImageValidator;
use Statamic\Facades\Glide;
use Statamic\Support\Str;
use Statamic\Support\Svg;
use Symfony\Component\HttpFoundation\File\UploadedFile;
abstract class Uploader
{
public function upload(UploadedFile $file)
{
$source = $this->processSourceFile($file);
$this->write($source, $path = $this->uploadPath($file));
if (app()->runningInConsole()) {
app('files')->delete($source);
return $path;
}
dispatch(fn () => app('files')->delete($source))->afterResponse();
return $path;
}
private function processSourceFile(UploadedFile $file): string
{
if ($file->getMimeType() === 'image/gif') {
return $file->getRealPath();
}
if (! $preset = $this->preset()) {
return $file->getRealPath();
}
if (! ImageValidator::isValidImage($file->getClientOriginalExtension(), $file->getClientMimeType())) {
return $file->getRealPath();
}
$server = Glide::server([
'source' => $file->getPath(),
'cache' => $cache = storage_path('statamic/glide/tmp'),
'cache_with_file_extensions' => false,
]);
try {
return $cache.'/'.$server->makeImage($file->getFilename(), ['p' => $preset]);
} catch (\Exception $exception) {
return $file->getRealPath();
}
}
private function write($sourcePath, $destinationPath)
{
$stream = fopen($sourcePath, 'r');
if (config('statamic.assets.svg_sanitization_on_upload', true) && Str::endsWith($destinationPath, '.svg')) {
$stream = Svg::sanitize(stream_get_contents($stream));
}
$this->disk()->put($this->uploadPathPrefix().$destinationPath, $stream);
if (is_resource($stream)) {
fclose($stream);
}
}
abstract protected function uploadPath(UploadedFile $file);
protected function uploadPathPrefix()
{
return '';
}
abstract protected function preset();
abstract protected function disk();
}