Some code refactors

This commit is contained in:
Dmitriy Shishkov 2021-06-29 20:12:52 +05:00
parent 0ccf9ee770
commit 52057c0cd5
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
2 changed files with 47 additions and 36 deletions

View File

@ -7,53 +7,60 @@ from ebooklib import epub
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
"""
tokens = await epub_to_tokens(file)
...
# TODO: join tokens to HTML
html_content = ""
...
return html_content
try:
tokens = await epub_to_tokens(file)
...
# TODO: join tokens to HTML
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]:
"""
Passes file content to ebooklib library and parses epub tokens into dict of the following format:
"\<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
"""
tokens = {"metadata": {"test"}}
async with aiof.tempfile.NamedTemporaryFile() as tmp:
await tmp.write(file.read())
await tmp.seek(0)
content = await tmp.read()
try:
book = epub.read_epub(tmp.name)
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()
book = epub.read_epub(tmp.name)
for item in book.get_items():
tokens[name] = f'data:{media_type};base64,{b64_content}'
elif item_type == ebooklib.ITEM_DOCUMENT:
name = item.get_name()
content = item.get_content()
item_type = item.get_type()
name = item.get_name()
content = item.get_content()
tokens[name] = content
return tokens
except Exception as e:
return 'Error! Wrong epub file format: ' + str(e)
if item_type == ebooklib.ITEM_DOCUMENT:
tokens[name] = content
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

View File

@ -5,16 +5,20 @@ from .epub import epub2html
app = FastAPI()
@app.get('/')
@app.get("/")
def root():
return "Hello, World!"
@app.post('/uploadfile/', )
@app.post(
"/uploadfile/",
)
async def create_upload_file(file: UploadFile = File(...)):
if file.filename.endswith('.epub'):
if file.filename.endswith(".epub"):
content = await epub2html(file.file)
elif file.filename.endswith('.fb2'):
elif file.filename.endswith(".fb2"):
content = await fb22html(file.file)
else:
content = 'Error! Unsupported file type'
return HTMLResponse(content=content)
content = "Error! Unsupported file type"
return HTMLResponse(content=content)