Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nimarion committed Apr 25, 2024
1 parent 9ca6459 commit b3e0825
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,8 @@ pyrightconfig.json
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode

# End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
header
footer
font
29 changes: 18 additions & 11 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
from fastapi import FastAPI, Response
from fastapi import FastAPI, Response, HTTPException
from typing import Optional
from os import walk
from fastapi.staticfiles import StaticFiles
from bib import generate_image

header_folder = "header"
footer_folder = "footer"
font_folder = "font"

app = FastAPI(title="DLV", docs_url="/swagger",
openapi_url="/swagger-json", redoc_url=None)

app.mount("/header", StaticFiles(directory="header"), name="header")
app.mount("/footer", StaticFiles(directory="footer"), name="footer")
app.mount("/" + header_folder, StaticFiles(directory="header"), name="header")
app.mount("/" + footer_folder, StaticFiles(directory="footer"), name="footer")


def headers():
header = []
for (_, _, filenames) in walk("header"):
for (_, _, filenames) in walk(header_folder):
filenames = [f for f in filenames if f.endswith(
'.png') or f.endswith('.jpg')]
header.extend(filenames)
Expand All @@ -23,7 +27,7 @@ def headers():

def footers():
footer = []
for (_, _, filenames) in walk("footer"):
for (_, _, filenames) in walk(footer_folder):
filenames = [f for f in filenames if f.endswith(
'.png') or f.endswith('.jpg')]
footer.extend(filenames)
Expand All @@ -33,7 +37,7 @@ def footers():

def fonts():
fonts = []
for (_, _, filenames) in walk("fonts"):
for (_, _, filenames) in walk(font_folder):
filenames = [f for f in filenames if f.endswith(
'.ttf')]
fonts.extend(filenames)
Expand All @@ -53,14 +57,17 @@ def generate_bib(
font: str,
header_offset: Optional[int] = 60,
):
print("hi")
if header not in headers():
return {"error": "Header not found"}
raise HTTPException(status_code=404, detail="Header not found")
if footer not in footers():
return {"error": "Footer not found"}
raise HTTPException(status_code=404, detail="Footer not found")
print(font)
if font not in fonts():
return {"error": "Font not found"}
image = generate_image(text, header, footer,
font, header_offset)
raise HTTPException(status_code=404, detail="Font not found")
print("hi")
image = generate_image(text, header_folder + "/" + header, footer_folder + "/" + footer,
font_folder + "/" + font, header_offset)
return Response(content=image, media_type="image/png", headers={"Content-Disposition": "filename=" + text + ".png"})


Expand Down

0 comments on commit b3e0825

Please sign in to comment.