Remove product literal strings in "pht()", part 5
[phabricator.git] / src / applications / files / engine / PhabricatorLocalDiskFileStorageEngine.php
blobafed9f20b3ef09c1d7bd59bf16c05d4ad99420ca
1 <?php
3 /**
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
8 */
9 final class PhabricatorLocalDiskFileStorageEngine
10 extends PhabricatorFileStorageEngine {
13 /* -( Engine Metadata )---------------------------------------------------- */
16 /**
17 * This engine identifies as "local-disk".
19 public function getEngineIdentifier() {
20 return 'local-disk';
23 public function getEnginePriority() {
24 return 5;
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 )------------------------------------------------- */
37 /**
38 * Write the file data to local disk. Returns the relative path as the
39 * file data handle.
40 * @task impl
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
48 // bad news.
49 do {
50 $name = md5(mt_rand());
51 $name = preg_replace('/^(..)(..)(.*)$/', '\\1/\\2/\\3', $name);
52 if (!Filesystem::pathExists($root.'/'.$name)) {
53 break;
55 } while (true);
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);
65 return $name;
69 /**
70 * Read the file data off local disk.
71 * @task impl
73 public function readFile($handle) {
74 $path = $this->getLocalDiskFileStorageFullPath($handle);
75 return Filesystem::readFile($path);
79 /**
80 * Deletes the file from local disk, if it exists.
81 * @task impl
83 public function deleteFile($handle) {
84 $path = $this->getLocalDiskFileStorageFullPath($handle);
85 if (Filesystem::pathExists($path)) {
86 AphrontWriteGuard::willWrite();
87 Filesystem::remove($path);
92 /* -( Internals )---------------------------------------------------------- */
95 /**
96 * Get the configured local disk path for file storage.
98 * @return string Absolute path to somewhere that files can be stored.
99 * @task internal
101 private function getLocalDiskFileStorageRoot() {
102 $root = PhabricatorEnv::getEnvConfig('storage.local-disk.path');
104 if (!$root || $root == '/' || $root[0] != '/') {
105 throw new PhabricatorFileStorageConfigurationException(
106 pht(
107 "Malformed local disk storage root. You must provide an absolute ".
108 "path, and can not use '%s' as the root.",
109 '/'));
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.
121 * @task internal
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)) {
128 throw new Exception(
129 pht(
130 "Local disk filesystem handle '%s' is malformed!",
131 $handle));
133 $root = $this->getLocalDiskFileStorageRoot();
134 return $root.'/'.$handle;