homecache.php 2.6 KB

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