|
37 | 37 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
38 | 38 | * |
39 | 39 | */ |
| 40 | + |
40 | 41 | namespace OC\Files\Cache; |
41 | 42 |
|
42 | 43 | use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
@@ -188,6 +189,7 @@ public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader |
188 | 189 | $data['fileid'] = (int)$data['fileid']; |
189 | 190 | $data['parent'] = (int)$data['parent']; |
190 | 191 | $data['size'] = 0 + $data['size']; |
| 192 | + $data['unencrypted_size'] = 0 + ($data['unencrypted_size'] ?? 0); |
191 | 193 | $data['mtime'] = (int)$data['mtime']; |
192 | 194 | $data['storage_mtime'] = (int)$data['storage_mtime']; |
193 | 195 | $data['encryptedVersion'] = (int)$data['encrypted']; |
@@ -428,7 +430,7 @@ public function update($id, array $data) { |
428 | 430 | protected function normalizeData(array $data): array { |
429 | 431 | $fields = [ |
430 | 432 | 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', |
431 | | - 'etag', 'permissions', 'checksum', 'storage']; |
| 433 | + 'etag', 'permissions', 'checksum', 'storage', 'unencrypted_size']; |
432 | 434 | $extensionFields = ['metadata_etag', 'creation_time', 'upload_time']; |
433 | 435 |
|
434 | 436 | $doNotCopyStorageMTime = false; |
@@ -873,27 +875,56 @@ public function calculateFolderSize($path, $entry = null) { |
873 | 875 | $id = $entry['fileid']; |
874 | 876 |
|
875 | 877 | $query = $this->getQueryBuilder(); |
876 | | - $query->selectAlias($query->func()->sum('size'), 'f1') |
877 | | - ->selectAlias($query->func()->min('size'), 'f2') |
| 878 | + $query->select('size', 'unencrypted_size') |
878 | 879 | ->from('filecache') |
879 | | - ->whereStorageId($this->getNumericStorageId()) |
880 | 880 | ->whereParent($id); |
881 | 881 |
|
882 | 882 | $result = $query->execute(); |
883 | | - $row = $result->fetch(); |
| 883 | + $rows = $result->fetchAll(); |
884 | 884 | $result->closeCursor(); |
885 | 885 |
|
886 | | - if ($row) { |
887 | | - [$sum, $min] = array_values($row); |
| 886 | + if ($rows) { |
| 887 | + $sizes = array_map(function (array $row) { |
| 888 | + return (int)$row['size']; |
| 889 | + }, $rows); |
| 890 | + $unencryptedOnlySizes = array_map(function (array $row) { |
| 891 | + return (int)$row['unencrypted_size']; |
| 892 | + }, $rows); |
| 893 | + $unencryptedSizes = array_map(function (array $row) { |
| 894 | + return (int)(($row['unencrypted_size'] > 0) ? $row['unencrypted_size']: $row['size']); |
| 895 | + }, $rows); |
| 896 | + |
| 897 | + $sum = array_sum($sizes); |
| 898 | + $min = min($sizes); |
| 899 | + |
| 900 | + $unencryptedSum = array_sum($unencryptedSizes); |
| 901 | + $unencryptedMin = min($unencryptedSizes); |
| 902 | + $unencryptedMax = max($unencryptedOnlySizes); |
| 903 | + |
888 | 904 | $sum = 0 + $sum; |
889 | 905 | $min = 0 + $min; |
890 | 906 | if ($min === -1) { |
891 | 907 | $totalSize = $min; |
892 | 908 | } else { |
893 | 909 | $totalSize = $sum; |
894 | 910 | } |
| 911 | + if ($unencryptedMin === -1 || $min === -1) { |
| 912 | + $unencryptedTotal = $unencryptedMin; |
| 913 | + } else { |
| 914 | + $unencryptedTotal = $unencryptedSum; |
| 915 | + } |
895 | 916 | if ($entry['size'] !== $totalSize) { |
896 | | - $this->update($id, ['size' => $totalSize]); |
| 917 | + // only set unencrypted size for a folder if any child entries have it set |
| 918 | + if ($unencryptedMax > 0) { |
| 919 | + $this->update($id, [ |
| 920 | + 'size' => $totalSize, |
| 921 | + 'unencrypted_size' => $unencryptedTotal, |
| 922 | + ]); |
| 923 | + } else { |
| 924 | + $this->update($id, [ |
| 925 | + 'size' => $totalSize, |
| 926 | + ]); |
| 927 | + } |
897 | 928 | } |
898 | 929 | } |
899 | 930 | } |
|
0 commit comments