tripal.files.api.inc

Provides an application programming interface (API) for managing files within the Tripal data directory structure.

File

tripal/api/tripal.files.api.inc
View source
  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. function tripal_format_bytes($bytes, $precision = 2) {
  128. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  129. $bytes = max($bytes, 0);
  130. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  131. $pow = min($pow, count($units) - 1);
  132. // Uncomment one of the following alternatives
  133. $bytes /= pow(1000, $pow);
  134. // $bytes /= (1 << (10 * $pow));
  135. return round($bytes, $precision) . '' . $units[$pow];
  136. }
  137. /**
  138. * Retrieves the list of files uploaded by a user.
  139. * @param $uid
  140. * The ID of the user whose files should be retrieved.
  141. * @param $allowed_types
  142. * A list of valid extensions to restrict the files to.
  143. * @param $module
  144. * The name of the module that is managing the file.
  145. *
  146. * @return
  147. * A list of file objects.
  148. */
  149. function tripal_get_user_uploads($uid, $allowed_types = array(), $module = 'tripal') {
  150. $user = user_load($uid);
  151. $query = db_select('file_managed', 'FM');
  152. $query->fields('FM', array('fid'));
  153. $query->distinct();
  154. $query->condition('FM.uid', $user->uid);
  155. $query->innerJoin('file_usage', 'FU', "FU.fid = FM.fid");
  156. $query->condition('FU.module', $module);
  157. $query->orderBy('FM.filename');
  158. $files = $query->execute();
  159. $files_list = [];
  160. while ($fid = $files->fetchField()) {
  161. $file = file_load($fid);
  162. foreach ($allowed_types as $type) {
  163. if (preg_match('/\.' . $type . '$/', $file->filename)) {
  164. $files_list[$fid] = $file;
  165. }
  166. }
  167. }
  168. return $files_list;
  169. }