You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.7 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Files\Cache;
  29. use OCP\Files\Cache\ICacheEntry;
  30. class HomeCache extends Cache {
  31. /**
  32. * get the size of a folder and set it in the cache
  33. *
  34. * @param string $path
  35. * @param array $entry (optional) meta data of the folder
  36. * @return int
  37. */
  38. public function calculateFolderSize($path, $entry = null) {
  39. if ($path !== '/' and $path !== '' and $path !== 'files' and $path !== 'files_trashbin' and $path !== 'files_versions') {
  40. return parent::calculateFolderSize($path, $entry);
  41. } elseif ($path === '' or $path === '/') {
  42. // since the size of / isn't used (the size of /files is used instead) there is no use in calculating it
  43. return 0;
  44. }
  45. $totalSize = 0;
  46. if (is_null($entry)) {
  47. $entry = $this->get($path);
  48. }
  49. if ($entry && $entry['mimetype'] === 'httpd/unix-directory') {
  50. $id = $entry['fileid'];
  51. $sql = 'SELECT SUM(`size`) AS f1 ' .
  52. 'FROM `*PREFIX*filecache` ' .
  53. 'WHERE `parent` = ? AND `storage` = ? AND `size` >= 0';
  54. $result = \OC_DB::executeAudited($sql, [$id, $this->getNumericStorageId()]);
  55. if ($row = $result->fetchRow()) {
  56. $result->closeCursor();
  57. list($sum) = array_values($row);
  58. $totalSize = 0 + $sum;
  59. $entry['size'] += 0;
  60. if ($entry['size'] !== $totalSize) {
  61. $this->update($id, ['size' => $totalSize]);
  62. }
  63. }
  64. }
  65. return $totalSize;
  66. }
  67. /**
  68. * @param string $path
  69. * @return ICacheEntry
  70. */
  71. public function get($path) {
  72. $data = parent::get($path);
  73. if ($path === '' or $path === '/') {
  74. // only the size of the "files" dir counts
  75. $filesData = parent::get('files');
  76. if (isset($filesData['size'])) {
  77. $data['size'] = $filesData['size'];
  78. }
  79. }
  80. return $data;
  81. }
  82. }