/tags/0.2-final/webapp/php/trunk/classes/LocalFS.php

https://github.com/shanti/olio · PHP · 221 lines · 145 code · 30 blank · 46 comment · 46 complexity · ab59f5d16064a04a13bd8ab2d4ed2063 MD5 · raw file

  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * Class for accessing the local file system.
  21. * Allows creation, retrieval and storage of files, querying
  22. * existence of a file, etc.
  23. */
  24. //require_once("../classes/DirectoryType.php");
  25. class LocalFS extends FileSystem {
  26. function __construct() {
  27. $this->localFSRoot = Olio::$config['localfsRoot'];
  28. }
  29. function getRootDir() {
  30. return $this->localFSRoot;
  31. }
  32. /*
  33. * This method creates the path that is needed for the file that is to be created/copied
  34. * It takes a directoryType and an id. It then generates the
  35. * path and if it does not already exist it creates it. If it is unable to create the path
  36. * it returns false, else it returns true.
  37. */
  38. function createPath($dirType, $identifier)
  39. {
  40. //build path variables
  41. $dirPrimaryPath = sprintf("%03d", $identifier % 1000);
  42. $dirSecondaryPath = sprintf("%03d", $identifier / 1000000 % 1000);
  43. $base_directory = $this->localFSRoot;
  44. if($dirType != "invalid")
  45. {
  46. $path = $base_directory . '/' . $dirType . '/' . $dirPrimaryPath . '/' . $dirSecondaryPath;
  47. if(is_dir($path) == true)
  48. {
  49. return true;
  50. }else
  51. {
  52. if(is_dir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath) == true)
  53. {
  54. //partial path exists eg /filestore/people/444 initial hash dir exists
  55. //so we need to complete the path
  56. if(mkdir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath . '/' . $dirSecondaryPath, 0777, true) == true)
  57. {
  58. return true;
  59. }else //mkdir($base_directory . '/' . $dirTypeExpanded . '/' . $dirPrimaryPath . '/' . $dirSecondaryPath, 0777, true) failed
  60. {
  61. return false;
  62. }
  63. }else if(is_dir($base_directory . '/' . $dirType) == true)
  64. {
  65. //partial path exists eg /filestore/people initial hash dir does not exists
  66. //so we need to complete the path
  67. if(mkdir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath, 0777, true) == true)
  68. {
  69. if(mkdir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath . '/' . $dirSecondaryPath, 0777, true) == true)
  70. {
  71. return true;
  72. }else //mkdir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath . '/' . $dirSecondaryPath, 0777, true) failed
  73. {
  74. return false;
  75. }
  76. }else //mkdir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath, 0777, true) failed
  77. {
  78. return false;
  79. }
  80. }else //the path really is not there so it needs to be created
  81. {
  82. if(mkdir($base_directory . '/' . $dirType, 0777, true) == true)
  83. {
  84. if(mkdir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath, 0777, true) == true)
  85. {
  86. if(mkdir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath . '/' . $dirSecondaryPath, 0777, true) == true)
  87. {
  88. return true;
  89. }else //mkdir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath . '/' . $dirSecondaryPath, 0777, true) failed
  90. {
  91. return false;
  92. }
  93. }else //mkdir($base_directory . '/' . $dirType . '/' . $dirPrimaryPath, 0777, true) failed
  94. {
  95. return false;
  96. }
  97. }else //mkdir($base_directory . '/' . $dirType, 0777, true) failed
  98. {
  99. printf("mkdir failed");
  100. return false;
  101. }
  102. }//end else path does not exist
  103. }//end else whole path does not exist
  104. }else //DirectoryType::Decode() returned invalid
  105. {
  106. return false;
  107. }
  108. }
  109. static $typemap = array("p" => "person", "P" => "person",
  110. "e" => "event", "E"=> "event");
  111. function mapAttributes($filename) {
  112. // Do pattern matching and splitting.
  113. $prefix = substr($filename, 0, 1);
  114. $attrs["type"] = LocalFS::$typemap[$prefix];
  115. $dotidx = strrpos($filename, ".");
  116. $postfix = substr($filename, $dotidx - 1, 1);
  117. if (is_numeric($postfix)) {
  118. $attrs["id"] = substr($filename, 1, $dotidx - 1);
  119. $attrs["type"] .= "s";
  120. } else if ($postfix == "t" || $postfix =="T") {
  121. $attrs["id"] = substr($filename, 1, $dotidx - 2);
  122. $attrs["type"] .= "Thumbs";
  123. } else if ($postfix == "l" || $postfix =="L") {
  124. $attrs["id"] = substr($filename, 1, $dotidx - 2);
  125. $attrs["type"] .= "Lits";
  126. } else {
  127. error_log("Invalid file name pattern ".$filename);
  128. $attrs["type"] = "invalid";
  129. }
  130. return $attrs;
  131. }
  132. function create($srcpath, $replication_factor='1', $overwrite = 'true') {
  133. $filename = basename($srcpath); //remove the file's path so we can work with just the filename.
  134. $attrs = $this->mapAttributes($filename);
  135. $destpath = $this->getFullPath($filename, $attrs);
  136. if ($destpath == "invalid") {
  137. printf("Invalid response from getFullPath");
  138. return false;
  139. } else {
  140. if ($overwrite) {
  141. //printf("<p>Using overwrite path</p>");
  142. if ($this->createPath($attrs["type"], $attrs["id"])) {
  143. //we are assuming that the file will be stored in /tmp
  144. return copy($srcpath, $destpath);
  145. } else {//path cretion failed
  146. return false;
  147. }
  148. } else {//we are not allowing overwrite
  149. //printf("<p>Using non-overwrite path</p>");
  150. if (file_exists($destpath)) {
  151. return false;
  152. } else { //file does not exist
  153. if ($this->createPath($attrs["type"], $attrs["id"]) == true) {
  154. //we are assuming that the file will be stored in /tmp
  155. return copy("/tmp/" . $filename, $destpath);
  156. } else {//path cretion failed
  157. return false;
  158. }
  159. } //end file_exists else
  160. } //end overwrite else
  161. } //end invalid else
  162. }
  163. /*function getNewFileName($oldfilename) {
  164. return $this->localFSRoot . '/' . basename($oldfilename);
  165. }*/
  166. function getFullPath($filename, $attrs = null) {
  167. if (is_null($attrs)) {
  168. $attrs = $this->mapAttributes($filename);
  169. }
  170. //build path
  171. $dirPrimaryPath = sprintf("%03d", $attrs["id"] % 1000);
  172. $dirSecondaryPath = sprintf("%03d", $attrs["id"] / 1000000 % 1000);
  173. $base_directory = $this->localFSRoot;
  174. if($attrs["type"] != "invalid") {
  175. $path = $base_directory . '/' . $attrs["type"] . '/' . $dirPrimaryPath . '/' . $dirSecondaryPath . '/' . $filename;
  176. return $path;
  177. } else {
  178. //return invalid as a failed message
  179. return "invalid";
  180. }
  181. }
  182. function getPaths($filename) {
  183. return array(0 => $this->getFullPath($filename));
  184. }
  185. /* return true or false */
  186. function delete($filename) { return unlink($this->getFullPath($filename)); }
  187. /* return true or false */
  188. function exists($filename) { return file_exists($this->getFullPath($filename)); }
  189. function open($filename) { return file_get_contents($this->getFullPath($filename)); }
  190. }
  191. ?>