Added hash for book content and fixed typing errors

This commit is contained in:
Dmitriy Shishkov 2021-07-04 14:05:07 +05:00
parent 605af559f6
commit 3ca47d915e
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
4 changed files with 22 additions and 9 deletions

View File

@ -7,10 +7,10 @@ from ebooklib import epub
from tempfile import SpooledTemporaryFile
from .utils import Document_Tokens, strip_whitespace
from .utils import Document_Tokens, strip_whitespace, HTMLBook
async def epub2html(file: SpooledTemporaryFile) -> str:
async def epub2html(file: SpooledTemporaryFile) -> HTMLBook:
"""
Splits epub to tokens and joins them to one html file
@ -107,8 +107,6 @@ def set_cover(tokens: Document_Tokens):
def epub_tokens2html(spine: list[tuple[str, str]], tokens: Document_Tokens):
res = b""
print(spine)
for name, enabled in spine:
if name in tokens.keys():
res += process_xhtml(tokens[name], tokens)

View File

@ -4,7 +4,7 @@ from xml.etree.ElementTree import Element
from typing import Optional
from fastapi import HTTPException
from .utils import Document_Tokens, strip_whitespace
from .utils import Document_Tokens, strip_whitespace, HTMLBook
namespaces = {
@ -14,7 +14,7 @@ namespaces = {
HREF = f"{{{namespaces['xlink']}}}href"
async def fb22html(file: SpooledTemporaryFile) -> dict[str, str]:
async def fb22html(file: SpooledTemporaryFile) -> HTMLBook:
"""
Splits fb2 to tokens and joins them to one html file

View File

@ -2,7 +2,7 @@ from fastapi import FastAPI, File, UploadFile, HTTPException
from .epub import epub2html
from .fb2 import fb22html
from .utils import HTMLBook
from .utils import HashedHTMLBook, add_hash
app = FastAPI()
@ -12,7 +12,7 @@ def root():
return "Hello, World!"
@app.post("/uploadfile/", response_model=HTMLBook)
@app.post("/uploadfile/", response_model=HashedHTMLBook)
async def create_upload_file(file: UploadFile = File(...)):
if file.filename.endswith(".fb2"):
content = await fb22html(file.file)
@ -20,4 +20,7 @@ async def create_upload_file(file: UploadFile = File(...)):
content = await epub2html(file.file)
else:
raise HTTPException(status_code=415, detail="Error! Unsupported file type")
return content
h_content = add_hash(content)
return h_content

View File

@ -1,6 +1,7 @@
from typing import Union, Optional
from pydantic import BaseModel
import re
from hashlib import sha256
Document_Tokens = dict[str, Union[str, dict[str, str]]]
@ -12,6 +13,10 @@ class HTMLBook(BaseModel):
content: str
class HashedHTMLBook(HTMLBook):
hash: str
replacements = [
("
", "\r"),
(">\s+?<", "><"),
@ -25,3 +30,10 @@ def strip_whitespace(s: bytes) -> str:
res = re.sub(old, new, res)
return res.strip()
def add_hash(content: HTMLBook) -> HashedHTMLBook:
h_content: HashedHTMLBook = content.copy()
h_content["hash"] = sha256(content["content"].encode()).hexdigest()
return h_content