notifier support

This commit is contained in:
Ville Rantanen
2018-02-19 14:37:08 +02:00
parent 1be6a713a6
commit 43e45f0592
4 changed files with 77 additions and 2 deletions

View File

@@ -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

View File

@@ -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'])

12
code/notifier.py.template Normal file
View File

@@ -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()

View File

@@ -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",