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 return (bool)strlen($path);
33 /* -( Managing File Data )------------------------------------------------- */
37 * Write the file data to local disk. Returns the relative path as the
41 public function writeFile($data, array $params) {
42 $root = $this->getLocalDiskFileStorageRoot();
44 // Generate a random, unique file path like "ab/29/1f918a9ac39201ff". We
45 // put a couple of subdirectories up front to avoid a situation where we
46 // have one directory with a zillion files in it, since this is generally
49 $name = md5(mt_rand());
50 $name = preg_replace('/^(..)(..)(.*)$/', '\\1/\\2/\\3', $name);
51 if (!Filesystem
::pathExists($root.'/'.$name)) {
56 $parent = $root.'/'.dirname($name);
57 if (!Filesystem
::pathExists($parent)) {
58 execx('mkdir -p %s', $parent);
61 AphrontWriteGuard
::willWrite();
62 Filesystem
::writeFile($root.'/'.$name, $data);
69 * Read the file data off local disk.
72 public function readFile($handle) {
73 $path = $this->getLocalDiskFileStorageFullPath($handle);
74 return Filesystem
::readFile($path);
79 * Deletes the file from local disk, if it exists.
82 public function deleteFile($handle) {
83 $path = $this->getLocalDiskFileStorageFullPath($handle);
84 if (Filesystem
::pathExists($path)) {
85 AphrontWriteGuard
::willWrite();
86 Filesystem
::remove($path);
91 /* -( Internals )---------------------------------------------------------- */
95 * Get the configured local disk path for file storage.
97 * @return string Absolute path to somewhere that files can be stored.
100 private function getLocalDiskFileStorageRoot() {
101 $root = PhabricatorEnv
::getEnvConfig('storage.local-disk.path');
103 if (!$root ||
$root == '/' ||
$root[0] != '/') {
104 throw new PhabricatorFileStorageConfigurationException(
106 "Malformed local disk storage root. You must provide an absolute ".
107 "path, and can not use '%s' as the root.",
111 return rtrim($root, '/');
116 * Convert a handle into an absolute local disk path.
118 * @param string File data handle.
119 * @return string Absolute path to the corresponding file.
122 private function getLocalDiskFileStorageFullPath($handle) {
123 // Make sure there's no funny business going on here. Users normally have
124 // no ability to affect the content of handles, but double-check that
125 // we're only accessing local storage just in case.
126 if (!preg_match('@^[a-f0-9]{2}/[a-f0-9]{2}/[a-f0-9]{28}\z@', $handle)) {
129 "Local disk filesystem handle '%s' is malformed!",
132 $root = $this->getLocalDiskFileStorageRoot();
133 return $root.'/'.$handle;