1
|
|
|
<?php namespace Modules\Media\Services; |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Filesystem\Factory; |
4
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
5
|
|
|
use Modules\Media\Entities\File; |
6
|
|
|
use Modules\Media\Jobs\CreateThumbnails; |
7
|
|
|
use Modules\Media\Repositories\FileRepository; |
8
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
9
|
|
|
|
10
|
|
|
class FileService |
11
|
|
|
{ |
12
|
|
|
use DispatchesJobs; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var FileRepository |
16
|
|
|
*/ |
17
|
|
|
private $file; |
18
|
|
|
/** |
19
|
|
|
* @var Factory |
20
|
|
|
*/ |
21
|
|
|
private $filesystem; |
22
|
|
|
|
23
|
|
|
public function __construct(FileRepository $file, Factory $filesystem) |
24
|
|
|
{ |
25
|
|
|
$this->file = $file; |
26
|
|
|
$this->filesystem = $filesystem; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param UploadedFile $file |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function store(UploadedFile $file) |
34
|
|
|
{ |
35
|
|
|
$savedFile = $this->file->createFromFile($file); |
36
|
|
|
|
37
|
|
|
$path = $this->getDestinationPath($savedFile->getOriginal('path')); |
38
|
|
|
$stream = fopen($file->getRealPath(), 'r+'); |
39
|
|
|
$this->filesystem->disk($this->getConfiguredFilesystem())->writeStream($path, $stream, [ |
|
|
|
|
40
|
|
|
'visibility' => 'public', |
41
|
|
|
'mimetype' => $savedFile->mimetype, |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
$this->createThumbnails($savedFile); |
45
|
|
|
|
46
|
|
|
return $savedFile; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create the necessary thumbnails for the given file |
51
|
|
|
* @param $savedFile |
52
|
|
|
*/ |
53
|
|
|
private function createThumbnails(File $savedFile) |
54
|
|
|
{ |
55
|
|
|
$this->dispatch(new CreateThumbnails($savedFile->path)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $path |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
private function getDestinationPath($path) |
63
|
|
|
{ |
64
|
|
|
if ($this->getConfiguredFilesystem() === 'local') { |
65
|
|
|
return basename(public_path()) . $path; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $path; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
private function getConfiguredFilesystem() |
75
|
|
|
{ |
76
|
|
|
return config('asgard.media.config.filesystem'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.