From bfaa3467cbbc0b3c15a594e5c40c3c20f4caa8b5 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Fri, 26 Jan 2018 12:41:29 +0200 Subject: [PATCH] flees manager list shares and folders --- code/app.py | 3 +++ utils/flees-manager.py | 58 +++++++++++++++++++++++++++++++++++++++--- utils/requirements.txt | 1 + 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 utils/requirements.txt diff --git a/code/app.py b/code/app.py index c630000..2ebd66b 100644 --- a/code/app.py +++ b/code/app.py @@ -134,6 +134,9 @@ def download_direct(name,password,filename): (ok,share) = get_share(name, require_auth = False) if not ok: return share + allow_direct = get_or_none(share,'direct_links') + if allow_direct != True: + return 'Direct download not allowed', 403 token = get_direct_token(share, filename) if token == None: return 'Cannot generate token', 400 diff --git a/utils/flees-manager.py b/utils/flees-manager.py index e173a04..ad1fb77 100755 --- a/utils/flees-manager.py +++ b/utils/flees-manager.py @@ -1,17 +1,58 @@ #!/usr/bin/env python import argparse,json,sys,os from shutil import copyfile +from tabulate import tabulate +def get_folder_size(path): + + total_size = 0 + for dirpath, dirnames, filenames in os.walk(path): + for f in filenames: + fp = os.path.join(dirpath, f) + total_size += os.path.getsize(fp) + return total_size + +def get_or_no(key,d,no): + if key in d: + return d[key] + return no + + +def file_size_human(num): + for x in ['B','KB','MB','GB','TB']: + if num < 1024.0: + if x=='B': + return "%d %s" % (num, x) + return "%3.1f %s" % (num, x) + num /= 1024.0 def list_shares(shares): - print("\t".join( ('Name', 'Path') )) + table = [] + table.append(('Name', 'Path','Public','Password','Upload','Overwrite','Direct','Expire')) for share in shares: - print("\t".join( (share['name'], share['path']) )) + public = get_or_no('public',share, False) + password = 'pass_hash' in share or 'pass_plain' in share + upload = get_or_no('upload',share, False) + overwrite = get_or_no('overwrite',share, True) + direct = get_or_no('direct_links',share, False) if password else False + expire = get_or_no('expire',share, "-") + table.append(( + share['name'], + share['path']+"/", + public, + password, + upload, + overwrite, + direct, + expire + )) + print(tabulate(table, headers = "firstrow")) def list_folders(shares,config): folders = sorted(os.listdir(config['data_folder'])) - print("\t".join( ('Path','Share') )) + table = [] + table.append( ('Path','Share','Size','Unit') ) for folder in folders: full_path = os.path.join(config['data_folder'], folder) if not os.path.isdir(full_path): @@ -22,7 +63,16 @@ def list_folders(shares,config): if os.path.samefile(full_path, share_path): share_name = share['name'] break - print("\t".join( (folder, share_name) )) + (size_num, size_unit) = file_size_human(get_folder_size( + full_path + )).split(" ") + table.append(( + folder, + share_name, + size_num, + size_unit + )) + print(tabulate(table, headers = "firstrow")) def remove_share(name,shares,config): share = [share for share in shares if share['name'] == name] diff --git a/utils/requirements.txt b/utils/requirements.txt new file mode 100644 index 0000000..a5d5159 --- /dev/null +++ b/utils/requirements.txt @@ -0,0 +1 @@ +tabulate