|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: UTF-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
import browsepy, flask, magic, os.path, pprint |
|
5
|
|
|
|
|
6
|
|
|
from .fileservice import FileService |
|
7
|
|
|
|
|
8
|
|
|
def get_all_mimetypes(): |
|
9
|
|
|
exceptions = [ "inode" ] |
|
10
|
|
|
excluded = [] |
|
11
|
|
|
mimetypes = [] |
|
12
|
|
|
f = open("/etc/mime.types") |
|
13
|
|
|
if f: |
|
14
|
|
|
for line in f.readlines(): |
|
15
|
|
|
if "\t" in line: |
|
16
|
|
|
mimetypes.append(line.split("\t")[0]) |
|
17
|
|
|
|
|
18
|
|
|
for mimetype in mimetypes: |
|
19
|
|
|
for exception in exceptions: |
|
20
|
|
|
if exception in mimetype: |
|
21
|
|
|
excluded.append(mimetype) |
|
22
|
|
|
break |
|
23
|
|
|
|
|
24
|
|
|
if excluded: |
|
25
|
|
|
print("All mimetypes can be shared except for these.") |
|
26
|
|
|
pprint.pprint(excluded) |
|
27
|
|
|
|
|
28
|
|
|
return [mimetype for mimetype in mimetypes if mimetype not in excluded] |
|
29
|
|
|
|
|
30
|
|
|
__basedir__= os.path.dirname(os.path.abspath(__file__)) |
|
31
|
|
|
|
|
32
|
|
|
share_blueprint = flask.Blueprint('share', __name__, |
|
33
|
|
|
url_prefix='/share', |
|
34
|
|
|
template_folder=os.path.join(__basedir__, 'templates'), |
|
35
|
|
|
static_folder=os.path.join(__basedir__, 'static'), |
|
36
|
|
|
) |
|
37
|
|
|
|
|
38
|
|
|
fileservice = FileService() |
|
39
|
|
|
root_url = "" |
|
40
|
|
|
|
|
41
|
|
|
@share_blueprint.route("/clear/<key>") |
|
42
|
|
|
def clear(key): |
|
43
|
|
|
if key == "secretkey": |
|
44
|
|
|
fileservice.clear() |
|
45
|
|
|
return "Shared files cleared.", 200 |
|
46
|
|
|
else: |
|
47
|
|
|
return "Not Authorized.", 403 |
|
48
|
|
|
|
|
49
|
|
|
@share_blueprint.route("/file/<path:path>") |
|
50
|
|
|
def file(path): |
|
51
|
|
|
file = browsepy.File.from_urlpath(path) |
|
52
|
|
|
hash = fileservice.add_file(file.path) |
|
53
|
|
|
if hash: |
|
54
|
|
|
url = root_url + "/share/get/" + hash |
|
55
|
|
|
ret = "Access the file from the address <a href=\"" + url + "\">" + os.path.basename(file.path) + "</a>" |
|
56
|
|
|
return ret, 200 |
|
57
|
|
|
else: |
|
58
|
|
|
return "File could not be shared.", 400 |
|
59
|
|
|
|
|
60
|
|
|
def register_plugin(manager): |
|
61
|
|
|
root_url = manager.app.config["root_url"] |
|
62
|
|
|
|
|
63
|
|
|
manager.register_blueprint(share_blueprint) |
|
64
|
|
|
|
|
65
|
|
|
style = manager.style_class('share.static', filename='css/browse.css') |
|
66
|
|
|
manager.register_widget(style) |
|
67
|
|
|
|
|
68
|
|
|
share_widget = manager.button_class(css='share') |
|
69
|
|
|
manager.register_action( |
|
70
|
|
|
"share.file", |
|
71
|
|
|
share_widget, |
|
72
|
|
|
mimetypes=get_all_mimetypes()) |
|
73
|
|
|
|
|
74
|
|
|
@manager.app.route("/share/get/<hash>") |
|
75
|
|
|
def give(hash): |
|
76
|
|
|
file = fileservice.get_file(hash) |
|
77
|
|
|
|
|
78
|
|
|
if not file: |
|
79
|
|
|
return "Not found.", 404 |
|
80
|
|
|
|
|
81
|
|
|
filepath = file["path"] |
|
82
|
|
|
if not os.path.exists(filepath): |
|
83
|
|
|
return "Not found.", 404 |
|
84
|
|
|
|
|
85
|
|
|
response = flask.Response( |
|
86
|
|
|
fileservice.file_read_generator(file), |
|
87
|
|
|
mimetype=magic.from_file(filepath, mime=True)) |
|
88
|
|
|
|
|
89
|
|
|
response.headers.add("Content-Disposition", |
|
90
|
|
|
"attachment; filename=\"" + os.path.basename(filepath) + "\""); |
|
91
|
|
|
response.headers.add("Content-Length", |
|
92
|
|
|
str(os.path.getsize(filepath))) |
|
93
|
|
|
return response |
|
94
|
|
|
|