diff options
author | Keuin <[email protected]> | 2023-06-04 15:28:11 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-06-04 15:28:11 +0800 |
commit | 063e8fed7ee2b4f6c6d2adfc5f244f7d5cd5b9e2 (patch) | |
tree | 1ec9118da132a2003da020969f5ea692520363f0 /web.py | |
parent | 6c8ddae3f8bf9fb37d113bc0dc04cd0ffaebd9c3 (diff) |
bugfix: PDF preview is buggy
Diffstat (limited to 'web.py')
-rw-r--r-- | web.py | 19 |
1 files changed, 11 insertions, 8 deletions
@@ -17,18 +17,16 @@ utaten_pattern = re.compile(r'[a-z0-9]+') tex_generator = texgen.TexGenerator('pdf_cache', 'temp', 20) html_cache = htmlcache.HtmlCache('html_cache') -suggest_file_name = False +preview_pdf = False @app.get("/utaten/{item_id}.pdf") async def get_utaten_lyric_pdf(item_id: str): - global suggest_file_name + global preview_pdf try: lyric_info = await html_cache.get_utaten_tex_source(item_id) pdf_path = await tex_generator.xelatex(lyric_info.tex_source) - if not suggest_file_name: - filename = None - elif lyric_info.title and lyric_info.artist: + if lyric_info.title and lyric_info.artist: filename = f'{lyric_info.title} - {lyric_info.artist}.pdf' elif not lyric_info.title and not lyric_info.artist: filename = f'{lyric_info.utaten_id}.pdf' @@ -36,7 +34,12 @@ async def get_utaten_lyric_pdf(item_id: str): filename = f'{lyric_info.title} - {lyric_info.utaten_id}.pdf' else: filename = f'{lyric_info.artist} - {lyric_info.utaten_id}.pdf' - return FileResponse(pdf_path, media_type='application/pdf', filename=filename) + if preview_pdf: + content_disposition_type = 'inline' + else: + content_disposition_type = 'attachment' + return FileResponse(pdf_path, media_type='application/pdf', filename=filename, + content_disposition_type=content_disposition_type) except texgen.TexGenerationError as e: return Response(content=f'Failed to generate tex file: {e}', status_code=502) @@ -68,9 +71,9 @@ if __name__ == '__main__': p = argparse.ArgumentParser(prog='utaten2tex') p.add_argument('-l', '--host', default='127.0.0.1') p.add_argument('-p', '--port', default='8080') - p.add_argument('-s', '--suggest-file-name', action='store_true', default=False) + p.add_argument('-P', '--preview-pdf', action='store_true', default=False) args = p.parse_args() - suggest_file_name = args.suggest_file_name + preview_pdf = args.preview_pdf setup_loop() uvicorn.run( 'web:app', |