forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebinterface.py
More file actions
157 lines (114 loc) · 4.53 KB
/
webinterface.py
File metadata and controls
157 lines (114 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
@author: RaNaN
"""
import sys
import module.common.pylgettext as gettext
import os
from os.path import join, abspath, dirname, exists
from os import makedirs
PROJECT_DIR = abspath(dirname(__file__))
PYLOAD_DIR = abspath(join(PROJECT_DIR, "..", ".."))
sys.path.append(PYLOAD_DIR)
from module import InitHomeDir
from module.utils import decode, formatSize
import bottle
from bottle import run, app
from jinja2 import Environment, FileSystemLoader, PrefixLoader, FileSystemBytecodeCache
from middlewares import StripPathMiddleware, GZipMiddleWare, PrefixMiddleware
SETUP = None
PYLOAD = None
from module.web import ServerThread
if not ServerThread.core:
if ServerThread.setup:
SETUP = ServerThread.setup
config = SETUP.config
else:
raise Exception("Could not access pyLoad Core")
else:
PYLOAD = ServerThread.core.api
config = ServerThread.core.config
from module.common.JsEngine import JsEngine
JS = JsEngine()
TEMPLATE = config.get('webinterface', 'template')
DL_ROOT = config.get('general', 'download_folder')
LOG_ROOT = config.get('log', 'log_folder')
PREFIX = config.get('webinterface', 'prefix')
if PREFIX:
PREFIX = PREFIX.rstrip("/")
if not PREFIX.startswith("/"):
PREFIX = "/" + PREFIX
DEBUG = config.get("general", "debug_mode") or "-d" in sys.argv or "--debug" in sys.argv
bottle.debug(DEBUG)
cache = join("tmp", "jinja_cache")
if not exists(cache):
makedirs(cache)
bcc = FileSystemBytecodeCache(cache, '%s.cache')
loader = PrefixLoader({
"default": FileSystemLoader(join(PROJECT_DIR, "templates", "default")),
'js': FileSystemLoader(join(PROJECT_DIR, 'media', 'js'))
})
env = Environment(loader=loader, extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'], trim_blocks=True, auto_reload=False,
bytecode_cache=bcc)
from filters import quotepath, path_make_relative, path_make_absolute, truncate, date
env.filters["quotepath"] = quotepath
env.filters["truncate"] = truncate
env.filters["date"] = date
env.filters["path_make_relative"] = path_make_relative
env.filters["path_make_absolute"] = path_make_absolute
env.filters["decode"] = decode
env.filters["type"] = lambda x: str(type(x))
env.filters["formatsize"] = formatSize
env.filters["getitem"] = lambda x, y: x.__getitem__(y)
if PREFIX:
env.filters["url"] = lambda x: x
else:
env.filters["url"] = lambda x: PREFIX + x if x.startswith("/") else x
gettext.setpaths([join(os.sep, "usr", "share", "pyload", "locale"), None])
translation = gettext.translation("django", join(PYLOAD_DIR, "locale"),
languages=[config.get("general", "language"), "en"],fallback=True)
translation.install(True)
env.install_gettext_translations(translation)
from beaker.middleware import SessionMiddleware
session_opts = {
'session.type': 'file',
'session.cookie_expires': False,
'session.data_dir': './tmp',
'session.auto': False
}
web = StripPathMiddleware(SessionMiddleware(app(), session_opts))
web = GZipMiddleWare(web)
if PREFIX:
web = PrefixMiddleware(web, prefix=PREFIX)
import pyload_app
import json_app
import cnl_app
import api_app
def run_simple(host="0.0.0.0", port="8000"):
run(app=web, host=host, port=port, quiet=True)
def run_lightweight(host="0.0.0.0", port="8000"):
run(app=web, host=host, port=port, quiet=True, server="bjoern")
def run_threaded(host="0.0.0.0", port="8000", theads=3, cert="", key=""):
from wsgiserver import CherryPyWSGIServer
if cert and key:
CherryPyWSGIServer.ssl_certificate = cert
CherryPyWSGIServer.ssl_private_key = key
CherryPyWSGIServer.numthreads = theads
from utils import CherryPyWSGI
run(app=web, host=host, port=port, server=CherryPyWSGI, quiet=True)
def run_fcgi(host="0.0.0.0", port="8000"):
from bottle import FlupFCGIServer
run(app=web, host=host, port=port, server=FlupFCGIServer, quiet=True)
if __name__ == "__main__":
run(app=web, port=8001)