1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lincable\Http\File; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Lincable\Http\FileRequest; |
7
|
|
|
use Illuminate\Container\Container; |
8
|
|
|
use Illuminate\Http\File as IlluminateFile; |
9
|
|
|
use Lincable\Exceptions\NotResolvableFileException; |
10
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
11
|
|
|
use Symfony\Component\HttpFoundation\File\File as SymfonyFile; |
12
|
|
|
|
13
|
|
|
class FileResolver |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Resolve the file object to a symfony file, handling the |
17
|
|
|
* file request operations. |
18
|
|
|
* |
19
|
|
|
* @param mixed $file |
20
|
|
|
* @return \Illuminate\Http\File |
21
|
|
|
*/ |
22
|
26 |
|
public static function resolve($file) |
23
|
|
|
{ |
24
|
26 |
|
if ($file instanceof IlluminateFile) { |
25
|
17 |
|
return $file; |
26
|
|
|
} |
27
|
|
|
|
28
|
9 |
|
return static::toIlluminateFile(static::resolveSymfonyFile($file)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Prepare the file . |
33
|
|
|
* |
34
|
|
|
* @param mixed $file |
35
|
|
|
* @return \Symfony\Component\HttpFoundation\File\File |
36
|
|
|
* |
37
|
|
|
* @throws \Lincable\Exceptions\NotResolvableFileException |
38
|
|
|
*/ |
39
|
9 |
|
public static function resolveSymfonyFile($file) |
40
|
|
|
{ |
41
|
9 |
|
if (is_string($file)) { |
42
|
2 |
|
return new SymfonyFile($file); |
43
|
|
|
} |
44
|
|
|
|
45
|
7 |
|
if ($file instanceof FileRequest) { |
46
|
2 |
|
return $file->prepareFile(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
7 |
|
if ($file instanceof UploadedFile) { |
50
|
5 |
|
return $file->move( |
51
|
5 |
|
FileFactory::getTemporaryDirectory(), |
|
|
|
|
52
|
5 |
|
$file->hashName() |
|
|
|
|
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
if ($file instanceof Symfonyfile) { |
57
|
1 |
|
return $file; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
throw new NotResolvableFileException($file); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Convert a symfony file to illuminate file. |
65
|
|
|
* |
66
|
|
|
* @param \Symfony\Component\HttpFoundation\File\File $file |
67
|
|
|
* @return \Illuminate\Http\File |
68
|
|
|
*/ |
69
|
7 |
|
public static function toIlluminateFile(SymfonyFile $file) |
70
|
|
|
{ |
71
|
7 |
|
return new IlluminateFile($file->getPathName()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|