type hints and cleanup

This commit is contained in:
Darryl Nixon 2023-05-20 10:48:55 -07:00
parent 1f5afc3000
commit feeebd8fe5
2 changed files with 8 additions and 11 deletions

View file

@ -8,9 +8,10 @@ import binwalk
import os import os
from aiohttp_compress import compress_middleware from aiohttp_compress import compress_middleware
from aiohttp import web from aiohttp import web
from typing import List, Tuple, Dict, Union
async def scan_file(filename, base_dir): async def scan_file(filename: str, base_dir: str) -> List:
try: try:
scan = binwalk.scan( scan = binwalk.scan(
filename, filename,
@ -26,7 +27,7 @@ async def scan_file(filename, base_dir):
print("Critical failure: ", e) print("Critical failure: ", e)
async def build_listing(path): async def build_listing(path: str) -> Tuple[int, int, Dict]:
result = {} result = {}
num_files, num_dirs = 0, 0 num_files, num_dirs = 0, 0
for item in os.listdir(path): for item in os.listdir(path):
@ -41,7 +42,7 @@ async def build_listing(path):
return num_files, num_dirs, result return num_files, num_dirs, result
async def upload_file(request): async def upload_file(request: web.Request) -> web.json_response:
reader = await request.multipart() reader = await request.multipart()
field = await reader.next() field = await reader.next()
assert field.name == "file" assert field.name == "file"
@ -119,18 +120,18 @@ async def upload_file(request):
return web.json_response(response_data) return web.json_response(response_data)
async def serve_index(request): async def serve_index(request: web.Request) -> web.FileResponse:
return web.FileResponse("index.html") return web.FileResponse("index.html")
async def serve_static(request): async def serve_static(request: web.Request) -> Union[web.FileResponse, web.HTTPNotFound]:
path = request.path[1:] path = request.path[1:]
if not path.startswith("static/"): if not path.startswith("static/"):
return web.HTTPNotFound() return web.HTTPNotFound()
return web.FileResponse(path) return web.FileResponse(path)
async def main(): async def main() -> None:
app = web.Application() app = web.Application()
app.middlewares.append(compress_middleware) app.middlewares.append(compress_middleware)

View file

@ -209,14 +209,10 @@ async function process_upload(file) {
}); });
if (response.ok) { if (response.ok) {
// if (1 === 1) {
const data = await response.json(); const data = await response.json();
// const json = '{"meta":{"name":"miwifi_r4av2_firmware_6bdd4_2.30.500.bin","sizeb":13632488,"sha1":"6cefd9d5de3b80799d0341b19043d108624dcaca","md5":"639616a5b70983903ccdd019a7a6bdd4","sig_quant":411,"duration":"00:05.457"},"offsets":[{"start":960,"end":13630770,"d":"LZMA compressed data"},{"start":1704660,"end":13621866,"d":"Squashfs filesystem"},{"start":4534008,"end":5267984,"d":"xz compressed data"},{"start":5258380,"end":5267984,"d":"ASCII cpio archive"}]}';
// const data = JSON.parse(json);
const container = document.querySelector('.container'); const container = document.querySelector('.container');
// visualization
container.innerHTML = ` container.innerHTML = `
<div id="blob-row"> <div id="blob-row">
<canvas id="blob" width="1200px" height="60px" style="background-color: #18232c;"></canvas> <canvas id="blob" width="1200px" height="60px" style="background-color: #18232c;"></canvas>