ajout images

This commit is contained in:
François Pelletier 2023-07-05 01:12:44 -04:00
parent c2c9159631
commit 027ad1c62f
7 changed files with 64 additions and 6 deletions

View file

@ -29,15 +29,14 @@ COPY requirements.txt .
# Install Python dependencies
RUN pip install -r requirements.txt
# Copy the application code
COPY . .
# Policy for ImageMagick
COPY conf/policy.xml /etc/ImageMagick-6/policy.xml
# Expose the application port
EXPOSE 8000
# Copy the application code
COPY . .
# Run the application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

BIN
logo-case-cocher.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

48
main.py
View file

@ -1,7 +1,7 @@
import datetime
import logging
from fastapi import FastAPI
from fastapi import FastAPI, UploadFile
from fastapi.responses import FileResponse
import pypandoc
import json
@ -92,6 +92,51 @@ async def get_format_parameters(style: str, format: str):
return parameters
@app.get("/images/")
async def get_images():
# list all files in resources/images
files = [f for f in os.listdir("./resources/images") if os.path.isfile(os.path.join("./resources/images", f))]
# sort the files
files.sort()
return {"images": files}
@app.post("/images/")
async def ajouter_image(file: UploadFile):
"""
Add an image to the images folder.
:param file:
:return:
"""
image_path = f"{os.getcwd()}/resources/images/{file.filename}"
try:
contents = file.file.read()
with open(image_path, 'wb') as f:
f.write(contents)
except Exception as e:
return {"message": f"There was an error uploading the file: {e}"}
finally:
file.file.close()
return {"message": f"Successfully uploaded all files"}
@app.delete("/images/{nom_image}")
async def supprimer_image(nom_image: str):
"""
Delete an image from the images folder.
:param nom_image:
:return:
"""
image_path = f"{os.getcwd()}/resources/images/{nom_image}"
try:
os.remove(image_path)
except Exception as e:
return {"message": f"There was an error deleting the file: {e}"}
finally:
return {"message": f"Successfully deleted {nom_image}"}
@app.get("/generer/")
async def generer(specs: DocumentSpecs):
header_file = f'{os.getcwd()}/styles/{specs.style}/{specs.format}/header.tex'
@ -106,6 +151,7 @@ async def generer(specs: DocumentSpecs):
'--dpi=300',
f'--toc-depth={specs.tocdepth}',
f'--pdf-engine={specs.pdfengine}',
f'--resource-path={os.getcwd()}/resources/',
'-V', f'linkcolor={specs.linkcolor}',
'-V', f'fontsize={specs.fontsize}pt',
'-V', f'geometry:paperwidth={round(specs.paperwidth * specs.ratio / 100, -1) / 300}in',

View file

@ -25,4 +25,5 @@ setuptools==67.7.2
pypandoc==1.11
Wand~=0.6.10
emoji~=2.2.0
opencv-python~=4.7.0.68
opencv-python~=4.7.0.68
python-multipart~=0.0.6

View file

View file

@ -1,5 +1,9 @@
# Test your FastAPI endpoints
###
GET http://127.0.0.1:8000/generer/
Content-Type: application/json
Accept: application/zip

8
upload_image.sh Normal file
View file

@ -0,0 +1,8 @@
curl -X POST --location "http://127.0.0.1:8000/images/" \
-H "Accept: application/json" \
-H "Content-Type: multipart/form-data; boundary=boundary" \
-F "file=@logo-case-cocher.png"
curl -X GET --location "http://127.0.0.1:8000/images/"
curl -X DELETE --location "http://127.0.0.1:8000/images/logo-case-cocher.png"