caff-previewer-wrapper/caff_previewer_wrapper/converter.py

50 lines
2.1 KiB
Python
Raw Permalink Normal View History

2020-11-27 16:01:23 +01:00
import os.path
import subprocess # nosec: That's the whole point of this application
2020-11-27 05:44:32 +01:00
from flask import current_app
import werkzeug.exceptions
2020-11-27 18:07:47 +01:00
def run_abstract_converter(converter: str, source: str, destination: str, extra_args: list = None) -> int:
2020-11-27 05:44:32 +01:00
"""
Just runs a binary and gives it two arguments
:param converter: the converter binary to run
:param source: source file
:param destination: destination file
:returns: exitcode of the converter
"""
2020-11-27 16:01:23 +01:00
if not (os.path.isfile(source) and os.path.isfile(converter)):
raise FileNotFoundError("Source or converter binary does not exists")
2020-11-27 18:07:47 +01:00
if not extra_args:
extra_args = []
completed_process = subprocess.run([converter, *extra_args, source, destination], # nosec: Concerning arguments checked above
2020-11-27 05:44:32 +01:00
timeout=current_app.config['CONVERSION_TIMEOUT'], env={})
return completed_process.returncode
2020-11-27 16:01:23 +01:00
2020-11-27 05:44:32 +01:00
def convert_caff_to_tga(source: str, destination: str):
"""
This function uses caff_previewer to convert a CAFF file into a TGA file
:param source: path of the source TGA file (must exists)
:param destination: path of the destination TGA file (will be created)
"""
INTERNAL_ERROR_CODES = [0x01, 0x03, 0x04, 0x05, 0x32, 0x51]
ret = run_abstract_converter(current_app.config['CAFF_PREVIEWER_BINARY'], source, destination)
if ret in INTERNAL_ERROR_CODES:
raise RuntimeError(f"Caff Previewer returned an unexpected error code: {ret}")
elif ret != 0:
raise werkzeug.exceptions.BadRequest("CAFF format violation")
def convert_tga_to_png(source: str, destination: str):
"""
This function uses ImageMagick to convert a TGA file into a PNG file
:param source: path of the source TGA file (must exists)
:param destination: path of the destination TGA file (will be created)
"""
2020-11-27 18:07:47 +01:00
ret = run_abstract_converter(current_app.config['IMAGEMAGICK_CONVERT_BINARY'], source, destination, ['-auto-orient'])
2020-11-27 05:44:32 +01:00
if ret != 0:
raise RuntimeError(f"Image magick convert returned an unexpected error code: {ret}")