async def post_image(request: Request, key: str = Form(...), secret: str = Form(None), file: UploadFile = File(None), mode: str = Form(...)): if mode == "encode": if secret: pxl = Rothko(key).encode_to_img(secret, scale=True) pxl.save(TMP) # redirect user to page when they can download image return RedirectResponse(url=f"/encoded/{pxl.id}", status_code=status.HTTP_303_SEE_OTHER) else: result = "Secret field was not filled but is required for encoding" else: try: img = read_bytes(await file.read()) result = Rothko(key).decode_from_img(img) except (SyntaxError, AttributeError): # file was not supplied or is not valid png result = "Image was not supplied but is required by decode method" return templates.TemplateResponse('img.html', context={ 'request': request, 'result': result, })
def ed_image_helper(key, og): encoded = Rothko(key).encode_to_img(og) save_path = encoded.save(TEST_DIR) try: with open(save_path, "rb") as img: decoded = Rothko(key).decode_from_img(img) finally: # always cleanup unless something went horribly wrong if os.path.isfile(save_path) and save_path.split(".")[-1] == "png": os.unlink(save_path) assert og == decoded
def post_req(request: Request, key: str = Form(...), secret: str = Form(...), mode: str = Form(...)): if mode == "encode": result = Rothko(key).encode_to_string(secret) else: result = Rothko(key).decode_from_string(secret) return templates.TemplateResponse('serve.html', context={ 'request': request, 'result': result, 'key': key, })
async def encode_img(background_tasks: BackgroundTasks, key: str = Form(...), secret: str = Form(...)): pxl = Rothko(key).encode_to_img(secret, scale=True) save_path = pxl.save(TMP) background_tasks.add_task(remove_file, save_path) return FileResponse(save_path)
def test_good_post_image(): response = poke_post_image("encode", example_key, example_secret) # succesfully redirected assert response.status_code == 303 next_url = response.next.url img_id = next_url.split("/")[-1] img_name = img_id + ".png" file_path = os.sep.join((TMP, img_name)) # temporary file was created assert os.path.isfile(file_path) # serve image response_get = client.get(next_url) # type: ignore pyright's drunk assert response_get.status_code == 200 # temporary file should be deleted at this point assert not os.path.isfile(file_path) # check if it the response is valid png # PngImageFile(png) called by Rothko will raise SyntaxError otherwise # By the way check if the image is decoded into original message valid_png_hopefully = read_bytes(response_get.content) decoded = Rothko(example_key).decode_from_img( valid_png_hopefully) # type: ignore assert decoded == example_secret
def ed_helper(original): # ed feels unfortunate because of certain dysfunction for key in KEYS: encoded = Rothko(key).encode(original) assert original == Rothko(key).decode(encoded)
def test_ed_strings(): for text, key in zip(FOREIGN, KEYS): encoded = Rothko(key).encode_to_string(text) assert text == Rothko(key).decode_from_string(encoded)
async def decode_img(key: str = Form(...), file: UploadFile = File(None)): img = read_bytes(await file.read()) result = Rothko(key).decode_from_img(img) return result
def url_based_encode(background_tasks: BackgroundTasks, key, msg): pxl = Rothko(key).encode_to_img(msg, scale=True) save_path = pxl.save(TMP) background_tasks.add_task(remove_file, save_path) return FileResponse(save_path)
def decode_str(key: str = Form(...), secret: str = Form(...)): return Rothko(key).decode_from_string(secret)
def encode_str(key: str = Form(...), secret: str = Form(...)): return Rothko(key).encode_to_string(secret)