From 3ca47d915ee386e6e26a842ace9558df30399f42 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sun, 4 Jul 2021 14:05:07 +0500 Subject: [PATCH] Added hash for book content and fixed typing errors --- app/epub.py | 6 ++---- app/fb2.py | 4 ++-- app/main.py | 9 ++++++--- app/utils.py | 12 ++++++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/epub.py b/app/epub.py index 3259737..596a2a4 100644 --- a/app/epub.py +++ b/app/epub.py @@ -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) diff --git a/app/fb2.py b/app/fb2.py index 8cd4129..cb8ad2b 100644 --- a/app/fb2.py +++ b/app/fb2.py @@ -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 diff --git a/app/main.py b/app/main.py index 2d0bbc2..57a9192 100644 --- a/app/main.py +++ b/app/main.py @@ -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 diff --git a/app/utils.py b/app/utils.py index f398dff..52da556 100644 --- a/app/utils.py +++ b/app/utils.py @@ -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