tripal.files.api.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for managing files within
  5. * the Tripal data directory structure.
  6. */
  7. /**
  8. * @defgroup tripal_files_api Files
  9. * @ingroup tripal_api
  10. * @{
  11. * Provides an application programming interface (API) for managing files within
  12. * the Tripal data directory structure.
  13. * @}
  14. *
  15. */
  16. /**
  17. * Creates a directory for a module in the Drupal's public files directory.
  18. *
  19. * Previously it was recommended that this function be called during
  20. * installation of the module in the .install file. However this causes
  21. * permission problems if the module is installed via drush with a
  22. * user account that is not the same as the web user. Therefore, this
  23. * function should not be called in a location accessiblve via a drush
  24. * command. The tripal_get_files_dir() and tripal_get_files_stream()
  25. * will automatically create the directory if it doesn't exist so there is
  26. * little need to call this function directly.
  27. *
  28. * @param $module_name
  29. * the name of the module being installed
  30. * @param $path
  31. * Optional sub-path to create
  32. *
  33. * @ingroup tripal_files_api
  34. */
  35. function tripal_create_files_dir($module_name, $path = FALSE) {
  36. // if the path is not supplied then assume they want to create the base files
  37. // directory for the specified module
  38. if (!$path) {
  39. // make the data directory for this module
  40. $data_dir = tripal_get_files_dir() . "/$module_name";
  41. if (!file_prepare_directory($data_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  42. $message = "Cannot create directory $data_dir. This module may not " .
  43. "behave correctly without this directory. Please create " .
  44. "the directory manually or fix the problem and reinstall.";
  45. drupal_set_message(check_plain(t($message)), 'error');
  46. tripal_report_error('tripal', TRIPAL_ERROR, $message, array());
  47. }
  48. }
  49. else {
  50. // make sure the module data directory exists, we make a recursive call
  51. // but without the path
  52. tripal_create_files_dir($module_name);
  53. // now make sure the sub dir exists
  54. $sub_dir = tripal_get_files_dir() . '/' . $module_name . '/' . $path;
  55. if (!file_prepare_directory($sub_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  56. $message = "Can not create directory $sub_dir. ";
  57. drupal_set_message(check_plain(t($message)), 'error');
  58. tripal_report_error('tripal', TRIPAL_ERROR, $message, array());
  59. }
  60. }
  61. }
  62. /**
  63. * Retrieves the Drupal relative directory for a Tripal module.
  64. *
  65. * Each Tripal module has a unique data directory which was created using the
  66. * tripal_create_files_dir function during installation. This function
  67. * retrieves the directory path.
  68. *
  69. * @param $module_name
  70. * (Optional) The name of the module.
  71. *
  72. * @returns
  73. * The path within the Drupal installation where the data directory resides
  74. *
  75. * @ingroup tripal_files_api
  76. */
  77. function tripal_get_files_dir($module_name = FALSE) {
  78. // Build the directory path.
  79. $data_dir = variable_get('file_public_path', conf_path() . '/files/tripal');
  80. // If a module name is provided then append the module directory.
  81. if ($module_name) {
  82. $data_dir .= "/$module_name";
  83. // Make sure the directory exists.
  84. tripal_create_files_dir($module_name);
  85. }
  86. return $data_dir;
  87. }
  88. /**
  89. * Retrieves the Drupal stream (e.g. public://...) for a Tripal module.
  90. *
  91. * Each Tripal module has a unique data directory which was created using the
  92. * tripal_create_files_dir function during installation. This function
  93. * retrieves the directory path.
  94. *
  95. * @param $module_name
  96. * (Optional) The name of the module.
  97. *
  98. * @returns
  99. * The path within the Drupal installation where the data directory resides
  100. *
  101. * @ingroup tripal_files_api
  102. */
  103. function tripal_get_files_stream($module_name = FALSE) {
  104. // Build the directory path.
  105. $stream = 'public://tripal';
  106. // If a module name is provided then append the module directory.
  107. if ($module_name) {
  108. $stream .= "/$module_name";
  109. // Make sure the directory exists.
  110. tripal_create_files_dir($module_name);
  111. }
  112. return $stream;
  113. }
  114. /**
  115. * Formats a size (in bytes) in human readable format.
  116. *
  117. * Function taken from php.net
  118. *
  119. * @param $bytes
  120. * The size of the file in bytes
  121. * @param $precision
  122. * The number of decimal places to use in the final number if needed
  123. *
  124. * @return string
  125. * A formatted string indicating the size of the file
  126. *
  127. *
  128. * @ingroup tripal_files_api
  129. */
  130. function tripal_format_bytes($bytes, $precision = 2) {
  131. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  132. $bytes = max($bytes, 0);
  133. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  134. $pow = min($pow, count($units) - 1);
  135. // Uncomment one of the following alternatives
  136. $bytes /= pow(1000, $pow);
  137. // $bytes /= (1 << (10 * $pow));
  138. return round($bytes, $precision) . '' . $units[$pow];
  139. }
  140. /**
  141. * Retrieves the list of files uploaded by a user.
  142. *
  143. * @param $uid
  144. * The ID of the user whose files should be retrieved.
  145. * @param $allowed_types
  146. * A list of valid extensions to restrict the files to.
  147. * @param $module
  148. * The name of the module that is managing the file.
  149. *
  150. * @return
  151. * A list of file objects.
  152. *
  153. * @ingroup tripal_files_api
  154. */
  155. function tripal_get_user_uploads($uid, $allowed_types = array(), $module = 'tripal') {
  156. $user = user_load($uid);
  157. $query = db_select('file_managed', 'FM');
  158. $query->fields('FM', array('fid'));
  159. $query->distinct();
  160. $query->condition('FM.uid', $user->uid);
  161. $query->innerJoin('file_usage', 'FU', "FU.fid = FM.fid");
  162. $query->condition('FU.module', $module);
  163. $query->orderBy('FM.filename');
  164. $files = $query->execute();
  165. $files_list = [];
  166. while ($fid = $files->fetchField()) {
  167. $file = file_load($fid);
  168. foreach ($allowed_types as $type) {
  169. if (preg_match('/\.' . $type . '$/', $file->filename)) {
  170. $files_list[$fid] = $file;
  171. }
  172. }
  173. }
  174. return $files_list;
  175. }
  176. /**
  177. * Retrieves the URI for the dedicated directory for a user's files.
  178. *
  179. * This directory is used by the file uploader and by data collections.
  180. *
  181. * @param $user
  182. * A Drupal user object.
  183. *
  184. * @return
  185. * The URI of the directory.
  186. *
  187. * @ingroup tripal_files_api
  188. */
  189. function tripal_get_user_files_dir($user) {
  190. $user_dir = 'public://tripal/users/' . $user->uid;
  191. return $user_dir;
  192. }
  193. /**
  194. * Checks if the user's dedicated directory is accessible and writeable.
  195. *
  196. * @param $user
  197. * A Drupal user object.
  198. *
  199. * @return
  200. * TRUE if the user's directory is writeable. FALSE otherwise.
  201. *
  202. * @ingroup tripal_files_api
  203. */
  204. function tripal_is_user_files_dir_writeable($user) {
  205. $user_dir = tripal_get_user_files_dir($user);
  206. // First, make sure the directory exists.
  207. if (!file_prepare_directory($user_dir, FILE_CREATE_DIRECTORY)) {
  208. return FALSE;
  209. }
  210. // It has been reported that file_prepare_directory is not properly
  211. // detecting if the directory is writeable, so we'll do another
  212. // round of checks to be sure.
  213. if (!is_dir($user_dir)) {
  214. return FALSE;
  215. }
  216. if (!is_writeable($user_dir)) {
  217. return FALSE;
  218. }
  219. return TRUE;
  220. }