def server_static(filepath): ''' Serves static files from the application's own internal static path, e.g. for its CSS/JS ''' response.add_header('Cache-Control', 'max-age=7200') return static_file(filepath, root=APPLICATION_PATH + STATIC_PATH)
def media_preview(media_id): media = Media.load(media_id) try: root = media.path.rsplit(_sep, 1)[0] except: root = '' preview = static_file(media.filename, root=root) return preview
def blog_static(filepath): ''' Returns static routes for the currently staged blog. The blog ID is appended to the URL as a '_' parameter (e.g., ?_=1 for blog ID 1) The future of this function is currently being debated. ''' try: blog_id = int(request.query['_']) except KeyError: return system_site_index() # raise blog = Blog.load(blog_id) root_path = blog.path filesystem_filepath = urllib.parse.quote(filepath) # TODO: replace this with results from /preview path, # or another function that finds by abs path in db. if os.path.isfile(root_path + "/" + filesystem_filepath) is False: filepath += ("index.html") if os.path.isfile(root_path + "/" + filesystem_filepath) is False: abort( 404, 'File {} not found'.format(root_path + "/" + filesystem_filepath)) k = static_file(filesystem_filepath, root=root_path) if (k.headers['Content-Type'][:5] == "text/" or k.headers['Content-Type'] == "application/javascript"): k.add_header('Cache-Control', 'max-age=7200, public, must-revalidate') if (k._headers['Content-Type'][0][:5] == "text/" and not k.body == ""): x = k.body.read() k.body.close() y = x.decode('utf8') z = re.compile(r' href=["\']' + (blog.url) + '([^"\']*)["\']') z2 = re.compile(r' href=["\'](/[^"\']*)["\']') y = re.sub( z, r" href='" + BASE_URL_PROTOCOL + DEFAULT_LOCAL_ADDRESS + DEFAULT_LOCAL_PORT + "\\1?_={}'".format(blog_id), y) y = re.sub(z2, r" href='\1?_={}'".format(blog_id), y) y = y.encode('utf8') k.headers['Content-Length'] = len(y) k.body = y return (k)
def blog_static(filepath): ''' Returns static routes for the currently staged blog. The blog ID is appended to the URL as a '_' parameter (e.g., ?_=1 for blog ID 1) The future of this function is currently being debated. ''' try: blog_id = int(request.query['_']) except KeyError: return system_site_index() # raise blog = Blog.load(blog_id) root_path = blog.path filesystem_filepath = urllib.parse.quote(filepath) # TODO: replace this with results from /preview path, # or another function that finds by abs path in db. if os.path.isfile(root_path + "/" + filesystem_filepath) is False: filepath += ("index.html") if os.path.isfile(root_path + "/" + filesystem_filepath) is False: abort(404, 'File {} not found'.format(root_path + "/" + filesystem_filepath)) k = static_file(filesystem_filepath, root=root_path) if (k.headers['Content-Type'][:5] == "text/" or k.headers['Content-Type'] == "application/javascript"): k.add_header( 'Cache-Control', 'max-age=7200, public, must-revalidate') if (k._headers['Content-Type'][0][:5] == "text/" and not k.body == ""): x = k.body.read() k.body.close() y = x.decode('utf8') z = re.compile(r' href=["\']' + (blog.url) + '([^"\']*)["\']') z2 = re.compile(r' href=["\'](/[^"\']*)["\']') y = re.sub(z, r" href='" + BASE_URL_PROTOCOL + DEFAULT_LOCAL_ADDRESS + DEFAULT_LOCAL_PORT + "\\1?_={}'".format(blog_id), y) y = re.sub(z2, r" href='\1?_={}'".format(blog_id), y) y = y.encode('utf8') k.headers['Content-Length'] = len(y) k.body = y return (k)
def system_theme_download(theme_id): user = auth.is_logged_in(request) permission = auth.is_sys_admin(user) from core.models import Theme theme = Theme.load(theme_id) import shutil, tempfile, os from core.libs.bottle import static_file with tempfile.TemporaryDirectory() as temp_zipdir: zip_filename = 'theme_{}'.format(theme.id) shutil.make_archive(os.path.join(temp_zipdir, zip_filename), 'zip', theme.path) return static_file('{}.zip'.format(zip_filename), root=temp_zipdir, download=True)
def img_server(filename): return static_file(filename, root="views/img")
def js_server(filename): return static_file(filename, root="views/js")
def css_server(filename): return static_file(filename, root="views/css")