-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
421f19b
commit 681c640
Showing
9 changed files
with
107 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ static | |
### Sensible ### | ||
/private.pem | ||
/public.pem | ||
|
||
/tmp |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,14 @@ | ||
__all__ = ["Converting"] | ||
|
||
import os | ||
|
||
import unoserver.client | ||
|
||
|
||
class Converting: | ||
def __init__(self, server="0.0.0.0", port=2003): | ||
self.client = unoserver.client.UnoClient(server, port) | ||
|
||
def conversion_is_allowed(self, filename: str): | ||
return filename[filename.rfind(".") + 1 :] in [ | ||
"doc", | ||
"docx", | ||
"png", | ||
"txt", | ||
"jpg", | ||
"md", | ||
"bmp", | ||
"xlsx", | ||
"xls", | ||
"odt", | ||
"ods", | ||
] | ||
|
||
def any2pdf(self, filename: str): | ||
if not self.conversion_is_allowed(filename): | ||
print("The file cannot be converted.") | ||
return | ||
filepath = (here := os.path.dirname(__file__))[: here.index("src")] + f"files_to_be_printed/{filename}" | ||
self.client.convert(inpath=filepath, outpath=f"{filepath[:filepath.rfind('.')]}.pdf") | ||
def any2pdf(self, inpath: str, outpath: str): | ||
self.client.convert(inpath=inpath, outpath=outpath) | ||
|
||
|
||
converting_repository: Converting = Converting() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import os | ||
import pathlib | ||
import tempfile | ||
|
||
from fastapi import APIRouter, UploadFile | ||
from fastapi.exceptions import HTTPException | ||
from starlette.responses import FileResponse | ||
|
||
from src.modules.converting.repository import converting_repository | ||
from src.modules.printing.repository import PrintingOptions, printing_repository | ||
|
||
router = APIRouter(prefix="/print", tags=["Print"]) | ||
|
||
|
||
@router.get("/job_status") | ||
async def job_status(job_id: int) -> str: | ||
""" | ||
Returns the status of a job | ||
""" | ||
|
||
return printing_repository.get_job_status(job_id) | ||
|
||
|
||
tempfiles = {} | ||
dir_for_temp = os.getcwd() + "/tmp" | ||
|
||
|
||
@router.post("/prepare", responses={400: {"description": "Unsupported format"}}) | ||
async def prepare_printing(file: UploadFile) -> str: | ||
""" | ||
Convert a file to pdf and return the path to the converted file | ||
""" | ||
|
||
ext = file.filename[file.filename.rfind(".") :] | ||
if ext == ".pdf": | ||
f = tempfile.NamedTemporaryFile(dir=dir_for_temp, suffix=".pdf") | ||
f.write(await file.read()) | ||
tempfiles[f.name] = f | ||
return f.name | ||
elif ext in [".doc", ".docx", ".png", ".txt", ".jpg", ".md", ".bmp", ".xlsx", ".xls", ".odt", ".ods"]: | ||
with ( | ||
tempfile.NamedTemporaryFile(dir=dir_for_temp, suffix=ext) as in_f, | ||
tempfile.NamedTemporaryFile(dir=dir_for_temp, suffix=".pdf", delete=False) as out_f, | ||
): | ||
in_f.write(await file.read()) | ||
in_f.flush() | ||
converting_repository.any2pdf(in_f.name, out_f.name) | ||
in_f.close() | ||
tempfiles[out_f.name] = out_f | ||
return out_f.name | ||
else: | ||
raise HTTPException(400, "Unsupported format") | ||
|
||
|
||
@router.post("/print", responses={404: {"description": "No such file"}}) | ||
def actual_print(filename: str, printer_name: str, printing_options: PrintingOptions = PrintingOptions()) -> int: | ||
""" | ||
Returns job identifier | ||
""" | ||
|
||
if filename in tempfiles: | ||
job_id = printing_repository.print_file(printer_name, filename, "job", printing_options) | ||
os.unlink(filename) | ||
del tempfiles[filename] | ||
return job_id | ||
else: | ||
raise HTTPException(404, "No such file") | ||
|
||
|
||
@router.get("/get_file", responses={404: {"description": "No such file"}}) | ||
def get_file(filename: str) -> FileResponse: | ||
if filename in tempfiles: | ||
short_name = pathlib.Path(filename).name | ||
return FileResponse(filename, headers={"Content-Disposition": f"attachment; filename={short_name}"}) | ||
else: | ||
raise HTTPException(404, "No such file") |
Empty file.