Some code refactors
This commit is contained in:
parent
0ccf9ee770
commit
52057c0cd5
67
app/epub.py
67
app/epub.py
@ -7,53 +7,60 @@ from ebooklib import epub
|
|||||||
from tempfile import SpooledTemporaryFile
|
from tempfile import SpooledTemporaryFile
|
||||||
|
|
||||||
|
|
||||||
async def epub2html(file: SpooledTemporaryFile):
|
async def epub2html(file: SpooledTemporaryFile) -> str:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Splits epub to tokens and joins them to one html file
|
Splits epub to tokens and joins them to one html file
|
||||||
"""
|
"""
|
||||||
|
|
||||||
tokens = await epub_to_tokens(file)
|
try:
|
||||||
...
|
|
||||||
# TODO: join tokens to HTML
|
tokens = await epub_to_tokens(file)
|
||||||
html_content = ""
|
...
|
||||||
...
|
# TODO: join tokens to HTML
|
||||||
return html_content
|
html_content = ""
|
||||||
|
...
|
||||||
|
return html_content
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return "Error! Wrong epub file format: " + str(e)
|
||||||
|
|
||||||
|
|
||||||
async def epub_to_tokens(file: SpooledTemporaryFile) -> dict[str, str]:
|
async def epub_to_tokens(file: SpooledTemporaryFile) -> dict[str, str]:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Passes file content to ebooklib library and parses epub tokens into dict of the following format:
|
Passes file content to ebooklib library and parses epub tokens into dict of the following format:
|
||||||
|
|
||||||
"\<file_name\>": "\<file_content\>"
|
"\<file_name\>": "\<file_content\>"
|
||||||
|
|
||||||
Where file content is either plain text for xhtml or base64 encoded data for other formats, prepared for embeding to html
|
Where file content is either plain text for xhtml or base64 encoded data for other formats, prepared for embeding to html
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
tokens = {"metadata": {"test"}}
|
||||||
|
|
||||||
async with aiof.tempfile.NamedTemporaryFile() as tmp:
|
async with aiof.tempfile.NamedTemporaryFile() as tmp:
|
||||||
|
|
||||||
await tmp.write(file.read())
|
await tmp.write(file.read())
|
||||||
await tmp.seek(0)
|
|
||||||
content = await tmp.read()
|
|
||||||
|
|
||||||
try:
|
book = epub.read_epub(tmp.name)
|
||||||
book = epub.read_epub(tmp.name)
|
for item in book.get_items():
|
||||||
tokens = {}
|
|
||||||
for item in book.get_items():
|
|
||||||
item_type = item.get_type()
|
|
||||||
if item_type in (ebooklib.ITEM_COVER, ebooklib.ITEM_IMAGE, ebooklib.ITEM_STYLE, ebooklib.ITEM_VIDEO):
|
|
||||||
name = item.get_name()
|
|
||||||
media_type = item.media_type
|
|
||||||
b64_content = b64encode(item.get_content()).decode()
|
|
||||||
|
|
||||||
tokens[name] = f'data:{media_type};base64,{b64_content}'
|
item_type = item.get_type()
|
||||||
elif item_type == ebooklib.ITEM_DOCUMENT:
|
name = item.get_name()
|
||||||
name = item.get_name()
|
content = item.get_content()
|
||||||
content = item.get_content()
|
|
||||||
|
|
||||||
tokens[name] = content
|
if item_type == ebooklib.ITEM_DOCUMENT:
|
||||||
return tokens
|
tokens[name] = content
|
||||||
except Exception as e:
|
|
||||||
return 'Error! Wrong epub file format: ' + str(e)
|
elif item_type in (
|
||||||
|
ebooklib.ITEM_COVER,
|
||||||
|
ebooklib.ITEM_IMAGE,
|
||||||
|
ebooklib.ITEM_STYLE,
|
||||||
|
ebooklib.ITEM_VIDEO,
|
||||||
|
ebooklib.ITEM_VECTOR,
|
||||||
|
):
|
||||||
|
media_type = item.media_type
|
||||||
|
b64_content = b64encode(content).decode()
|
||||||
|
|
||||||
|
tokens[name] = f"data:{media_type};base64,{b64_content}"
|
||||||
|
|
||||||
|
return tokens
|
||||||
|
16
app/main.py
16
app/main.py
@ -5,16 +5,20 @@ from .epub import epub2html
|
|||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
@app.get('/')
|
|
||||||
|
@app.get("/")
|
||||||
def root():
|
def root():
|
||||||
return "Hello, World!"
|
return "Hello, World!"
|
||||||
|
|
||||||
@app.post('/uploadfile/', )
|
|
||||||
|
@app.post(
|
||||||
|
"/uploadfile/",
|
||||||
|
)
|
||||||
async def create_upload_file(file: UploadFile = File(...)):
|
async def create_upload_file(file: UploadFile = File(...)):
|
||||||
if file.filename.endswith('.epub'):
|
if file.filename.endswith(".epub"):
|
||||||
content = await epub2html(file.file)
|
content = await epub2html(file.file)
|
||||||
elif file.filename.endswith('.fb2'):
|
elif file.filename.endswith(".fb2"):
|
||||||
content = await fb22html(file.file)
|
content = await fb22html(file.file)
|
||||||
else:
|
else:
|
||||||
content = 'Error! Unsupported file type'
|
content = "Error! Unsupported file type"
|
||||||
return HTMLResponse(content=content)
|
return HTMLResponse(content=content)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user