[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 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! 6 * 7 * @task impl Implementation 8 * @task internal Internals 9 */ 10 final class PhabricatorLocalDiskFileStorageEngine 11 extends PhabricatorFileStorageEngine { 12 13 14 /* -( Implementation )----------------------------------------------------- */ 15 16 17 /** 18 * This engine identifies as "local-disk". 19 * @task impl 20 */ 21 public function getEngineIdentifier() { 22 return 'local-disk'; 23 } 24 25 26 /** 27 * Write the file data to local disk. Returns the relative path as the 28 * file data handle. 29 * @task impl 30 */ 31 public function writeFile($data, array $params) { 32 $root = $this->getLocalDiskFileStorageRoot(); 33 34 // Generate a random, unique file path like "ab/29/1f918a9ac39201ff". We 35 // put a couple of subdirectories up front to avoid a situation where we 36 // have one directory with a zillion files in it, since this is generally 37 // bad news. 38 do { 39 $name = md5(mt_rand()); 40 $name = preg_replace('/^(..)(..)(.*)$/', '\\1/\\2/\\3', $name); 41 if (!Filesystem::pathExists($root.'/'.$name)) { 42 break; 43 } 44 } while (true); 45 46 $parent = $root.'/'.dirname($name); 47 if (!Filesystem::pathExists($parent)) { 48 execx('mkdir -p %s', $parent); 49 } 50 51 AphrontWriteGuard::willWrite(); 52 Filesystem::writeFile($root.'/'.$name, $data); 53 54 return $name; 55 } 56 57 58 /** 59 * Read the file data off local disk. 60 * @task impl 61 */ 62 public function readFile($handle) { 63 $path = $this->getLocalDiskFileStorageFullPath($handle); 64 return Filesystem::readFile($path); 65 } 66 67 68 /** 69 * Deletes the file from local disk, if it exists. 70 * @task impl 71 */ 72 public function deleteFile($handle) { 73 $path = $this->getLocalDiskFileStorageFullPath($handle); 74 if (Filesystem::pathExists($path)) { 75 AphrontWriteGuard::willWrite(); 76 Filesystem::remove($path); 77 } 78 } 79 80 81 /* -( Internals )---------------------------------------------------------- */ 82 83 84 /** 85 * Get the configured local disk path for file storage. 86 * 87 * @return string Absolute path to somewhere that files can be stored. 88 * @task internal 89 */ 90 private function getLocalDiskFileStorageRoot() { 91 $root = PhabricatorEnv::getEnvConfig('storage.local-disk.path'); 92 93 if (!$root || $root == '/' || $root[0] != '/') { 94 throw new PhabricatorFileStorageConfigurationException( 95 "Malformed local disk storage root. You must provide an absolute ". 96 "path, and can not use '/' as the root."); 97 } 98 99 return rtrim($root, '/'); 100 } 101 102 103 /** 104 * Convert a handle into an absolute local disk path. 105 * 106 * @param string File data handle. 107 * @return string Absolute path to the corresponding file. 108 * @task internal 109 */ 110 private function getLocalDiskFileStorageFullPath($handle) { 111 // Make sure there's no funny business going on here. Users normally have 112 // no ability to affect the content of handles, but double-check that 113 // we're only accessing local storage just in case. 114 if (!preg_match('@^[a-f0-9]{2}/[a-f0-9]{2}/[a-f0-9]{28}\z@', $handle)) { 115 throw new Exception( 116 "Local disk filesystem handle '{$handle}' is malformed!"); 117 } 118 $root = $this->getLocalDiskFileStorageRoot(); 119 return $root.'/'.$handle; 120 } 121 122 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |