4 * Local disk storage engine. Keeps files on local disk. This engine is easy
5 * to set up, but it doesn't work if you have multiple web frontends!
7 * @task internal Internals
9 final class PhabricatorLocalDiskFileStorageEngine
10 extends PhabricatorFileStorageEngine
{
13 /* -( Engine Metadata )---------------------------------------------------- */
17 * This engine identifies as "local-disk".
19 public function getEngineIdentifier() {
23 public function getEnginePriority() {
27 public function canWriteFiles() {
28 $path = PhabricatorEnv
::getEnvConfig('storage.local-disk.path');
29 $path = phutil_string_cast($path);
30 return (bool)strlen($path);
34 /* -( Managing File Data )------------------------------------------------- */
38 * Write the file data to local disk. Returns the relative path as the
42 public function writeFile($data, array $params) {
43 $root = $this->getLocalDiskFileStorageRoot();
45 // Generate a random, unique file path like "ab/29/1f918a9ac39201ff". We
46 // put a couple of subdirectories up front to avoid a situation where we
47 // have one directory with a zillion files in it, since this is generally
50 $name = md5(mt_rand());
51 $name = preg_replace('/^(..)(..)(.*)$/', '\\1/\\2/\\3', $name);
52 if (!Filesystem
::pathExists($root.'/'.$name)) {
57 $parent = $root.'/'.dirname($name);
58 if (!Filesystem
::pathExists($parent)) {
59 execx('mkdir -p %s', $parent);
62 AphrontWriteGuard
::willWrite();
63 Filesystem
::writeFile($root.'/'.$name, $data);
70 * Read the file data off local disk.
73 public function readFile($handle) {
74 $path = $this->getLocalDiskFileStorageFullPath($handle);
75 return Filesystem
::readFile($path);
80 * Deletes the file from local disk, if it exists.
83 public function deleteFile($handle) {
84 $path = $this->getLocalDiskFileStorageFullPath($handle);
85 if (Filesystem
::pathExists($path)) {
86 AphrontWriteGuard
::willWrite();
87 Filesystem
::remove($path);
92 /* -( Internals )---------------------------------------------------------- */
96 * Get the configured local disk path for file storage.
98 * @return string Absolute path to somewhere that files can be stored.
101 private function getLocalDiskFileStorageRoot() {
102 $root = PhabricatorEnv
::getEnvConfig('storage.local-disk.path');
104 if (!$root ||
$root == '/' ||
$root[0] != '/') {
105 throw new PhabricatorFileStorageConfigurationException(
107 "Malformed local disk storage root. You must provide an absolute ".
108 "path, and can not use '%s' as the root.",
112 return rtrim($root, '/');
117 * Convert a handle into an absolute local disk path.
119 * @param string File data handle.
120 * @return string Absolute path to the corresponding file.
123 private function getLocalDiskFileStorageFullPath($handle) {
124 // Make sure there's no funny business going on here. Users normally have
125 // no ability to affect the content of handles, but double-check that
126 // we're only accessing local storage just in case.
127 if (!preg_match('@^[a-f0-9]{2}/[a-f0-9]{2}/[a-f0-9]{28}\z@', $handle)) {
130 "Local disk filesystem handle '%s' is malformed!",
133 $root = $this->getLocalDiskFileStorageRoot();
134 return $root.'/'.$handle;