Add in more garbage collecting and initial implementation of file download.

This commit is contained in:
2023-10-29 17:00:19 -04:00
parent ae8bafc8c3
commit e28f647a60
2 changed files with 74 additions and 9 deletions

View File

@@ -2,7 +2,6 @@ import ElementTree
import urequests
from urequests import auth
import gc
import uos
class uNextcloud:
@@ -41,12 +40,35 @@ class uNextcloud:
content_type = prop.find_first_by_tag("getcontenttype")
if content_type != None:
file_list.append(self.File(href.text, content_type.text))
response.close()
gc.collect()
return file_list
except AttributeError:
response.close()
gc.collect()
return None
else:
response.close()
gc.collect()
return None
def download_file_to_path(self, folder_path, file, destination_path):
pass
def download_file_to_path(self, url_path, destination_path):
response = urequests.request("GET", self.url+url_path, auth=urequests.auth.HTTPBasicAuth(self.username, self.password))
if 200 <= response.status_code < 300:
dest = open(destination_path, 'wb')
data = bytearray(1024)
len = 0
while True:
print(f"downloaded {len}")
gc.collect()
if response.raw.readinto(data) == 0:
break
len += 1024
dest.write(data)
print(gc.mem_free())
dest.close()
response.close()
gc.collect()
else:
response.close()
raise Exception()