mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-01-13 11:40:35 +00:00
114 lines
3.1 KiB
Python
114 lines
3.1 KiB
Python
import flask
|
|
import os
|
|
|
|
|
|
UPLOAD_DIRECTORY = os.path.abspath("/root/vre_files/jobs")
|
|
|
|
|
|
job_counter = 0
|
|
jobs = []
|
|
|
|
|
|
def create_job(file, language, name, service, user):
|
|
global job_counter
|
|
|
|
if (service == "nlp" and file.content_type != "text/plain"):
|
|
flask.abort(415)
|
|
if (service == "ocr" and file.content_type != "application/pdf"):
|
|
flask.abort(415)
|
|
|
|
job = {}
|
|
job["file"] = file.filename
|
|
job["id"] = "job-" + str(job_counter + 1)
|
|
job["language"] = language
|
|
job["name"] = name
|
|
job["report"] = ""
|
|
job["service"] = service
|
|
job["status"] = "queued"
|
|
job["user"] = user
|
|
|
|
job_directory = os.path.join(UPLOAD_DIRECTORY, job["id"])
|
|
if not os.path.exists(job_directory):
|
|
os.makedirs(job_directory)
|
|
file.save(os.path.join(job_directory, file.filename))
|
|
|
|
jobs.append(job)
|
|
job_counter += 1
|
|
|
|
return flask.make_response(flask.jsonify(job), 201)
|
|
|
|
|
|
def delete_job(id):
|
|
job_directory = os.path.join(UPLOAD_DIRECTORY, id)
|
|
|
|
for job in jobs:
|
|
if job["id"] == id:
|
|
if job["status"] != "queued":
|
|
flask.abort(423)
|
|
os.remove(os.path.join(job_directory, job["file"]))
|
|
os.rmdir(job_directory)
|
|
job["file"] = None
|
|
job["language"] = None
|
|
job["name"] = None
|
|
job["report"] = None
|
|
job["service"] = None
|
|
job["status"] = "deleted"
|
|
return flask.make_response("", 204)
|
|
|
|
flask.abort(404)
|
|
|
|
|
|
def get_job(id):
|
|
for job in jobs:
|
|
if job["id"] == id:
|
|
return job
|
|
|
|
flask.abort(404)
|
|
|
|
|
|
def get_jobs(name = None, service = None, status = None, user = None):
|
|
filtered_jobs = []
|
|
|
|
for job in jobs:
|
|
if name and job["name"] != name:
|
|
continue
|
|
if service and job["service"] != service:
|
|
continue
|
|
if status and job["status"] != status:
|
|
continue
|
|
if user and job["user"] != user:
|
|
continue
|
|
filtered_jobs.append(job)
|
|
|
|
return filtered_jobs
|
|
|
|
|
|
def update_job(id, file = None, language = None, name = None, report = None, status = None):
|
|
job_directory = os.path.join(UPLOAD_DIRECTORY, id)
|
|
|
|
for job in jobs:
|
|
if job["id"] == id:
|
|
if status:
|
|
job["status"] = status
|
|
if report:
|
|
job["report"] = report
|
|
return job
|
|
if job["status"] != "queued":
|
|
flask.abort(423)
|
|
if file:
|
|
if (job["service"] == "nlp" and file.content_type != "text/plain"):
|
|
flask.abort(415)
|
|
if (job["service"] == "ocr" and file.content_type != "application/pdf"):
|
|
flask.abort(415)
|
|
os.remove(os.path.join(job_directory, job["file"]))
|
|
file.save(os.path.join(job_directory, file.filename))
|
|
job["file"] = file.filename
|
|
if language:
|
|
job["language"] = language
|
|
if report:
|
|
job["report"] = report
|
|
if name:
|
|
job["name"] = name
|
|
return job
|
|
|
|
flask.abort(404) |