@@ -939,16 +939,16 @@ export class SequelizeFolderRepository implements FolderRepository {
939939 totalSize : number ;
940940 isTotalSizeExact : boolean ;
941941 } > {
942- try {
943- const fileStatusCondition = [ FileStatus . EXISTS ] ;
942+ const MAX_FILES = 10000 ;
943+ const MAX_DEPTH = 50 ;
944944
945- const calculateStatsQuery = `
945+ const calculateStatsQuery = `
946946 WITH RECURSIVE folder_recursive AS (
947947 SELECT
948948 fl1.uuid,
949949 fl1.parent_uuid,
950- 1 as depth,
951- fl1.user_id as owner_id
950+ 1 AS depth,
951+ fl1.user_id AS owner_id
952952 FROM folders fl1
953953 WHERE fl1.uuid = :folderUuid
954954 AND fl1.removed = FALSE
@@ -962,57 +962,49 @@ export class SequelizeFolderRepository implements FolderRepository {
962962 fr.depth + 1,
963963 fr.owner_id
964964 FROM folders fl2
965- INNER JOIN folder_recursive fr
966- ON fr.uuid = fl2.parent_uuid
967- WHERE fr.depth < 100000
965+ INNER JOIN folder_recursive fr ON fr.uuid = fl2.parent_uuid
966+ WHERE fr.depth < :maxDepth
968967 AND fl2.user_id = fr.owner_id
969968 AND fl2.removed = FALSE
970969 AND fl2.deleted = FALSE
971970 ),
972- ranked_files AS (
973- SELECT
974- f.uuid,
975- f.size,
976- ROW_NUMBER() OVER (ORDER BY f.creation_time) as rn
971+ limited_files AS (
972+ SELECT f.uuid, f.size, fr.depth
977973 FROM folder_recursive fr
978- INNER JOIN files f
979- ON f.folder_uuid = fr.uuid
980- AND f.status IN (:fileStatusCondition)
974+ INNER JOIN files f ON f.folder_uuid = fr.uuid
975+ WHERE f.status = :fileStatus
976+ LIMIT :maxFiles
981977 )
982978 SELECT
983- COUNT(uuid) as file_count,
984- COALESCE(SUM(size), 0) as total_size,
985- MAX(rn) as total_files_found
986- FROM ranked_files
987- WHERE rn <= 10000;
979+ COUNT(*) AS file_count,
980+ COALESCE(SUM(size), 0) AS total_size,
981+ MAX(depth) AS max_depth
982+ FROM limited_files
988983 ` ;
989984
990- const [ [ result ] ] : any = await FolderModel . sequelize . query (
991- calculateStatsQuery ,
992- {
993- replacements : {
994- folderUuid,
995- fileStatusCondition,
996- } ,
985+ const [ [ result ] ] : any = await FolderModel . sequelize . query (
986+ calculateStatsQuery ,
987+ {
988+ replacements : {
989+ folderUuid,
990+ maxDepth : MAX_DEPTH ,
991+ fileStatus : FileStatus . EXISTS ,
992+ maxFiles : MAX_FILES + 1 ,
997993 } ,
998- ) ;
999-
1000- const fileCount = Number . parseInt ( result . file_count ) ;
1001- const totalFilesFound = Number . parseInt ( result . total_files_found || 0 ) ;
994+ } ,
995+ ) ;
1002996
1003- return {
1004- fileCount : Math . min ( fileCount , 1000 ) ,
1005- isFileCountExact : totalFilesFound <= 1000 ,
1006- totalSize : Number . parseInt ( result . total_size ) ,
1007- isTotalSizeExact : totalFilesFound < 10000 ,
1008- } ;
1009- } catch ( error ) {
1010- if ( error . original ?. code === '57014' ) {
1011- throw new CalculateFolderSizeTimeoutException ( ) ;
1012- }
997+ const fileCount = Number . parseInt ( result . file_count ) ;
998+ const hitFileLimit = fileCount > MAX_FILES ;
999+ const hitDepthLimit = Number . parseInt ( result . max_depth ) >= MAX_DEPTH ;
1000+ const isExact = ! hitFileLimit && ! hitDepthLimit ;
10131001
1014- throw error ;
1015- }
1002+ return {
1003+ fileCount : Math . min ( fileCount , MAX_FILES ) ,
1004+ totalSize : Number . parseInt ( result . total_size ) ,
1005+ isFileCountExact : isExact ,
1006+ isTotalSizeExact : isExact ,
1007+ } ;
10161008 }
10171009
10181010 async getDeletedFoldersWithNotDeletedChildren ( options : {
0 commit comments