Пример #1
0
 def dir_listing(req_path):
     BASE_DIR = static_dir
     abs_path = os.path.join(BASE_DIR, req_path)
     abs_path = os.path.abspath(abs_path)
     if not os.path.exists(abs_path):
         return abort(404)
     if os.path.isfile(abs_path):
         return send_file(abs_path)
     if os.path.isdir(abs_path):
         fns = os.listdir(abs_path)
         fps = [join_path(url_prefix, req_path, f) for f in fns]
         return template.render(files=zip(fps, fns))
Пример #2
0
    def __init__(self,
                 import_name=None,
                 name='static',
                 url_prefix='/',
                 static_dir='./',
                 template=None,
                 debug=False,
                 **kwargs):
        super().__init__(name=name,
                         import_name=import_name,
                         url_prefix=url_prefix,
                         **kwargs)

        def tprint(*args, **kwargs):
            if debug:
                print('**info:', *args, **kwargs)

        import os
        template = resources.default_templates[
            'files'] if not template else template

        @self.route('/', defaults={'req_path': ''})
        @self.route(utils.join_path('/', '<path:req_path>'))
        def dir_listing(req_path):
            tprint('req_path:', req_path)
            BASE_DIR = static_dir
            # print(static_dir)
            abs_path = os.path.join(BASE_DIR, req_path)
            abs_path = os.path.abspath(abs_path)
            # print(abs_path)
            if not os.path.exists(abs_path):
                return abort(404)
            if os.path.isfile(abs_path):
                # print(abs_path)
                return send_file(abs_path)
            if os.path.isdir(abs_path):
                fns = os.listdir(abs_path)
                fps = [
                    utils.join_path(self.url_prefix, url_prefix, req_path, f)
                    for f in fns
                ]
                return utils.render(open(template, 'r',
                                         encoding='utf-8').read(),
                                    files=zip(fps, fns))
Пример #3
0
    def _add_static(self, url_prefix='/files', static_dir='./', template=None):
        template = get_template_by_name("files") if not template else template
        url_prefix = url_prefix.rstrip('/')

        @self.route(url_prefix + '/', defaults={'req_path': ''})
        @self.route(url_prefix + join_path('/', '<path:req_path>'))
        @rename_func("dir_listing_" + uuid.uuid4().hex)
        def dir_listing(req_path):
            BASE_DIR = static_dir
            abs_path = os.path.join(BASE_DIR, req_path)
            abs_path = os.path.abspath(abs_path)
            if not os.path.exists(abs_path):
                return abort(404)
            if os.path.isfile(abs_path):
                return send_file(abs_path)
            if os.path.isdir(abs_path):
                fns = os.listdir(abs_path)
                fps = [join_path(url_prefix, req_path, f) for f in fns]
                return template.render(files=zip(fps, fns))
Пример #4
0
 def dir_listing(req_path):
     tprint('req_path:', req_path)
     BASE_DIR = static_dir
     # print(static_dir)
     abs_path = os.path.join(BASE_DIR, req_path)
     abs_path = os.path.abspath(abs_path)
     # print(abs_path)
     if not os.path.exists(abs_path):
         return abort(404)
     if os.path.isfile(abs_path):
         # print(abs_path)
         return send_file(abs_path)
     if os.path.isdir(abs_path):
         fns = os.listdir(abs_path)
         fps = [
             utils.join_path(self.url_prefix, url_prefix, req_path, f)
             for f in fns
         ]
         return utils.render(open(template, 'r',
                                  encoding='utf-8').read(),
                             files=zip(fps, fns))