Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anjiurine committed Nov 25, 2023
1 parent 203f0b0 commit b97eb84
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 62 deletions.
11 changes: 2 additions & 9 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

def remove_temp_files():
save_path = "./cache/"
if not os.path.exists(save_path):
os.makedirs(save_path)
for file_name in os.listdir(save_path):
file_path = os.path.join(save_path, file_name)
try:
Expand Down Expand Up @@ -44,15 +46,6 @@ def run_cli_mode():

app = Flask(__name__, instance_relative_config=True, template_folder='templates')

def remove_temp_files():
save_path = "./cache/"
for file_name in os.listdir(save_path):
file_path = os.path.join(save_path, file_name)
try:
os.unlink(file_path)
except Exception as e:
logging.warning(f"Error removing temporary file: {str(e)}")

@app.route('/')
def list_files():
file_list = mongo_utils.list_files(client_list, cli_output=False)
Expand Down
48 changes: 7 additions & 41 deletions utils/mongo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def upload_file(client_list, file_path):
file_sha256 = hashlib.sha256(file_data).hexdigest()
file_name, file_ext = os.path.basename(file_path).split(".")
file_size = os.path.getsize(file_path)
if file_size < 1024:
file_size = f"{(file_size)} B"
elif file_size > 1024:
file_size = f"{round(file_size / 1024)} KB"
elif file_size > 1024 * 1024:
file_size = f"{round(file_size / 1024 / 1024)} MB"
else: file_size = f"{round(file_size / 1024 / 1024 / 1024)} GB"
file_7z_path = file_path + ".7z"
with py7zr.SevenZipFile(file_7z_path, 'w') as archive:
archive.write(file_path, file_name + "." + file_ext)
Expand Down Expand Up @@ -181,47 +188,6 @@ def search_file(client_list, keyword, cli_output=True):
else:
return file_list

def get_storage_info(client_list, selected_instance=None, cli_output=True):
total_storage = 0
total_free_storage = 0
total_used_storage = 0
storage_info_list = []

for i, client in enumerate(client_list, start=1):
if selected_instance is not None and i != selected_instance:
continue

db = client.files
stats = db.command('dbstats')
storage_info = {
"instance_uri": client.address[0],
"total_storage": stats["storageSize"],
"free_storage": stats["storageSize"] - stats["dataSize"],
"used_storage": stats["dataSize"]
}
storage_info_list.append(storage_info)

total_storage += stats["storageSize"]
total_free_storage += stats["storageSize"] - stats["dataSize"]
total_used_storage += stats["dataSize"]

if selected_instance is not None:
break

if selected_instance is None:
storage_info_list = {
"total_storage": total_storage,
"free_storage": total_free_storage,
"used_storage": total_used_storage
}

if cli_output:
headers = ["Instance URI", "Total Storage", "Free Storage", "Used Storage"]
table_data = [(info["instance_uri"], info["total_storage"], info["free_storage"], info["used_storage"]) for info in storage_info_list]
print(tabulate.tabulate(table_data, headers=headers, tablefmt="grid"))
else:
return storage_info_list

def reindex_files(client_list, save_path='./cache'):
if not os.path.exists(save_path):
os.makedirs(save_path)
Expand Down
12 changes: 0 additions & 12 deletions utils/user_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,6 @@ def parse_input(user_input, client_list):
except Exception as e:
print_error(e)

elif command == "fs" and len(user_input) in (1, 2):
try:
storage_info_list = mongo_utils.get_storage_info(client_list, cli_output=False)
if storage_info_list:
headers = ["Instance URI", "Total Storage", "Free Storage", "Used Storage"]
table_data = [(info["instance_uri"], info["total_storage"], info["free_storage"], info["used_storage"]) for info in storage_info_list]
print(tabulate.tabulate(table_data, headers=headers, tablefmt="grid"))
else:
print_error("Failed to retrieve storage information")
except Exception as e:
print_error(e)

elif command == "re" or command == "reindex" and len(user_input) == 1:
confirmation = input("Are you sure you want to reindex all files? This will download and re-upload all files. (y/n): ").lower()
if confirmation == 'yes' or confirmation == 'y':
Expand Down

0 comments on commit b97eb84

Please sign in to comment.