6 changed files with 87 additions and 19 deletions
@ -1,12 +1,64 @@
|
||||
import os |
||||
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.full_path = '' |
||||
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'] |
||||
filenames = os.listdir(video_path) |
||||
return render_template('file_listing/index.html', filenames=filenames) |
||||
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(): |
||||
pe = path_entry() |
||||
pe.base_name = entry.name |
||||
pe.path = entry.path |
||||
pe.is_dir = entry.is_dir() |
||||
pe.depth = depth |
||||
|
||||
# TODO: add relative path for http server root links as 'full_path' |
||||
|
||||
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 |
||||
|
||||
Loading…
Reference in new issue