support for autoremoving X days old files
This commit is contained in:
@@ -20,7 +20,7 @@ def get_root_path(opts):
|
||||
|
||||
def list_shares(shares,opts):
|
||||
table = []
|
||||
header = ('Name', 'Path','Public','Password','Token','Upload','Overwrite','Direct','Expire','Recipient','Description')
|
||||
header = ('Name', 'Path','Public','Password','Token','Upload','Overwrite','Direct','Expire','AutoRemove','Recipient','Description')
|
||||
short_header = ('Name', 'Path','Pub','Pwd','Up','Drct','Exp')
|
||||
for share in shares:
|
||||
public = get_or_none('public',share, False)
|
||||
@@ -30,6 +30,11 @@ def list_shares(shares,opts):
|
||||
tokens = len(share['tokens']) > 0
|
||||
upload = get_or_none('upload',share, False)
|
||||
overwrite = get_or_none('overwrite',share, True)
|
||||
autoremove = get_or_none('autoremove', share, 0)
|
||||
if autoremove > 0:
|
||||
autoremove = "%d d"%( autoremove, )
|
||||
else:
|
||||
autoremove = "-"
|
||||
direct = get_or_none('direct_links',share, False) if password else False
|
||||
expire = get_or_none('expire',share, "-")
|
||||
if not opts.verbose:
|
||||
@@ -45,6 +50,7 @@ def list_shares(shares,opts):
|
||||
overwrite,
|
||||
direct,
|
||||
expire,
|
||||
autoremove,
|
||||
get_or_none('recipient', share, "")[0:20],
|
||||
description
|
||||
))
|
||||
@@ -61,7 +67,6 @@ def list_shares(shares,opts):
|
||||
)
|
||||
|
||||
|
||||
|
||||
def list_folders(shares,config):
|
||||
if 'data_folder' not in config:
|
||||
print("data_folder not defined in config")
|
||||
@@ -174,6 +179,59 @@ def list_versions(shares, config, opts):
|
||||
print(tabulate(table, headers = header))
|
||||
|
||||
|
||||
def list_autoremove(shares, config, opts):
|
||||
if 'data_folder' not in config:
|
||||
print("data_folder not defined in config")
|
||||
sys.exit(1)
|
||||
data_folder = os.path.join(config['__root_path__'], config['data_folder'])
|
||||
table = []
|
||||
header = ['Path', 'Size', 'Age', 'ToDelete']
|
||||
for share in shares:
|
||||
if opts.name:
|
||||
if share['name'] != opts.name:
|
||||
continue
|
||||
share_folder = os.path.join(
|
||||
data_folder,
|
||||
share['path'],
|
||||
)
|
||||
autoremove = get_or_none('autoremove', share, 0)
|
||||
if autoremove == 0:
|
||||
autoremove = False
|
||||
del header[-1]
|
||||
for filename in iter_folder_files(share_folder):
|
||||
full_path = os.path.join(share_folder, filename)
|
||||
if os.path.isdir(full_path):
|
||||
continue
|
||||
size = os.path.getsize(full_path)
|
||||
size_str = file_size_human(
|
||||
size,
|
||||
HTML = False
|
||||
)
|
||||
age, age_str = file_age(full_path)
|
||||
to_delete = age.days >= autoremove
|
||||
if not autoremove:
|
||||
to_delete = False
|
||||
del_str = "Yes" if to_delete else "No"
|
||||
if opts.delete:
|
||||
if to_delete:
|
||||
del_str = "Deleted"
|
||||
os.remove(full_path)
|
||||
row_data = [
|
||||
os.path.join(
|
||||
share['path'],
|
||||
filename
|
||||
),
|
||||
size_str,
|
||||
age_str,
|
||||
del_str
|
||||
]
|
||||
if not autoremove:
|
||||
del row_data[-1]
|
||||
table.append(row_data)
|
||||
table.sort(key = lambda x: x[0])
|
||||
print(tabulate(table, headers = header))
|
||||
|
||||
|
||||
def add_share(shares, config, opts):
|
||||
|
||||
# Make name and path safe:
|
||||
@@ -194,7 +252,8 @@ def add_share(shares, config, opts):
|
||||
'overwrite': opts.overwrite,
|
||||
'direct_links': opts.direct,
|
||||
'description': opts.description,
|
||||
'recipient': opts.recipient
|
||||
'recipient': opts.recipient,
|
||||
'autoremove': opts.autoremove
|
||||
}
|
||||
|
||||
if opts.password:
|
||||
@@ -280,6 +339,8 @@ def modify_share(shares, config, opts):
|
||||
share['description'] = opts.description
|
||||
if opts.recipient != None:
|
||||
share['recipient'] = opts.recipient
|
||||
if opts.autoremove != None:
|
||||
share['autoremove'] = opts.autoremove
|
||||
# REMOVE password
|
||||
if opts.password == "":
|
||||
if 'pass_plain' in share:
|
||||
@@ -641,6 +702,25 @@ def parse_options():
|
||||
help = "Do not actually delete files.")
|
||||
parser_versions.add_argument('-n','--name', action="store", dest="name", required = False, default = None,
|
||||
help = "Show / Delete only this share versions. If omitted, applies to all shares")
|
||||
## list autoremove
|
||||
parser_autoremove = subparsers.add_parser(
|
||||
'ages',
|
||||
help = "List the file ages in shares, and their disk usage"
|
||||
)
|
||||
parser_autoremove.add_argument(
|
||||
'--delete',
|
||||
action = "store_true",
|
||||
dest = "delete",
|
||||
default = False,
|
||||
help = "Delete files older than share autoremove value."
|
||||
)
|
||||
parser_autoremove.add_argument(
|
||||
'-n','--name',
|
||||
action = "store",
|
||||
dest = "name",
|
||||
required = True,
|
||||
default = None,
|
||||
help = "Share name to show / delete from.")
|
||||
## Show
|
||||
parser_show = subparsers.add_parser('show', help = "Show share")
|
||||
parser_show.add_argument('-P', action="store_true", dest="show_password", default = False,
|
||||
@@ -654,9 +734,18 @@ def parse_options():
|
||||
)
|
||||
## Add
|
||||
parser_add = subparsers.add_parser('add', help = "Add a share")
|
||||
parser_add.add_argument('-n','--name', action="store", dest="name", required = True)
|
||||
parser_add.add_argument('-p','--path', action="store", dest="path", required = True,
|
||||
help= "path relative to data folder"
|
||||
parser_add.add_argument(
|
||||
'-n','--name',
|
||||
action = "store",
|
||||
dest = "name",
|
||||
required = True
|
||||
)
|
||||
parser_add.add_argument(
|
||||
'-p','--path',
|
||||
action = "store",
|
||||
dest = "path",
|
||||
required = True,
|
||||
help = "path relative to data folder"
|
||||
)
|
||||
parser_add.add_argument('-D','--description', action="store", dest="description", default = "",
|
||||
help= "Describe the contents"
|
||||
@@ -674,6 +763,14 @@ def parse_options():
|
||||
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(
|
||||
'--rm','--autoremove',
|
||||
action = "store",
|
||||
dest = "autoremove",
|
||||
default = 0,
|
||||
type = int,
|
||||
help = "Remove files older than N days. 0 disables the feature."
|
||||
)
|
||||
parser_add.add_argument('-r','--recipient', action="store", dest="recipient", default = "",
|
||||
help= "Recipient for notifications (if enabled)"
|
||||
)
|
||||
@@ -702,6 +799,14 @@ def parse_options():
|
||||
parser_modify.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'. Set as empty string to remove expiration."
|
||||
)
|
||||
parser_modify.add_argument(
|
||||
'--rm','--autoremove',
|
||||
action = "store",
|
||||
dest = "autoremove",
|
||||
default = None,
|
||||
type = int,
|
||||
help = "Remove files older than N days. 0 disables the feature."
|
||||
)
|
||||
parser_modify.add_argument('-r','--recipient', action="store", dest="recipient", default = None,
|
||||
help= "Recipient for notifications (if enabled)"
|
||||
)
|
||||
@@ -758,6 +863,8 @@ if __name__ == "__main__":
|
||||
list_shares(shares,opts)
|
||||
if opts.subparser_name == 'versions':
|
||||
list_versions(shares,config,opts)
|
||||
if opts.subparser_name == 'ages':
|
||||
list_autoremove(shares,config,opts)
|
||||
elif opts.subparser_name == 'folders':
|
||||
list_folders(shares,config)
|
||||
elif opts.subparser_name == 'show':
|
||||
|
||||
Reference in New Issue
Block a user