diff --git a/root/app/calibre-web/cps/cwa_functions.py b/root/app/calibre-web/cps/cwa_functions.py index 75402e0..ac239c3 100644 --- a/root/app/calibre-web/cps/cwa_functions.py +++ b/root/app/calibre-web/cps/cwa_functions.py @@ -8,11 +8,14 @@ import subprocess import sqlite3 -import os.path +from pathlib import Path from time import sleep import json from threading import Thread +import queue +import os +import tempfile import sys sys.path.insert(1, '/app/calibre-web-automated/scripts/') @@ -236,40 +239,46 @@ def cwa_flash_status(): return redirect(url_for('admin.admin')) -def convert_library_start(): - subprocess.Popen(['python3', '/app/calibre-web-automated/scripts/convert_library.py']) +def convert_library_start(queue): + cl_process = subprocess.Popen(['python3', '/app/calibre-web-automated/scripts/convert_library.py']) + queue.put(cl_process) + +def kill_convert_library(queue): + trigger_file = Path("/config/.kill_convert_library_trigger") + while True: + sleep(0.1) + if trigger_file.exists(): + cl_process = queue.get() + cl_process.terminate() + os.remove(tempfile.gettempdir() + '/convert_library.lock') + os.remove(trigger_file) + with open("/config/convert-library.log", 'a') as f: + f.write("\nCONVERT LIBRARY PROCESS TERMINATED BY USER") + break @convert_library.route('/cwa-library-convert', methods=['GET']) def start_conversion(): open('/config/convert-library.log', 'w').close() # Wipe conversion log from previous runs - t1 = Thread(target=convert_library_start) - t1.start() + # Queue to share the subprocess reference + process_queue = queue.Queue() + # Create and start the subprocess thread + cl_thread = Thread(target=convert_library_start, args=(process_queue,)) + cl_thread.start() + # Create and start the kill thread + cl_kill_thread = Thread(target=kill_convert_library, args=(process_queue,)) + cl_kill_thread.start() return render_title_template('cwa_convert_library.html', title=_("Calibre-Web Automated - Convert Library"), page="cwa-library-convert", target_format=CWA_DB().cwa_settings['auto_convert_target_format'].upper()) -@convert_library.route('/convert-library-status', methods=['GET']) -def getStatus(): - with open("/config/convert-library.log", 'r') as f: - status = f.read() - statusList = {'status':status} - return json.dumps(statusList) - - -# def flask_logger(): -# subprocess.Popen(['python3', '/app/calibre-web-automated/scripts/convert_library.py']) -# if os.path.isfile("/config/convert-library.log") == False: -# with open('/config/convert-library.log', 'w') as create_new_log: -# pass -# with open("/config/convert-library.log", 'r') as log_info: -# while True: -# data = log_info.read() -# yield data.encode() -# sleep(1) -# if "FIN" in data: -# break - -# @convert_library.route("/cwa-library-convert", methods=["GET", "POST"]) -# @login_required_if_no_ano -# @admin_required -# def cwa_library_convert(): -# return Response(flask_logger(), mimetype="text/plain", content_type="text/event-stream") \ No newline at end of file +@convert_library.route('/convert-library-status', methods=['GET', 'POST']) +def get_status(): + if request.method == "POST" and request.form['cancel_button'] == "Cancel": + open("/config/.kill_convert_library_trigger", 'w').close() + return render_title_template('cwa_convert_library.html', title=_("Calibre-Web Automated - Convert Library"), page="cwa-library-convert", + target_format=CWA_DB().cwa_settings['auto_convert_target_format'].upper()) + + elif request.method == "GET": + with open("/config/convert-library.log", 'r') as f: + status = f.read() + statusList = {'status':status} + return json.dumps(statusList) \ No newline at end of file diff --git a/root/app/calibre-web/cps/templates/admin.html b/root/app/calibre-web/cps/templates/admin.html index 353c5f6..5044ae0 100644 --- a/root/app/calibre-web/cps/templates/admin.html +++ b/root/app/calibre-web/cps/templates/admin.html @@ -209,7 +209,7 @@

{{_('CWA Admin Functions⚡')}}

{{_('Convert Library to Target Format')}} {{_('Show CWA History')}} -
+
{{_('CWA GitHub')}} {{_('CWA Discord Server')}}
diff --git a/root/app/calibre-web/cps/templates/cwa_convert_library.html b/root/app/calibre-web/cps/templates/cwa_convert_library.html index c9416a7..583a9b8 100644 --- a/root/app/calibre-web/cps/templates/cwa_convert_library.html +++ b/root/app/calibre-web/cps/templates/cwa_convert_library.html @@ -5,11 +5,21 @@

{{title}}

CWA Library Convertor - Current Target Format - {{target_format}}


-

- The Library Conversion Process automatically starts upon loading this page and therefore has already started, - you can follow it's progress below. Leaving this page will not interrupt the process but you will be unable to - return to check on it's process here in the Web UI after leaving, you would then need to check it's progress - in the container's logs.

+
+

+ The Library Conversion Process automatically starts upon loading this page and therefore has already started, + you can follow it's progress below. Leaving this page will not interrupt the process but you will be unable to + return to check on it's process here in the Web UI after leaving, you would then need to check it's progress + in the container's logs.

+
+ Cancel +
+
CWA Library Convertor - Current Target Format - { let get; try { - const res = await fetch("/convert-library-status"); + const res = await fetch("{{ url_for('convert_library.get_status')}}"); get = await res.json(); } catch (e) { console.error("Error: ", e); diff --git a/scripts/convert_library.py b/scripts/convert_library.py index 5748bf3..435e182 100644 --- a/scripts/convert_library.py +++ b/scripts/convert_library.py @@ -34,7 +34,7 @@ def print_and_log(string) -> None: lock.close() except FileExistsError: print_and_log("[convert-library]: CANCELLING... convert-library was initiated but is already running") - print_and_log("FIN") + print_and_log("\nDone!") sys.exit(2) # Defining function to delete the lock on script exit @@ -256,7 +256,7 @@ def convert_to_kepub(self, filepath:str ,import_format:str) -> tuple[bool, str]: ) as process: for line in process.stdout: # Read from the combined stdout (which includes stderr) print_and_log(line) - + if self.cwa_settings['auto_backup_conversions']: shutil.copy2(filepath, f"/config/processed_books/converted")