1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tkanstantsin\fileupload\saver; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\FilesystemInterface; |
6
|
|
|
use tkanstantsin\fileupload\formatter\File; |
7
|
|
|
use tkanstantsin\fileupload\model\IFile; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Saver allows store processed files. |
11
|
|
|
* E.g. store uploaded files or cache cropped/prepared images. |
12
|
|
|
* |
13
|
|
|
* @todo add option to force rewriting existed file. |
14
|
|
|
*/ |
15
|
|
|
class Saver |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var IFile |
19
|
|
|
*/ |
20
|
|
|
public $file; |
21
|
|
|
/** |
22
|
|
|
* Filesystem where file will be stored |
23
|
|
|
* @var FilesystemInterface |
24
|
|
|
*/ |
25
|
|
|
public $filesystem; |
26
|
|
|
/** |
27
|
|
|
* File path in $filesystem |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $path; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Saver constructor. |
34
|
|
|
* @param IFile $file |
35
|
|
|
* @param FilesystemInterface $filesystem |
36
|
|
|
* @param string $assetPath |
37
|
|
|
*/ |
38
|
|
|
public function __construct(IFile $file, FilesystemInterface $filesystem, string $assetPath) |
39
|
|
|
{ |
40
|
|
|
$this->file = $file; |
41
|
|
|
$this->filesystem = $filesystem; |
42
|
|
|
$this->path = $assetPath; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Copies, processes and saves file in $filesystem |
47
|
|
|
* @param \tkanstantsin\fileupload\formatter\File $formatter |
48
|
|
|
* @return bool |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
51
|
|
|
*/ |
52
|
|
|
public function save(File $formatter): bool |
53
|
|
|
{ |
54
|
|
|
$isCached = $this->isSaved(); |
55
|
|
|
$isEmpty = $this->isEmpty(); |
56
|
|
|
if ($isCached && !$isEmpty) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
if ($isEmpty) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// checks if path is writable |
64
|
|
|
// create new empty file or override existed one |
65
|
|
|
// also caches empty result for non-formatted files |
66
|
|
|
$this->filesystem->put($this->path, null); |
67
|
|
|
|
68
|
|
|
$content = $formatter->getContent(); |
69
|
|
|
if ($content === '') { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->write($content); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
protected function isEmpty(): bool |
80
|
|
|
{ |
81
|
|
|
return $this->filesystem->has($this->path) |
82
|
|
|
&& $this->filesystem->getSize($this->path) === 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Checks if file is already in $path. |
87
|
|
|
* @return bool |
88
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
89
|
|
|
*/ |
90
|
|
|
protected function isSaved(): bool |
91
|
|
|
{ |
92
|
|
|
return $this->filesystem->has($this->path) |
93
|
|
|
&& $this->filesystem->getTimestamp($this->path) > $this->file->getUpdatedAt(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Saves file into $filesystem |
98
|
|
|
* @param $content |
99
|
|
|
* @return bool |
100
|
|
|
* @throws \InvalidArgumentException |
101
|
|
|
*/ |
102
|
|
|
protected function write($content): bool |
103
|
|
|
{ |
104
|
|
|
if ($content === false || $content === null) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return \is_resource($content) |
109
|
|
|
? $this->filesystem->putStream($this->path, $content) |
110
|
|
|
: $this->filesystem->put($this->path, $content); |
111
|
|
|
} |
112
|
|
|
} |