@@ -166,30 +166,38 @@ async def get_contents(self, drive_name, path):
166166 if path == '/' :
167167 path = ''
168168 try :
169- currentObject = os .path .basename (path ) if os .path .basename (path ) is not None else ''
170-
171- # check if we are listing contents of a directory
172- if currentObject .find ('.' ) == - 1 :
173- data = []
174- # using Arrow lists as they are recommended for large results
175- # sream will be an async iterable of RecordBatch
176- stream = obs .list (self ._content_managers [drive_name ], path , chunk_size = 100 , return_arrow = True )
177- async for batch in stream :
178- contents_list = pyarrow .record_batch (batch ).to_pylist ()
179- for object in contents_list :
180- data .append ({
181- "path" : object ["path" ],
182- "last_modified" : object ["last_modified" ].isoformat (),
183- "size" : object ["size" ],
184- })
185- else :
169+ data = []
170+ isDir = False
171+ emptyDir = True # assume we are dealing with an empty directory
172+
173+ # using Arrow lists as they are recommended for large results
174+ # stream will be an async iterable of RecordBatch
175+ stream = obs .list (self ._content_managers [drive_name ], path , chunk_size = 100 , return_arrow = True )
176+ async for batch in stream :
177+ # check once if we are dealing with a directory
178+ if isDir is False and batch :
179+ isDir = True
180+ emptyDir = False
181+
182+ contents_list = pyarrow .record_batch (batch ).to_pylist ()
183+ for object in contents_list :
184+ data .append ({
185+ "path" : object ["path" ],
186+ "last_modified" : object ["last_modified" ].isoformat (),
187+ "size" : object ["size" ],
188+ })
189+
190+ # check if we are dealing with an empty drive
191+ if isDir is False and path != '' :
186192 content = b""
187193 # retrieve contents of object
188194 obj = await obs .get_async (self ._content_managers [drive_name ], path )
189195 stream = obj .stream (min_chunk_size = 5 * 1024 * 1024 ) # 5MB sized chunks
190196 async for buf in stream :
197+ if emptyDir is True and buf :
198+ emptyDir = False
191199 content += buf
192-
200+
193201 # retrieve metadata of object
194202 metadata = await obs .head_async (self ._content_managers [drive_name ], path )
195203
@@ -207,6 +215,12 @@ async def get_contents(self, drive_name, path):
207215 "last_modified" : metadata ["last_modified" ].isoformat (),
208216 "size" : metadata ["size" ]
209217 }
218+
219+ # dealing with the case of an empty directory
220+ # TO DO: find better way to check
221+ if emptyDir is True and os .path .basename (path ).find ('.' ) == - 1 :
222+ data = []
223+
210224 response = {
211225 "data" : data
212226 }
0 commit comments