@@ -1015,7 +1015,6 @@ public function calculateFolderSize($path, $entry = null) {
10151015 /**
10161016 * inner function because we can't add new params to the public function without breaking any child classes
10171017 *
1018- * @param string $path
10191018 * @param array|null|ICacheEntry $entry (optional) meta data of the folder
10201019 * @param bool $ignoreUnknown don't mark the folder size as unknown if any of it's children are unknown
10211020 * @return int|float
@@ -1031,14 +1030,6 @@ protected function calculateFolderSizeInner(string $path, $entry = null, bool $i
10311030 $ query = $ this ->getQueryBuilder ();
10321031 $ query ->selectAlias ($ query ->func ()->sum ('size ' ), 'size_sum ' )
10331032 ->selectAlias ($ query ->func ()->min ('size ' ), 'size_min ' )
1034- ->selectAlias (
1035- $ query ->createFunction ('SUM(CASE WHEN unencrypted_size > 0 THEN unencrypted_size ELSE size END) ' ),
1036- 'unencrypted_sum '
1037- )
1038- ->selectAlias (
1039- $ query ->createFunction ('MIN(CASE WHEN unencrypted_size > 0 THEN unencrypted_size ELSE size END) ' ),
1040- 'unencrypted_min '
1041- )
10421033 ->selectAlias ($ query ->func ()->max ('unencrypted_size ' ), 'unencrypted_max ' )
10431034 ->from ('filecache ' )
10441035 ->whereStorageId ($ this ->getNumericStorageId ())
@@ -1055,17 +1046,33 @@ protected function calculateFolderSizeInner(string $path, $entry = null, bool $i
10551046 if ($ agg && $ agg ['size_sum ' ] !== null ) {
10561047 $ sum = Util::numericToNumber ($ agg ['size_sum ' ]);
10571048 $ min = Util::numericToNumber ($ agg ['size_min ' ]);
1058- $ unencryptedSum = Util::numericToNumber ($ agg ['unencrypted_sum ' ]);
1059- $ unencryptedMin = Util::numericToNumber ($ agg ['unencrypted_min ' ]);
10601049 $ unencryptedMax = Util::numericToNumber ($ agg ['unencrypted_max ' ] ?? 0 );
10611050
10621051 $ sum = 0 + $ sum ;
10631052 $ min = 0 + $ min ;
1064- if ($ min === -1 ) {
1065- $ totalSize = $ min ;
1066- } else {
1067- $ totalSize = $ sum ;
1053+ $ totalSize = ($ min === -1 ) ? $ min : $ sum ;
1054+
1055+ $ query = $ this ->getQueryBuilder ();
1056+ $ query ->select ('size ' , 'unencrypted_size ' )
1057+ ->from ('filecache ' )
1058+ ->whereStorageId ($ this ->getNumericStorageId ())
1059+ ->whereParent ($ id );
1060+ if ($ ignoreUnknown ) {
1061+ $ query ->andWhere ($ query ->expr ()->gte ('size ' , $ query ->createNamedParameter (0 )));
10681062 }
1063+ $ result = $ query ->executeQuery ();
1064+ $ rows = $ result ->fetchAll ();
1065+ $ result ->closeCursor ();
1066+
1067+ $ unencryptedSizes = array_map (function (array $ row ) {
1068+ $ u = Util::numericToNumber ($ row ['unencrypted_size ' ]);
1069+ $ s = Util::numericToNumber ($ row ['size ' ]);
1070+ return ($ u > 0 ) ? $ u : $ s ;
1071+ }, $ rows );
1072+
1073+ $ unencryptedSum = array_sum ($ unencryptedSizes );
1074+ $ unencryptedMin = $ unencryptedSizes ? min ($ unencryptedSizes ) : 0 ;
1075+
10691076 if ($ unencryptedMin === -1 || $ min === -1 ) {
10701077 $ unencryptedTotal = $ unencryptedMin ;
10711078 } else {
@@ -1077,15 +1084,16 @@ protected function calculateFolderSizeInner(string $path, $entry = null, bool $i
10771084 $ unencryptedMax = 0 ;
10781085 }
10791086
1080- // only set unencrypted size for a folder if any child entries have it set, or the folder is empty
1087+ // only set unencrypted size for a folder if any child entries have it set
1088+ // or if the folder is empty
10811089 $ shouldWriteUnEncryptedSize = $ unencryptedMax > 0 || $ totalSize === 0 || ($ entry ['unencrypted_size ' ] ?? 0 ) > 0 ;
10821090 if ($ entry ['size ' ] !== $ totalSize || (($ entry ['unencrypted_size ' ] ?? 0 ) !== $ unencryptedTotal && $ shouldWriteUnEncryptedSize )) {
10831091 if ($ shouldWriteUnEncryptedSize ) {
1084- // if all children have an unencrypted size of 0, just set the folder unencrypted size to 0 instead of summing the sizes
1092+ // if all children have an unencrypted size of 0
1093+ // just set the folder unencrypted size to 0 instead of summing the sizes
10851094 if ($ unencryptedMax === 0 ) {
10861095 $ unencryptedTotal = 0 ;
10871096 }
1088-
10891097 $ this ->update ($ id , [
10901098 'size ' => $ totalSize ,
10911099 'unencrypted_size ' => $ unencryptedTotal ,
0 commit comments