import os import re import yaml from flask import Blueprint, current_app, render_template VIDEO_EXTENSIONS = ['.avi', '.m4v', '.mkv', '.mp4', '.mpg'] MAX_RECURSE_DEPTH = 5 bp = Blueprint('file_listing', __name__) class path_entry: def __init__(self): self.base_name = '' self.rel_path = '' # NOTE: fs path relative to config['MOVIE_DIR'] self.file_extension = '' self.is_dir = False self.depth = 0 self.contents = [] @bp.route('/file_listing') def index(): # construct tree of path_entry objects video_path = current_app.config['MOVIE_DIR'] paths = [] for entry in os.scandir(video_path): p = getPathInfo(entry) if p: paths.append(p) return render_template( 'file_listing/index.html', paths=paths, debug_str=yaml.dump(paths) ) def getPathInfo(entry, depth=0): if not entry.name.startswith('.') and not entry.is_symlink(): video_path = current_app.config['MOVIE_DIR'] pe = path_entry() pe.base_name = entry.name pe.rel_path = re.sub(video_path, '', entry.path) pe.is_dir = entry.is_dir() pe.depth = depth if entry.is_file(): ext = os.path.splitext(entry)[1] if ext in VIDEO_EXTENSIONS: pe.file_extension = ext else: return None elif entry.is_dir() and depth < MAX_RECURSE_DEPTH: for e in os.scandir(entry): p = getPathInfo(e, depth + 1) if p: pe.contents.append(p) return pe return None