Add ability to get file list and download files from project output

This commit is contained in:
Brett Williams
2022-10-26 18:27:13 -07:00
parent 2da06ab166
commit eb9e719f47
8 changed files with 71 additions and 28 deletions

View File

@@ -3,15 +3,17 @@
import json
import logging
import os
import pathlib
import shutil
import socket
import threading
import time
from datetime import datetime
from zipfile import ZipFile
import requests
import yaml
from flask import Flask, request, render_template
from flask import Flask, request, render_template, send_file, after_this_request
from werkzeug.utils import secure_filename
from lib.render_job import RenderJob
@@ -34,17 +36,53 @@ def filtered_jobs_json(status_val):
if jobs:
return jobs
else:
return {'error', f'Cannot find jobs with status {status_val}'}, 400
return f'Cannot find jobs with status {status_val}', 400
@app.get('/job_status/<job_id>')
def get_job_status(job_id):
found_job = RenderQueue.job_with_id(job_id)
if found_job:
logger.info("Founbd jobs")
return found_job.json()
else:
return {'error': f'Cannot find job with ID {job_id}'}, 400
return f'Cannot find job with ID {job_id}', 400
@app.get('/file_list/<job_id>')
def get_file_list(job_id):
found_job = RenderQueue.job_with_id(job_id)
if found_job:
job_dir = os.path.dirname(found_job.render.output_path)
return os.listdir(job_dir)
else:
return f'Cannot find job with ID {job_id}', 400
@app.route('/download_all/<job_id>')
def download_all(job_id):
zip_filename = None
@after_this_request
def clear_zip(response):
if zip_filename and os.path.exists(zip_filename):
os.remove(zip_filename)
return response
found_job = RenderQueue.job_with_id(job_id)
if found_job:
output_dir = os.path.dirname(found_job.render.output_path)
if os.path.exists(output_dir):
zip_filename = pathlib.Path(found_job.render.input_path).stem + '.zip'
with ZipFile(zip_filename, 'w') as zipObj:
for f in os.listdir(output_dir):
zipObj.write(filename=os.path.join(output_dir, f),
arcname=os.path.basename(f))
return send_file(zip_filename, mimetype="zip", as_attachment=True, )
else:
return f'Cannot find project files for job {job_id}', 500
else:
return f'Cannot find job with ID {job_id}', 400
@app.post('/register_client')
@@ -103,7 +141,6 @@ def snapshot():
@app.post('/add_job')
def add_job():
def remove_job_dir():
if job_dir and os.path.exists(job_dir):
logger.debug(f"Removing job dir: {job_dir}")
@@ -146,14 +183,17 @@ def add_job():
local_path = os.path.join(job_dir, secure_filename(uploaded_file.filename))
uploaded_file.save(local_path)
input_path = local_path
output_path = os.path.join(job_dir, os.path.basename(output_path))
output_dir = os.path.join(job_dir, 'output')
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, os.path.basename(output_path))
# local renders
if client == RenderQueue.host_name:
logger.info(f"Creating job locally - {input_path}")
try:
render_job = RenderWorkerFactory.create_worker(renderer, input_path, output_path, args)
except ValueError as e:
render_job.log_path = os.path.join(os.path.dirname(input_path), os.path.basename(input_path) + '.log')
except Exception as e:
err_msg = f"Error creating job: {str(e)}"
logger.exception(err_msg)
remove_job_dir()
@@ -268,7 +308,6 @@ def post_job_to_server(input_path, job_json, client, server_port=8080):
def start_server(background_thread=False):
def eval_loop(delay_sec=1):
while True:
RenderQueue.evaluate_queue()
@@ -311,4 +350,4 @@ def start_server(background_thread=False):
if __name__ == '__main__':
start_server()
start_server()