diff --git a/Dockerfile b/Dockerfile index 5fd104a..75e2fc4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/logo-case-cocher.png b/logo-case-cocher.png new file mode 100644 index 0000000..cc649a9 Binary files /dev/null and b/logo-case-cocher.png differ diff --git a/main.py b/main.py index 5e49225..abe2253 100644 --- a/main.py +++ b/main.py @@ -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', diff --git a/requirements.txt b/requirements.txt index ac62ba1..9c2b9c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +opencv-python~=4.7.0.68 +python-multipart~=0.0.6 diff --git a/resources/images/__file__ b/resources/images/__file__ new file mode 100644 index 0000000..e69de29 diff --git a/test_main.http b/test_main.http index ab92d6c..f86e065 100644 --- a/test_main.http +++ b/test_main.http @@ -1,5 +1,9 @@ # Test your FastAPI endpoints + + +### + GET http://127.0.0.1:8000/generer/ Content-Type: application/json Accept: application/zip diff --git a/upload_image.sh b/upload_image.sh new file mode 100644 index 0000000..0b65af3 --- /dev/null +++ b/upload_image.sh @@ -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"