|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace h4kuna\Upload; |
|
4
|
|
|
|
|
5
|
|
|
use h4kuna\Upload\Store, |
|
6
|
|
|
h4kuna\Upload\Upload\Options, |
|
7
|
|
|
Nette\Http; |
|
8
|
|
|
|
|
9
|
|
|
class Upload |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** @var IDriver */ |
|
13
|
|
|
private $driver; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Options[] */ |
|
16
|
|
|
private $uploadOptions = []; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
public function __construct(IDriver $driver) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->driver = $driver; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Http\FileUpload $fileUpload |
|
27
|
|
|
* @param Options|string|null $uploadOptions - string is path |
|
28
|
|
|
* @return Store\File |
|
29
|
|
|
* @throws FileUploadFailedException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function save(Http\FileUpload $fileUpload, $uploadOptions = null) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($uploadOptions === null || is_scalar($uploadOptions)) { |
|
34
|
|
|
$uploadOptions = $this->getUploadOptions((string) $uploadOptions); |
|
35
|
|
|
} elseif (!$uploadOptions instanceof Options) { |
|
36
|
|
|
throw new InvalidArgumentException('Second parameter must be instance of UploadOptions or null or string.'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return self::saveFileUpload($fileUpload, $this->driver, $uploadOptions); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $key |
|
45
|
|
|
* @return Options |
|
46
|
|
|
*/ |
|
47
|
|
|
private function getUploadOptions($key) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!isset($this->uploadOptions[$key])) { |
|
50
|
|
|
$this->uploadOptions[$key] = new Options($key); |
|
51
|
|
|
} |
|
52
|
|
|
return $this->uploadOptions[$key]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Output object save to database. |
|
58
|
|
|
* Don't forget use nette rule Form::MIME_TYPE and Form::IMAGE. |
|
59
|
|
|
* $fileUpload->isOk() nette call automatically. |
|
60
|
|
|
* @param Http\FileUpload $fileUpload |
|
61
|
|
|
* @param IDriver $driver |
|
62
|
|
|
* @param Options $uploadOptions |
|
63
|
|
|
* @return Store\File |
|
64
|
|
|
* @throws FileUploadFailedException |
|
65
|
|
|
* @throws UnSupportedFileTypeException |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function saveFileUpload(Http\FileUpload $fileUpload, IDriver $driver, Options $uploadOptions) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!$uploadOptions->getFilter()->isAllowed($fileUpload->getContentType())) { |
|
70
|
|
|
throw new UnSupportedFileTypeException('name: ' . $fileUpload->getName() . ', type: ' . $fileUpload->getContentType()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
do { |
|
74
|
|
|
$relativePath = $uploadOptions->getPath() . $uploadOptions->getFilename()->createUniqueName($fileUpload); |
|
75
|
|
|
} while ($driver->isFileExists($relativePath)); |
|
76
|
|
|
|
|
77
|
|
|
$storeFile = new Store\File($relativePath, $fileUpload->getName(), $fileUpload->getContentType()); |
|
78
|
|
|
|
|
79
|
|
|
$uploadOptions->runExtendStoredFile($storeFile, $fileUpload); |
|
80
|
|
|
|
|
81
|
|
|
try { |
|
82
|
|
|
$driver->save($fileUpload, $relativePath); |
|
83
|
|
|
} catch (\Exception $e) { |
|
84
|
|
|
throw new FileUploadFailedException('Driver "' . get_class($driver) . '" failed.', null, $e); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $storeFile; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|