def get_user_data(filepath):
user = root.authorized()
# filepath = request.query.filepath
# get the owner from the filepath
# e.g. "user_data/wes/mendel/y23022/file.dat"
path_list = filepath.split("/")
owner = path_list[0]
cid = path_list[2]
shared = jobs(cid=cid).shared
# print filepath, path_list, shared
# only allow admin to see other user's cases that have not been shared
if owner != user and shared != "True" and user != "admin":
return template('error', err="access forbidden")
return static_file(filepath, root=user_dir)
python类static_file()的实例源码
def zip_case():
"""zip case on machine to prepare for download"""
user = root.authorized()
import zipfile
app = request.query.app
cid = request.query.cid
base_dir = os.path.join(user_dir, user, app)
path = os.path.join(base_dir, cid+".zip")
zf = zipfile.ZipFile(path, mode='w', compression=zipfile.ZIP_DEFLATED)
sim_dir = os.path.join(base_dir, cid)
for fn in os.listdir(sim_dir):
zf.write(os.path.join(sim_dir, fn))
zf.close()
return static_file(path, root="./")
# status = "case compressed"
# redirect(request.headers.get('Referer')+"&status="+status)
def export_metadata():
mdc = MetaController()
hashes = request.forms.dict.get("file_hash[]")
dump_to_save = ""
random_id = id_generator()
tmp_path = "/tmp/meta_export"
tmp_folder = os.path.join(tmp_path, random_id)
call_with_output(["mkdir", "-p", tmp_folder])
for hash in hashes:
hash = clean_hash(hash.replace('\r', ''))
res = mdc.read(hash)
dump = dumps(res, indent=4)
file_name = os.path.join(tmp_folder, str(hash) + '.txt')
fd = open(file_name, "w")
fd.write(dump)
fd.close()
zip_path = os.path.join(tmp_path, random_id + '.zip')
call_with_output(["zip", "-jr", zip_path, tmp_folder])
resp = static_file(str(random_id) + '.zip', root=tmp_path, download=True)
resp.set_cookie('fileDownload', 'true')
shutil.rmtree(tmp_folder)
os.remove(zip_path)
return resp
def main():
if len(sys.argv) > 1:
print "Serves the demo. Needs bottle.py to run. Will serve via bjoern"
print "if installed, otherwise via wsgi_ref. Reads its configuration "
print "from config.ini."
return
# load configuration
port = int(CONFIG.get('port', 9090))
staticdir = os.path.join(os.path.dirname(__file__), 'static')
# start web server
print "Starting web server on localhost:%d..." % port
app.route('/static/:path#.+#', callback=lambda path:
bottle.static_file(path, root=staticdir))
try:
import bjoern
except ImportError:
bjoern = None
bottle.run(app, host='localhost', port=port,
server='bjoern' if bjoern else 'wsgiref')
def server_static(filepath):
"""Serves static assets from the directory `equitas/static`.
This supports returning static files from subdirectories within that
directory. For example, it allows you to return css stylesheets or even
javascript files from those directories. For example, in your HTML, you can
have the following:
<script src="/static/scripts/something/something_else.js"></script>
And this would return the script within
`equitas/static/scripts/something/something_else`.
"""
this_directory = os.path.dirname(os.path.abspath(__file__))
static_directory = os.path.join(this_directory, 'anxiety', 'static')
return static_file(filepath, root=static_directory)
################################################################################
# API for JSON requests
################################################################################
def serve_static(filename):
return static_file(filename, root=path.join(BASE_DIR, 'static'))
def fetch_static(filename):
response.set_header('Cache-Control', 'max-age=600')
return static_file(filename, root='static')
def server_static(filepath):
return static_file(filepath, root='/Users/pipskweak/Desktop/web/')
def server_static(filepath):
return static_file(filepath, root='./static/')
def custom_html(self, filepath):
"""
Serve custom HTML page
"""
try:
response.headers.append("Access-Control-Allow-Origin", "*")
response.headers.append("Accept-Encoding", "identity")
response.headers.append("Content-Type", "text/html")
return static_file(filepath, root=self.config.html)
except Exception as e:
raise PJFBaseException(e.message if hasattr(e, "message") else str(e))
def static(filename):
"""static file path"""
return static_file(filename, root=STATIC)
def get_favicon():
return static_file('favicon.ico', root=os.path.join(static_path, 'img'))
def get_css(file):
return static_file(file, root=os.path.join(static_path, 'css'))
def get_fonts(file):
return static_file(file, root=os.path.join(static_path, 'fonts'))
def get_img(file):
return static_file(file, root=os.path.join(static_path, 'img'))
def get_js(file):
return static_file(file, root=os.path.join(static_path, 'js'))
def server_static(filename):
return static_file(filename, root='./static')
def server_static_css(filename):
return static_file(filename, root='./static/css')
def server_static_js(filename):
return static_file(filename, root='./static/js')
def server_static_img(filename):
return static_file(filename, root='./static/img')
def server_static_img(filename):
return static_file(filename, root='./static/layer')
def get_static_file(path):
return static_file(path, root=STATIC_FILES)
def get_source():
"""Download the source of this application."""
# from http://stackoverflow.com/questions/458436/adding-folders-to-a-zip-file-using-python#6511788
directory = tempfile.mkdtemp()
temp_path = os.path.join(directory, APPLICATION)
zip_path = shutil.make_archive(temp_path, "zip", HERE)
return static_file(APPLICATION + ".zip", root=directory)
def server_static(filepath):
imgpath = static_file(filepath, root='./%s' % (IMGFOLDER, ))
return imgpath
def serve_static(filepath):
return static_file(filepath, root=RESOURCES_PATH)
# make this a plugin
def get_file(filename):
media_dir = app._config['henet']['media_dir']
fullpath = os.path.join(media_dir, filename)
mimetype = guess_type(fullpath)[0]
return static_file(filename, root=media_dir,
mimetype=mimetype)
def get_media_thumbnail(size, filename):
filename = filename.decode('utf8')
ext = os.path.splitext(filename)[-1].lower()
if ext == '.pdf':
redirect('/resources/images/pdf-200x200.png')
return
if ext not in ('.jpg', '.png', '.jpeg', '.bmp'):
redirect('/resources/images/blank.png')
return
media_dir = app._config['henet']['media_dir']
thumbnails_dir = app._config['henet']['thumbnails_dir']
thumbname = size + '-' + filename
thumbnail_file = os.path.join(thumbnails_dir, thumbname)
if not os.path.exists(thumbnail_file):
image_file = os.path.join(media_dir, filename)
size = [int(i) for i in size.split('x')]
image = Image.open(image_file)
image.thumbnail(size)
image.save(thumbnail_file, 'JPEG')
mimetype = guess_type(thumbnail_file)[0]
return static_file(thumbname, root=thumbnails_dir,
mimetype=mimetype)
def static(file_path):
"""
???????????????
/static/* ???????????????????
:param file_path:
:return:
"""
return static_file(file_path, root="./static")
def user_data(filepath):
return static_file(filepath, root='user_data')
def user_data(filepath):
return static_file(filepath, root='user_data')
# By default, the server will allow negotiations with extremely old protocols
# that are susceptible to attacks, so we only allow TLSv1.2