@@ -119,6 +119,12 @@ def _checkpoints_class_default(self):
119
119
if safe. And if ``delete_to_trash`` is True, the directory won't be deleted.""" ,
120
120
)
121
121
122
+ count_directory_items = Bool (
123
+ False ,
124
+ config = True ,
125
+ help = "Whether to count items in directories. Disable for better performance with large/remote directories." ,
126
+ ).tag (config = True )
127
+
122
128
@default ("files_handler_class" )
123
129
def _files_handler_class_default (self ):
124
130
return AuthenticatedFileHandler
@@ -296,16 +302,21 @@ def _dir_model(self, path, content=True):
296
302
model ["size" ] = None
297
303
os_dir = self ._get_os_path (path )
298
304
dir_contents = os .listdir (os_dir )
299
- filtered_count = 0
300
- for name in dir_contents :
301
- try :
302
- os_path = os .path .join (os_dir , name )
303
- if self .should_list (name ) and (self .allow_hidden or not is_file_hidden (os_path )):
304
- filtered_count += 1
305
- except OSError as e :
306
- self .log .warning ("Error accessing %s: %s" , os .path .join (os_dir , name ), e )
307
305
308
- model ["item_count" ] = filtered_count
306
+ if self .count_directory_items :
307
+ filtered_count = 0
308
+ for name in dir_contents :
309
+ try :
310
+ os_path = os .path .join (os_dir , name )
311
+ if self .should_list (name ) and (
312
+ self .allow_hidden or not is_file_hidden (os_path )
313
+ ):
314
+ filtered_count += 1
315
+ except OSError as e :
316
+ self .log .warning ("Error accessing %s: %s" , os .path .join (os_dir , name ), e )
317
+
318
+ model ["item_count" ] = filtered_count
319
+
309
320
if content :
310
321
model ["content" ] = contents = []
311
322
for name in os .listdir (os_dir ):
@@ -779,16 +790,21 @@ async def _dir_model(self, path, content=True):
779
790
model ["size" ] = None
780
791
os_dir = self ._get_os_path (path )
781
792
dir_contents = await run_sync (os .listdir , os_dir )
782
- filtered_count = 0
783
- for name in dir_contents :
784
- try :
785
- os_path = os .path .join (os_dir , name )
786
- if self .should_list (name ) and (self .allow_hidden or not is_file_hidden (os_path )):
787
- filtered_count += 1
788
- except OSError as e :
789
- self .log .warning ("Error accessing %s: %s" , os .path .join (os_dir , name ), e )
790
793
791
- model ["item_count" ] = filtered_count
794
+ if self .count_directory_items :
795
+ filtered_count = 0
796
+ for name in dir_contents :
797
+ try :
798
+ os_path = os .path .join (os_dir , name )
799
+ if self .should_list (name ) and (
800
+ self .allow_hidden or not is_file_hidden (os_path )
801
+ ):
802
+ filtered_count += 1
803
+ except OSError as e :
804
+ self .log .warning ("Error accessing %s: %s" , os .path .join (os_dir , name ), e )
805
+
806
+ model ["item_count" ] = filtered_count
807
+
792
808
if content :
793
809
model ["content" ] = contents = []
794
810
for name in dir_contents :
0 commit comments