HomeCache.php 2.6 KB

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