from fastapi import Request, Form, APIRouter, File, UploadFile
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from ..library.helpers import *

from alexandria import scanner

router = APIRouter()
templates = Jinja2Templates(directory="templates/")


@router.post("/form_str", response_class=HTMLResponse)
def form_post1(request: Request, book: str = Form(...)):
    s = scanner.Scanner()
    s.scan()
    str2search = book
    outfile = s.search(str2search)

    return templates.TemplateResponse('upload.html', context={'request': request, 'input_book': book, 'outfile': outfile })


@router.get("/upload", response_class=HTMLResponse)
def get_upload(request: Request):
    result = "Hello from upload.py"
    return templates.TemplateResponse('upload.html', context={'request': request, 'result': result})


@router.post("/upload/new/")
async def post_upload(imgdata: tuple, file: UploadFile = File(...)):
    print(file)
    data_dict = eval(imgdata[0])
    winWidth, imgWidth, imgHeight = data_dict["winWidth"], data_dict["imgWidth"], data_dict["imgHeight"]

    # create the full path
    workspace = create_workspace()
    # filename
    file_path = Path(file.filename)
    # image full path
    img_full_path = workspace / file_path
    with open(str(img_full_path), 'wb') as myfile:
        contents = await file.read()
        myfile.write(contents)
    # create a thumb image and save it
    thumb(img_full_path, winWidth, imgWidth, imgHeight)
    # create the thumb path
    # ext is like .png or .jpg
    filepath, ext = os.path.splitext(img_full_path)
    thumb_path = filepath + ".thumbnail"+ext

    data = {
        "img_path": img_full_path,
        "thumb_path": thumb_path
    }
    return data

2f18d20157dd372344286deca2fe8e0436ec9c92
<mask token>


@router.post('/form_str', response_class=HTMLResponse)
def form_post1(request: Request, book: str=Form(...)):
    s = scanner.Scanner()
    s.scan()
    str2search = book
    outfile = s.search(str2search)
    return templates.TemplateResponse('upload.html', context={'request':
        request, 'input_book': book, 'outfile': outfile})


@router.get('/upload', response_class=HTMLResponse)
def get_upload(request: Request):
    result = 'Hello from upload.py'
    return templates.TemplateResponse('upload.html', context={'request':
        request, 'result': result})


<mask token>

<mask token>


@router.post('/form_str', response_class=HTMLResponse)
def form_post1(request: Request, book: str=Form(...)):
    s = scanner.Scanner()
    s.scan()
    str2search = book
    outfile = s.search(str2search)
    return templates.TemplateResponse('upload.html', context={'request':
        request, 'input_book': book, 'outfile': outfile})


@router.get('/upload', response_class=HTMLResponse)
def get_upload(request: Request):
    result = 'Hello from upload.py'
    return templates.TemplateResponse('upload.html', context={'request':
        request, 'result': result})


@router.post('/upload/new/')
async def post_upload(imgdata: tuple, file: UploadFile=File(...)):
    print(file)
    data_dict = eval(imgdata[0])
    winWidth, imgWidth, imgHeight = data_dict['winWidth'], data_dict['imgWidth'
        ], data_dict['imgHeight']
    workspace = create_workspace()
    file_path = Path(file.filename)
    img_full_path = workspace / file_path
    with open(str(img_full_path), 'wb') as myfile:
        contents = await file.read()
        myfile.write(contents)
    thumb(img_full_path, winWidth, imgWidth, imgHeight)
    filepath, ext = os.path.splitext(img_full_path)
    thumb_path = filepath + '.thumbnail' + ext
    data = {'img_path': img_full_path, 'thumb_path': thumb_path}
    return data

<mask token>
router = APIRouter()
templates = Jinja2Templates(directory='templates/')


@router.post('/form_str', response_class=HTMLResponse)
def form_post1(request: Request, book: str=Form(...)):
    s = scanner.Scanner()
    s.scan()
    str2search = book
    outfile = s.search(str2search)
    return templates.TemplateResponse('upload.html', context={'request':
        request, 'input_book': book, 'outfile': outfile})


@router.get('/upload', response_class=HTMLResponse)
def get_upload(request: Request):
    result = 'Hello from upload.py'
    return templates.TemplateResponse('upload.html', context={'request':
        request, 'result': result})


@router.post('/upload/new/')
async def post_upload(imgdata: tuple, file: UploadFile=File(...)):
    print(file)
    data_dict = eval(imgdata[0])
    winWidth, imgWidth, imgHeight = data_dict['winWidth'], data_dict['imgWidth'
        ], data_dict['imgHeight']
    workspace = create_workspace()
    file_path = Path(file.filename)
    img_full_path = workspace / file_path
    with open(str(img_full_path), 'wb') as myfile:
        contents = await file.read()
        myfile.write(contents)
    thumb(img_full_path, winWidth, imgWidth, imgHeight)
    filepath, ext = os.path.splitext(img_full_path)
    thumb_path = filepath + '.thumbnail' + ext
    data = {'img_path': img_full_path, 'thumb_path': thumb_path}
    return data

from fastapi import Request, Form, APIRouter, File, UploadFile
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from ..library.helpers import *
from alexandria import scanner
router = APIRouter()
templates = Jinja2Templates(directory='templates/')


@router.post('/form_str', response_class=HTMLResponse)
def form_post1(request: Request, book: str=Form(...)):
    s = scanner.Scanner()
    s.scan()
    str2search = book
    outfile = s.search(str2search)
    return templates.TemplateResponse('upload.html', context={'request':
        request, 'input_book': book, 'outfile': outfile})


@router.get('/upload', response_class=HTMLResponse)
def get_upload(request: Request):
    result = 'Hello from upload.py'
    return templates.TemplateResponse('upload.html', context={'request':
        request, 'result': result})


@router.post('/upload/new/')
async def post_upload(imgdata: tuple, file: UploadFile=File(...)):
    print(file)
    data_dict = eval(imgdata[0])
    winWidth, imgWidth, imgHeight = data_dict['winWidth'], data_dict['imgWidth'
        ], data_dict['imgHeight']
    workspace = create_workspace()
    file_path = Path(file.filename)
    img_full_path = workspace / file_path
    with open(str(img_full_path), 'wb') as myfile:
        contents = await file.read()
        myfile.write(contents)
    thumb(img_full_path, winWidth, imgWidth, imgHeight)
    filepath, ext = os.path.splitext(img_full_path)
    thumb_path = filepath + '.thumbnail' + ext
    data = {'img_path': img_full_path, 'thumb_path': thumb_path}
    return data

from fastapi import Request, Form, APIRouter, File, UploadFile
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from ..library.helpers import *

from alexandria import scanner

router = APIRouter()
templates = Jinja2Templates(directory="templates/")


@router.post("/form_str", response_class=HTMLResponse)
def form_post1(request: Request, book: str = Form(...)):
    s = scanner.Scanner()
    s.scan()
    str2search = book
    outfile = s.search(str2search)

    return templates.TemplateResponse('upload.html', context={'request': request, 'input_book': book, 'outfile': outfile })


@router.get("/upload", response_class=HTMLResponse)
def get_upload(request: Request):
    result = "Hello from upload.py"
    return templates.TemplateResponse('upload.html', context={'request': request, 'result': result})


@router.post("/upload/new/")
async def post_upload(imgdata: tuple, file: UploadFile = File(...)):
    print(file)
    data_dict = eval(imgdata[0])
    winWidth, imgWidth, imgHeight = data_dict["winWidth"], data_dict["imgWidth"], data_dict["imgHeight"]

    # create the full path
    workspace = create_workspace()
    # filename
    file_path = Path(file.filename)
    # image full path
    img_full_path = workspace / file_path
    with open(str(img_full_path), 'wb') as myfile:
        contents = await file.read()
        myfile.write(contents)
    # create a thumb image and save it
    thumb(img_full_path, winWidth, imgWidth, imgHeight)
    # create the thumb path
    # ext is like .png or .jpg
    filepath, ext = os.path.splitext(img_full_path)
    thumb_path = filepath + ".thumbnail"+ext

    data = {
        "img_path": img_full_path,
        "thumb_path": thumb_path
    }
    return data
