HomeCache.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Files\Cache;
  30. use OCP\Files\Cache\ICacheEntry;
  31. class HomeCache extends Cache {
  32. /**
  33. * get the size of a folder and set it in the cache
  34. *
  35. * @param string $path
  36. * @param array $entry (optional) meta data of the folder
  37. * @return int
  38. */
  39. public function calculateFolderSize($path, $entry = null) {
  40. if ($path !== '/' and $path !== '' and $path !== 'files' and $path !== 'files_trashbin' and $path !== 'files_versions') {
  41. return parent::calculateFolderSize($path, $entry);
  42. } elseif ($path === '' or $path === '/') {
  43. // since the size of / isn't used (the size of /files is used instead) there is no use in calculating it
  44. return 0;
  45. }
  46. $totalSize = 0;
  47. if (is_null($entry)) {
  48. $entry = $this->get($path);
  49. }
  50. if ($entry && $entry['mimetype'] === 'httpd/unix-directory') {
  51. $id = $entry['fileid'];
  52. $query = $this->connection->getQueryBuilder();
  53. $query->selectAlias($query->func()->sum('size'), 'f1')
  54. ->from('filecache')
  55. ->where($query->expr()->eq('parent', $query->createNamedParameter($id)))
  56. ->andWhere($query->expr()->eq('storage', $query->createNamedParameter($this->getNumericStorageId())))
  57. ->andWhere($query->expr()->gte('size', $query->createNamedParameter(0)));
  58. $result = $query->execute();
  59. $row = $result->fetch();
  60. $result->closeCursor();
  61. if ($row) {
  62. [$sum] = array_values($row);
  63. $totalSize = 0 + $sum;
  64. $entry['size'] += 0;
  65. if ($entry['size'] !== $totalSize) {
  66. $this->update($id, ['size' => $totalSize]);
  67. }
  68. }
  69. $result->closeCursor();
  70. }
  71. return $totalSize;
  72. }
  73. /**
  74. * @param string $path
  75. * @return ICacheEntry
  76. */
  77. public function get($path) {
  78. $data = parent::get($path);
  79. if ($path === '' or $path === '/') {
  80. // only the size of the "files" dir counts
  81. $filesData = parent::get('files');
  82. if (isset($filesData['size'])) {
  83. $data['size'] = $filesData['size'];
  84. }
  85. }
  86. return $data;
  87. }
  88. }