Gros refactoring

This commit is contained in:
François Pelletier 2024-12-31 17:00:07 -05:00
parent 6008aa68f6
commit 4a6bfc951f
368 changed files with 22503 additions and 3 deletions

View file

@ -0,0 +1,34 @@
import json
import logging
import os
from typing import Annotated
from fastapi import APIRouter, Depends
from FormatParameters import FormatParameters
from list_dir import list_dir
from authentication import get_current_active_user
from models import User
from responses import Styles, Formats
router = APIRouter()
@router.get("/styles/")
async def get_styles(current_user: Annotated[User, Depends(get_current_active_user)]):
styles = Styles(styles=list_dir(f"{os.getcwd()}/styles"))
return styles
@router.get("/formats/{style}/")
async def get_formats(style: str, current_user: Annotated[User, Depends(get_current_active_user)]):
formats = Formats(formats=list_dir(f"{os.getcwd()}/styles/{style}/"))
return formats
@router.get("/format_parameters/{style}/{format}/")
async def get_format_parameters(style: str, format: str, current_user: Annotated[User, Depends(get_current_active_user)]):
# open styles/format_parameters.json as a dictionary
with open(f"{os.getcwd()}/styles/{style}/format_parameters.json", "r") as f:
format_data = json.load(f).get(format)
logging.info(str(format_data))
# load data from format_data into the FormatParameters object
parameters = FormatParameters(**format_data)
return parameters

159
backend/routers/generer.py Normal file
View file

@ -0,0 +1,159 @@
import logging
import os
import shutil
import zipfile
from datetime import datetime
from typing import Annotated
import pypandoc
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks
from starlette.responses import FileResponse
from DocumentSpecs import DocumentSpecs
from convert_pdf import convert_pdf
from convert_video import convert_video
from extract_emojis import replace_emojis
from authentication import get_current_active_user
from models import User
router = APIRouter()
logger = logging.getLogger(__name__)
def cleanup_task(output_dir: str):
logger.info(f"Cleaning up temporary directory: {output_dir}")
shutil.rmtree(output_dir)
logger.info("Cleanup complete")
@router.post("/")
async def generer(specs: DocumentSpecs, background_tasks: BackgroundTasks, current_user: Annotated[User, Depends(get_current_active_user)]):
logger.info(f"Starting document generation for user: {current_user.username}")
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
base_name = f"{specs.style}-{specs.format}-{timestamp}"
output_dir = f"./out/{base_name}"
os.makedirs(output_dir, exist_ok=True)
logger.info(f"Created output directory: {output_dir}")
header_file = f'{os.getcwd()}/styles/{specs.style}/{specs.format}/header.tex'
cover_file = f'{os.getcwd()}/styles/{specs.style}/{specs.format}/cover.tex'
logger.debug(f"Header file: {header_file}, Cover file: {cover_file}")
filters = ['latex-emoji.lua', 'centered.lua']
pdoc_args = [
f'--include-in-header={header_file}',
f'--include-after-body={cover_file}',
'--listings',
'--dpi=300',
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, -1) / 300}in',
'-V', f'geometry:paperheight={round(specs.paperheight, -1) / 300}in',
'-V', f'geometry:left={specs.margin / 300}in',
'-V', f'geometry:right={specs.margin / 300}in',
'-V', f'geometry:top={specs.vmargin / 300}in',
'-V', f'geometry:bottom={specs.vmargin / 300}in'
]
logger.debug(f"Pandoc arguments: {pdoc_args}")
pdf_file_path = f"{output_dir}/{base_name}.pdf"
markdown_file_path = f"{output_dir}/{base_name}.md"
latex_file_path = f"{output_dir}/{base_name}.tex"
images_path = f"{output_dir}/images"
video_file_path = f"{output_dir}/{base_name}.mp4"
try:
logger.info(f"Current working directory: {os.getcwd()}")
text_to_convert = replace_emojis(specs.content)
logger.debug("Emojis replaced in content")
# Save Markdown content
with open(markdown_file_path, 'w', encoding='utf-8') as md_file:
md_file.write(text_to_convert)
logger.info(f"Markdown file saved: {markdown_file_path}")
# Generate PDF and LaTeX
logger.info("Generating PDF...")
pypandoc.convert_text(source=text_to_convert,
to='pdf',
format='markdown+implicit_figures+smart+emoji',
encoding='utf-8',
extra_args=pdoc_args,
filters=filters,
cworkdir=os.getcwd(),
outputfile=pdf_file_path
)
logger.info(f"PDF generated: {pdf_file_path}")
logger.info("Generating LaTeX...")
pypandoc.convert_text(source=text_to_convert,
to='latex',
format='markdown+implicit_figures+smart+emoji',
encoding='utf-8',
extra_args=pdoc_args,
filters=filters,
cworkdir=os.getcwd(),
outputfile=latex_file_path
)
logger.info(f"LaTeX file generated: {latex_file_path}")
# Generate JPG images
os.makedirs(images_path, exist_ok=True)
logger.info(f"Converting PDF to JPG images in {images_path}")
convert_pdf(pdf_file_path, "jpg", images_path, resolution=300)
logger.info("JPG images generated")
# Generate MP4 video
logger.info("Generating MP4 video...")
try:
success = convert_video(
images_path=images_path,
output_path=video_file_path,
width=specs.paperwidth,
height=specs.paperheight,
fps=specs.fps,
stilltime=specs.stilltime
)
if success:
logger.info(f"MP4 video generated: {video_file_path}")
else:
logger.error(f"Failed to generate MP4 video: {video_file_path}")
raise Exception("Video generation failed")
except Exception as e:
logger.exception(f"Error during video generation: {str(e)}")
raise HTTPException(status_code=500, detail=f"Video generation failed: {str(e)}")
# Create ZIP file
zip_file_path = f"{output_dir}/{base_name}.zip"
logger.info(f"Creating ZIP file: {zip_file_path}")
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
zipf.write(pdf_file_path, os.path.basename(pdf_file_path))
zipf.write(markdown_file_path, os.path.basename(markdown_file_path))
zipf.write(latex_file_path, os.path.basename(latex_file_path))
zipf.write(video_file_path, os.path.basename(video_file_path))
for root, _, files in os.walk(images_path):
for file in files:
zipf.write(os.path.join(root, file),
os.path.join(f"{base_name}_images", file))
logger.info("ZIP file created successfully")
logger.info(f"Returning FileResponse for {zip_file_path}")
return FileResponse(zip_file_path, filename=f"{base_name}.zip")
except Exception as e:
logger.exception(f"Error during document generation: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
finally:
# Schedule the cleanup task to run in the background after the response is sent
background_tasks.add_task(cleanup_task, output_dir)
@router.post("/cleanup/{base_name}")
async def cleanup(base_name: str, current_user: Annotated[User, Depends(get_current_active_user)]):
output_dir = f"./out/{base_name}"
if os.path.exists(output_dir):
cleanup_task(output_dir)
return {"message": f"Cleanup for {base_name} completed successfully"}
else:
raise HTTPException(status_code=404, detail=f"Directory for {base_name} not found")

58
backend/routers/images.py Normal file
View file

@ -0,0 +1,58 @@
import os
from typing import Annotated
from fastapi import APIRouter, Depends, UploadFile
from starlette.responses import FileResponse
from authentication import get_current_active_user
from models import User
router = APIRouter()
@router.get("/")
async def get_images(current_user: Annotated[User, Depends(get_current_active_user)]):
# list all files in resources/images
files = [f for f in os.listdir(f"{os.getcwd()}/resources/images") if os.path.isfile(os.path.join(f"{os.getcwd()}/resources/images", f))]
# sort the files
files.sort()
return {"images": files}
@router.get("/{nom_image}")
async def get_image(nom_image: str, current_user: Annotated[User, Depends(get_current_active_user)]):
return FileResponse(f"{os.getcwd()}/resources/images/{nom_image}")
@router.post("/")
async def ajouter_image(file: UploadFile, current_user: Annotated[User, Depends(get_current_active_user)]):
"""
Add an image to the images folder.
:param current_user:
: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"}
@router.delete("/{nom_image}")
async def supprimer_image(nom_image: str, current_user: Annotated[User, Depends(get_current_active_user)]):
"""
Delete an image from the images folder.
:param current_user:
: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}"}

31
backend/routers/users.py Normal file
View file

@ -0,0 +1,31 @@
from datetime import timedelta
from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException
from fastapi.security import OAuth2PasswordRequestForm
from starlette import status
from authentication import authenticate_user, create_access_token, get_current_active_user
from config import ACCESS_TOKEN_EXPIRE_MINUTES, get_fake_users_db
from models import Token, User
router = APIRouter()
# Update the token endpoint in your main.py or wherever it's defined
@router.post("/token", response_model=Token)
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
try:
user = authenticate_user(get_fake_users_db(), form_data.username, form_data.password)
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(
data={"sub": user.username}, expires_delta=access_token_expires
)
return {"access_token": access_token, "token_type": "bearer"}
except HTTPException as e:
raise e
@router.get("/users/me")
async def read_users_me(
current_user: Annotated[User, Depends(get_current_active_user)],
):
return current_user