Skip to content

Commit

Permalink
fix: Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlospaco committed Sep 17, 2024
1 parent 95d577f commit aab78da
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions storage3/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os
import tempfile

from datetime import datetime
from hashlib import md5

Expand All @@ -23,7 +25,7 @@ class FileStore:

def __init__(self):
self.storage = {}
self.store_name = "resumable_filestore.json"
self.disk_storage = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
self.reload_storage()

def fingerprint(self, file_info: FileInfo):
Expand All @@ -37,8 +39,10 @@ def fingerprint(self, file_info: FileInfo):
file_info["fingerprint"] = md5(data).hexdigest()

def persist(self):
with open(self.store_name, "w") as f:
json.dump(self.storage, f, indent=2)
with open(self.disk_storage.name, 'w') as f:
f.seek(0)
f.write(json.dumps(self.storage))
f.flush()

def mark_file(self, file_info: FileInfo):
"""Store file metadata in a in-memory storage"""
Expand All @@ -55,8 +59,11 @@ def get_file_info(self, filename):
return self.storage[filename]

def reload_storage(self):
with open(self.store_name) as f:
self.storage = json.load(f)
self.storage = {}
size = os.stat(self.disk_storage.name).st_size
if size > 0:
with open(self.disk_storage.name) as f:
self.storage = json.load(f)

def update_file_headers(self, filename, key, value):
file = self.get_file_info(filename)
Expand All @@ -80,9 +87,6 @@ def delete_file_headers(self, filename, key):
def get_file_headers(self, filename):
return self.get_file_info(filename)["headers"]

def get_file_storage_link(self, filename):
return self.get_file_headers(filename)["link"]

def open_file(self, filename: str, offset: int):
"""Open file in the specified offset
Parameters
Expand Down

0 comments on commit aab78da

Please sign in to comment.