/
FileSystem.php
109 lines (103 loc) · 3.79 KB
/
FileSystem.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Upload
*
* @author Josh Lockhart <info@joshlockhart.com>
* @copyright 2012 Josh Lockhart
* @link http://www.joshlockhart.com
* @version 2.0.0
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Upload\Storage;
/**
* FileSystem Storage
*
* This class uploads files to a designated directory on the filesystem.
*
* @author Josh Lockhart <info@joshlockhart.com>
* @since 1.0.0
* @package Upload
*/
class FileSystem implements \Upload\StorageInterface
{
/**
* Path to upload destination directory (with trailing slash)
* @var string
*/
protected $directory;
/**
* Overwrite existing files?
* @var bool
*/
protected $overwrite;
/**
* Constructor
*
* @param string $directory Relative or absolute path to upload directory
* @param bool $overwrite Should this overwrite existing files?
* @throws \InvalidArgumentException If directory does not exist
* @throws \InvalidArgumentException If directory is not writable
*/
public function __construct($directory, $overwrite = false)
{
if (!is_dir($directory)) {
throw new \InvalidArgumentException('Directory does not exist');
}
if (!is_writable($directory)) {
throw new \InvalidArgumentException('Directory is not writable');
}
$this->directory = rtrim($directory, '/') . DIRECTORY_SEPARATOR;
$this->overwrite = (bool)$overwrite;
}
/**
* Upload
*
* @param \Upload\FileInfoInterface $file The file object to upload
* @throws \Upload\Exception If overwrite is false and file already exists
* @throws \Upload\Exception If error moving file to destination
*/
public function upload(\Upload\FileInfoInterface $fileInfo)
{
$destinationFile = $this->directory . $fileInfo->getNameWithExtension();
if ($this->overwrite === false && file_exists($destinationFile) === true) {
throw new \Upload\Exception('File already exists', $fileInfo);
}
if ($this->moveUploadedFile($fileInfo->getPathname(), $destinationFile) === false) {
throw new \Upload\Exception('File could not be moved to final destination.', $fileInfo);
}
}
/**
* Move uploaded file
*
* This method allows us to stub this method in unit tests to avoid
* hard dependency on the `move_uploaded_file` function.
*
* @param string $source The source file
* @param string $destination The destination file
* @return bool
*/
protected function moveUploadedFile($source, $destination)
{
return move_uploaded_file($source, $destination);
}
}