diff --git a/utils/flees-manager.py b/utils/flees-manager.py index 6fd598b..4c04e67 100755 --- a/utils/flees-manager.py +++ b/utils/flees-manager.py @@ -3,6 +3,7 @@ import argparse,json,sys,os from shutil import copyfile from tabulate import tabulate import hashlib +from datetime import datetime def get_folder_size(path): @@ -128,6 +129,7 @@ def add_share(shares, config, opts): date_object = datetime.strptime(opts.expire,"%Y-%m-%d %H:%M") except ValueError as e: print(e) + print("Date format error") sys.exit(1) share.update({ 'expire': opts.expire @@ -151,6 +153,64 @@ def add_share(shares, config, opts): print(json.dumps(share, indent = 2, sort_keys = True)) +def modify_share(shares, config, opts): + + print("Modifying share: %s"%( opts.name, )) + found = False + for i,share in enumerate(shares): + if share['name'] != opts.name: + continue + orig_share = share.copy() + print(json.dumps(share, indent = 2, sort_keys = True)) + found = True + break + if not found: + print('no such share') + sys.exit(1) + if opts.path != None: + share['path'] = opts.path + for attr in ('public','upload','direct_links','overwrite'): + if getattr(opts,attr) != None: + share[attr] = getattr(opts,attr) == 'true' + if opts.plain: + share['pass_plain'] = opts.plain + if opts.hashed: + share['pass_hash'] = hashlib.sha1(opts.hashed).hexdigest() + if opts.expire: + try: + date_object = datetime.strptime(opts.expire,"%Y-%m-%d %H:%M") + except ValueError as e: + print(e) + print("Date format error") + sys.exit(1) + share['expire'] = opts.expire + + if opts.insert: + shares[i] = share + shares_file = os.path.join(config['__root_path__'], opts.shares_file) + if os.path.exists(shares_file): + print("creating backup %s"%(shares_file+".bkp",)) + copyfile( + shares_file, + shares_file+".bkp" + ) + with open(shares_file,'wt') as fp: + json.dump(shares, fp, indent = 2, sort_keys = True) + print("Wrote file %s"%(shares_file,)) + else: + print("Share not saved anywhere. Save with -i") + modified = [] + for key in share: + if not key in orig_share: + modified.append(key) + continue + if orig_share[key] != share[key]: + modified.append(key) + continue + print("Modified values: %s"%(", ".join(modified))) + print(json.dumps(share, indent = 2, sort_keys = True)) + + def remove_share(shares,config,opts): name = opts.name share = [share for share in shares if share['name'] == name] @@ -301,7 +361,28 @@ def parse_options(): help = "Allow direct file sharing (password hash included in URL)") parser_add.add_argument('--pass-plain', action="store", dest="plain", default = False) parser_add.add_argument('--pass-hash', action="store", dest="hashed", default = False, - help = "Hashed password allows use of login links and direct downloads") + help = "Hashed password enables use of login links and direct downloads") + parser_add.add_argument('-e','--expire', action="store", dest="expire", default = False, + help = "expire date in format '%%Y-%%m-%%d %%H:%%M' ex. '2018-12-24 21:00'" + ) + parser_add.add_argument('-i','--insert', action="store_true", dest="insert", default = False, + help = "Insert new share directly in the shares.json file" + ) + ## Modify + parser_add = subparsers.add_parser('modify', help = "Modify share") + parser_add.add_argument('-n','--name', action="store", dest="name", required = True) + parser_add.add_argument('-p','--path', action="store", dest="path", default = None, + help= "path relative to data folder" + ) + parser_add.add_argument('-P','--public', action="store", dest="public", default = None, choices = ['true','false']) + parser_add.add_argument('-u','--upload', action="store", dest="upload", default = None, choices = ['true','false']) + parser_add.add_argument('-o','--overwrite', action="store", dest="overwrite", default = None, choices = ['true','false'], + help = "Disable file overwrites") + parser_add.add_argument('-d','--direct', action="store", dest="direct_links", default = None, choices = ['true','false'], + help = "Allow direct file sharing (password hash included in URL)") + parser_add.add_argument('--pass-plain', action="store", dest="plain", default = False) + parser_add.add_argument('--pass-hash', action="store", dest="hashed", default = False, + help = "Hashed password enables use of login links and direct downloads") parser_add.add_argument('-e','--expire', action="store", dest="expire", default = False, help = "expire date in format '%%Y-%%m-%%d %%H:%%M' ex. '2018-12-24 21:00'" ) @@ -347,6 +428,8 @@ if __name__ == "__main__": remove_share(shares,config,opts) elif opts.subparser_name == 'add': add_share(shares,config,opts) + elif opts.subparser_name == 'modify': + modify_share(shares,config,opts) elif opts.subparser_name == 'rest': print_rest_api(shares,config,opts)