From 43e45f0592bd2527546d9261e26bf7beb4a2b1a0 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Mon, 19 Feb 2018 14:37:08 +0200 Subject: [PATCH] notifier support --- README.md | 26 ++++++++++++++++++++++++- code/app.py | 40 ++++++++++++++++++++++++++++++++++++++- code/notifier.py.template | 12 ++++++++++++ data/config.json.example | 1 + 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 code/notifier.py.template diff --git a/README.md b/README.md index 080543c..a956ae9 100644 --- a/README.md +++ b/README.md @@ -45,5 +45,29 @@ location /flees/ { - curl -F file=@my.file http://host/upload/[share name]/[hashed password] - "direct link" is a sharing link that does not require other passwords, and is unique to each file. (there should be no danger in sharing a file, and the password to rest of the files leaking) - + + +# custom notifier + +- Add a notifier module, and refer to it in the config with: + 'notifier': 'python-path-to-file:class-name' +- The class must have method `def notify(self, message)` + +Check the notifier.py.template for clues. +Flees will send notification on upload and download events, with a Dict like this: +``` +{ + "recipient": "share recipient", + "share": "name", + "filename": "file_path", + "operation": "direct_download" +} +``` + +Operation is one of download, direct_download, zip_download, or upload + + + + + diff --git a/code/app.py b/code/app.py index 38b3949..f2e197e 100644 --- a/code/app.py +++ b/code/app.py @@ -11,7 +11,7 @@ import hashlib import zipfile from revprox import ReverseProxied -__FLEES_VERSION__ = "20180218.1" +__FLEES_VERSION__ = "20180219.0" app = Flask(__name__) app.config.from_object(__name__) # Read config from json ! @@ -25,6 +25,16 @@ app.config['DATE_FORMAT'] = config_values['date_format'] app.config['UID'] = config_values['uid'] app.config['GID'] = config_values['gid'] app.config['DEBUG'] = config_values['debug'] +if 'notifier' in config_values: + if len(config_values['notifier']) > 0: + notifier_config = config_values['notifier'].split(":") + imported = getattr(__import__( + notifier_config[0], + fromlist=[notifier_config[1]] + ), + notifier_config[1] + ) + app.config['notifier'] = imported() app.secret_key = config_values['app_secret_key'] app.wsgi_app = ReverseProxied(app.wsgi_app) @@ -93,6 +103,12 @@ def upload(name = None, password = None): return "Overwrite forbidden", 403 file.save(filename) set_rights(filename) + notify({ + "recipient": get_or_none(share,'recipient'), + "share": name, + "filename": filename, + "operation": "upload" + }) if 'from_gui' in request.form: if request.form['from_gui'] == "true": return redirect(url_for('list_view',name=name)) @@ -169,6 +185,12 @@ def download_direct(name,password,filename): file_path = os.path.join(share['path'], filename) if not os.path.exists(file_path): return 'no such file', 404 + notify({ + "recipient": get_or_none(share,'recipient'), + "share": name, + "filename": file_path, + "operation": "direct_download" + }) return send_from_directory(directory=share['path'], filename=filename) @@ -183,6 +205,12 @@ def download_file(name,filename,password = None): file_path = os.path.join(share['path'], filename) if not os.path.exists(file_path): return 'no such file', 404 + notify({ + "recipient": get_or_none(share,'recipient'), + "share": name, + "filename": file_path, + "operation": "download" + }) return send_from_directory(directory=share['path'], filename=filename) @@ -199,6 +227,12 @@ def download_zip(name,password = None): return "Maximum ZIP size exceeded", 400 zip_clean() zip_path = zip_share(share) + notify({ + "recipient": get_or_none(share,'recipient'), + "share": name, + "filename": name + ".zip", + "operation": "zip_download" + }) return send_file( zip_path, as_attachment = True, @@ -342,6 +376,10 @@ def makedirs_rights(path): os.mkdir(current_path) set_rights(current_path) +def notify(msg): + if 'notifier' in app.config: + app.config['notifier'].notify(msg) + def set_rights(path): os.chown(path, app.config['UID'], app.config['GID']) diff --git a/code/notifier.py.template b/code/notifier.py.template new file mode 100644 index 0000000..9ac7d00 --- /dev/null +++ b/code/notifier.py.template @@ -0,0 +1,12 @@ +import sys +import time + +class Notifier: + def __init__(self): + self.output = open('notifier.log','at') + def notify(self, msg): + self.output.write(str(time.time())+"\n") + self.output.write(str(msg)+"\n") + self.output.flush() + + diff --git a/data/config.json.example b/data/config.json.example index 1a004fc..1379eb8 100644 --- a/data/config.json.example +++ b/data/config.json.example @@ -15,6 +15,7 @@ "gid": -1, "max_zip_size": 1000, "app_secret_key": "Cz2dw5NiRt3PSMFBSLTAJJi7kKrc4QU2CdQqEeOaU6", + "notifier": "", "__do_not_edit": "most likely you will not change anything after this line", "data_folder": "data", "shares_file": "data/shares.json",