11import asyncio
2- import os
32import tempfile
43from pathlib import Path
54from typing import Any
1817from src .modules .printing .repository import printing_repository
1918
2019router = APIRouter (prefix = "/print" , tags = ["Print" ])
21- tempfiles : dict [tuple [str , str ], Any ] = {}
2220
2321
2422@router .get ("/job_status" )
@@ -33,11 +31,13 @@ async def job_status(job_id: int, _innohassle_user_id: USER_AUTH) -> JobAttribut
3331
3432@router .get ("/get_file" , responses = {404 : {"description" : "No such file" }})
3533def get_file (filename : str , innohassle_user_id : USER_AUTH ) -> FileResponse :
36- if (innohassle_user_id , filename ) in tempfiles :
37- full_path = tempfiles [(innohassle_user_id , filename )].name
38- return FileResponse (full_path , headers = {"Content-Disposition" : f"attachment; filename={ filename } " })
34+ if (innohassle_user_id , filename ) in printing_repository .tempfiles :
35+ return FileResponse (
36+ printing_repository .get_tempfile_path (innohassle_user_id , filename ),
37+ headers = {"Content-Disposition" : f"attachment; filename={ filename } " },
38+ )
3939 else :
40- raise HTTPException (404 , "No such file" )
40+ raise HTTPException (404 , "No such file. It was removed from our servers due to expiration " )
4141
4242
4343@router .get ("/get_printers" )
@@ -69,7 +69,7 @@ async def get_printer_status(printer_cups_name: str, _innohassle_user_id: USER_A
6969@router .post ("/prepare" , responses = {400 : {"description" : "Unsupported format" }})
7070async def prepare_printing (file : UploadFile , innohassle_user_id : USER_AUTH ) -> PreparePrintingResponse :
7171 """
72- Convert a file to pdf and return the path to the converted file
72+ Convert a file to PDF and return the path to the converted file
7373 """
7474
7575 if not file .size :
@@ -80,7 +80,7 @@ async def prepare_printing(file: UploadFile, innohassle_user_id: USER_AUTH) -> P
8080 if ext == ".pdf" :
8181 f = tempfile .NamedTemporaryFile (dir = settings .api .temp_dir , suffix = ".pdf" )
8282 f .write (await file .read ())
83- tempfiles [ (innohassle_user_id , Path ( f . name ). name )] = f
83+ printing_repository . store_tempfile (innohassle_user_id , f )
8484 return PreparePrintingResponse (filename = Path (f .name ).name , pages = len (PyPDF2 .PdfReader (f ).pages ))
8585 elif ext in [".doc" , ".docx" , ".png" , ".txt" , ".jpg" , ".md" , ".bmp" , ".xlsx" , ".xls" , ".odt" , ".ods" ]:
8686 with (
@@ -92,7 +92,7 @@ async def prepare_printing(file: UploadFile, innohassle_user_id: USER_AUTH) -> P
9292 # Run conversion in a background thread
9393 await asyncio .to_thread (converting_repository .any2pdf , in_f .name , out_f .name )
9494 in_f .close ()
95- tempfiles [ (innohassle_user_id , Path ( out_f . name ). name )] = out_f
95+ printing_repository . store_tempfile (innohassle_user_id , out_f )
9696 return PreparePrintingResponse (filename = Path (out_f .name ).name , pages = len (PyPDF2 .PdfReader (out_f ).pages ))
9797 else :
9898 raise HTTPException (400 , f"no support of the { ext } format" )
@@ -110,18 +110,15 @@ async def actual_print(
110110 """
111111 logger .info (f"Printing options: { printing_options } " )
112112
113- if (innohassle_user_id , filename ) in tempfiles :
113+ if (innohassle_user_id , filename ) in printing_repository . tempfiles :
114114 printer = printing_repository .get_printer (printer_cups_name )
115115 if not printer :
116116 raise HTTPException (400 , "No such printer" )
117- full_path = tempfiles [(innohassle_user_id , filename )].name
118- job_id = printing_repository .print_file (printer , full_path , "job" , printing_options )
117+ job_id = printing_repository .print_file (innohassle_user_id , filename , printer , printing_options )
119118 logger .info (f"Job { job_id } has started" )
120- os .unlink (full_path )
121- del tempfiles [(innohassle_user_id , filename )]
122119 return job_id
123120 else :
124- raise HTTPException (404 , "No such file" )
121+ raise HTTPException (404 , "No such file. It was removed from our servers due to expiration " )
125122
126123
127124@router .post ("/cancel" , responses = {404 : {"description" : "No such file" }, 400 : {"description" : "No such printer" }})
@@ -132,12 +129,8 @@ async def cancel_printing(job_id: int, _innohassle_user_id: USER_AUTH) -> None:
132129
133130@router .post ("/cancel_preparation" , responses = {404 : {"description" : "No such file" }})
134131async def cancel_preparation (filename : str , innohassle_user_id : USER_AUTH ) -> None :
135- if (innohassle_user_id , filename ) in tempfiles :
136- full_path = tempfiles [(innohassle_user_id , filename )].name
137- os .unlink (full_path )
138- del tempfiles [(innohassle_user_id , filename )]
139- else :
140- raise HTTPException (404 , "No such file" )
132+ if not printing_repository .remove_tempfile (innohassle_user_id , filename ):
133+ raise HTTPException (404 , "No such file. It was removed from our servers due to expiration" )
141134
142135
143136@router .post ("/debug/getPrinterAttributes" )
0 commit comments