tripal.quotas.api.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Retrieves the user's quote and default days to expire.
  4. *
  5. * @param $uid
  6. * The User ID.
  7. *
  8. * @return
  9. * An associative array containing the quota and default days to
  10. * expire.
  11. */
  12. function tripal_get_user_quota($uid) {
  13. $quota = db_select('tripal_custom_quota', 'tgcq')->fields('tgcq', [
  14. 'custom_quota',
  15. 'custom_expiration'
  16. ])
  17. ->condition('uid', $uid)
  18. ->execute()
  19. ->fetchObject();
  20. if (! $quota) {
  21. $quota = new stdClass();
  22. $quota->custom_quota = variable_get('tripal_default_file_quota', pow(20, 6));
  23. $quota->custom_expiration = variable_get('tripal_default_file_expiration', '60');
  24. }
  25. return $quota;
  26. }
  27. /**
  28. * Sets a user's file space quota and default file expiration.
  29. *
  30. * @param $uid The
  31. * User ID for whom the quota will be set.
  32. * @param $quota The
  33. * quota
  34. * @param
  35. * $expriation
  36. *
  37. * @return The inserted record.
  38. */
  39. function tripal_set_user_quota($uid, $quota, $expriation) {
  40. $values = [
  41. 'uid' => $uid,
  42. 'custom_quota' => $quota,
  43. 'custom_expiration' => $expriation
  44. ];
  45. return db_insert('tripal_custom_quota')->fields($values)->execute();
  46. }
  47. /**
  48. * Removes a user's file space and default file expiration.
  49. *
  50. * @param $uid The
  51. * User ID for whom the quota will be removed.
  52. *
  53. * @return
  54. */
  55. function tripal_remove_user_quota($uid) {
  56. db_delete('tripal_custom_quota')->condition('uid', $uid)->execute();
  57. }
  58. /**
  59. * Retrieves the current size of all files uploaded by the user.
  60. *
  61. * @param $uid The
  62. * User ID.
  63. *
  64. * @return The total number of bytes currently used.
  65. */
  66. function tripal_get_user_usage($uid) {
  67. // Get the user's current file usage
  68. $sql = "
  69. SELECT SUM(filesize) FROM (
  70. SELECT DISTINCT FM.fid, FM.filename, FM.filesize
  71. FROM file_usage FU
  72. INNER JOIN file_managed FM ON FM.fid = FU.fid and FU.module = 'tripal'
  73. WHERE FM.uid = :uid) AS foo
  74. ";
  75. $total_usage = db_query($sql, [':uid' => $uid])->fetchField();
  76. return ($total_usage);
  77. }
  78. /**
  79. * Checks if a file needs to be expired.
  80. */
  81. function tripal_expire_files(TripalJob $job = NULL) {
  82. $results = db_select('tripal_expiration_files', 'tgfe')
  83. ->fields('tgfe')
  84. ->execute();
  85. while ($result = $results->fetchObject()) {
  86. if (time() > $result->expiration_date) {
  87. $file = file_load($result->fid);
  88. if ($file) {
  89. if ($job) {
  90. $job->logMessage('File "' . $file->filename . '" has expired. Removing...');
  91. }
  92. // First remove the file from the file system.
  93. file_delete($file, TRUE);
  94. // Remove the file from our file expiration table.
  95. $query = db_delete('tripal_expiration_files');
  96. $query->condition('fid', $result->fid);
  97. $query->execute();
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Resets the expiration data of a file managed by Tripal.
  104. *
  105. * @param $fid
  106. * The file ID of the file to reset.
  107. *
  108. * @return
  109. * TRUE on success, FALSE on failure.
  110. */
  111. function tripal_reset_file_expiration($fid) {
  112. $file = file_load($fid);
  113. try {
  114. $quota = tripal_get_user_quota($file->uid);
  115. $custom_expiration = $quota->custom_expiration;
  116. $expiration_date = time() + $custom_expiration * 24 * 60 * 60;
  117. db_delete('tripal_expiration_files')
  118. ->condition('fid', $fid)
  119. ->execute();
  120. db_insert('tripal_expiration_files')
  121. ->fields([
  122. 'fid' => $file->fid,
  123. 'expiration_date' => $expiration_date,
  124. ])
  125. ->execute();
  126. }
  127. catch (Exception $e) {
  128. tripal_report_error('trp_quota', TRIPAL_ERROR, $e->getMessage());
  129. return FALSE;
  130. }
  131. return TRUE;
  132. }