-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathFilesystem.php
More file actions
158 lines (142 loc) · 4.2 KB
/
Filesystem.php
File metadata and controls
158 lines (142 loc) · 4.2 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Codesleeve\Stapler\Storage;
use Codesleeve\Stapler\Interfaces\Storage as StorageInterface;
use Codesleeve\Stapler\Exceptions;
use Codesleeve\Stapler\Attachment;
class Filesystem implements StorageInterface
{
/**
* The current attachedFile object being processed.
*
* @var \Codesleeve\Stapler\Attachment
*/
public $attachedFile;
/**
* Constructor method.
*
* @param Attachment $attachedFile
*/
public function __construct(Attachment $attachedFile)
{
$this->attachedFile = $attachedFile;
}
/**
* Return the url for a file upload.
*
* @param string $styleName
* @param string $expires
*
* @return string
*/
public function url($styleName, $expires = null)
{
return $this->attachedFile->getInterpolator()->interpolate($this->attachedFile->url, $this->attachedFile, $styleName);
}
/**
* Return the path (on disk) of a file upload.
*
* @param string $styleName
*
* @return string
*/
public function path($styleName)
{
return $this->attachedFile->getInterpolator()->interpolate($this->attachedFile->path, $this->attachedFile, $styleName);
}
/**
* Remove an attached file.
*
* @param array $filePaths
*/
public function remove(array $filePaths)
{
foreach ($filePaths as $filePath) {
$directory = dirname($filePath);
$this->emptyDirectory($directory, true);
}
}
/**
* Move an uploaded file to it's intended destination.
* The file can be an actual uploaded file object or the path to
* a resized image file on disk.
*
* @param string $file
* @param string $filePath
*/
public function move($file, $filePath)
{
$this->buildDirectory($filePath);
$this->moveFile($file, $filePath);
$this->setPermissions($filePath, $this->attachedFile->override_file_permissions);
}
/**
* Determine if a style directory needs to be built and if so create it.
*
* @param string $filePath
*/
protected function buildDirectory($filePath)
{
$directory = dirname($filePath);
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
}
/**
* Set the file permissions of a file upload
* Does not ignore umask.
*
* @param string $filePath
* @param int $overrideFilePermissions
*/
protected function setPermissions($filePath, $overrideFilePermissions)
{
if ($overrideFilePermissions) {
chmod($filePath, $overrideFilePermissions & ~umask());
} elseif (is_null($overrideFilePermissions)) {
chmod($filePath, 0666 & ~umask());
}
}
/**
* Attempt to move and uploaded file to it's intended location on disk.
*
* @param string $file
* @param string $filePath
*
* @throws Exceptions\FileException
*/
protected function moveFile($file, $filePath)
{
if (!@rename($file, $filePath)) {
$error = error_get_last();
throw new Exceptions\FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $file, $filePath, strip_tags($error['message'])));
}
}
/**
* Recursively delete the files in a directory.
*
* @desc Recursively loops through each file in the directory and deletes it.
*
* @param string $directory
* @param bool $deleteDirectory
*/
protected function emptyDirectory($directory, $deleteDirectory = false)
{
if (!is_dir($directory) || !($directoryHandle = opendir($directory))) {
return;
}
while (false !== ($object = readdir($directoryHandle))) {
if ($object == '.' || $object == '..') {
continue;
}
if (!is_dir($directory.'/'.$object)) {
unlink($directory.'/'.$object);
} else {
$this->emptyDirectory($directory.'/'.$object, true); // The object is a folder, recurse through it.
}
}
if ($deleteDirectory) {
closedir($directoryHandle);
rmdir($directory);
}
}
}