Skip to content

Commit

Permalink
feat(server): serving mimetypes
Browse files Browse the repository at this point in the history
Signed-off-by: John Andersen <johnandersen777@protonmail.com>
  • Loading branch information
johnandersen777 committed Dec 3, 2024
1 parent 779e7a7 commit 1094755
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "atprotobin"
version = "0.3.1"
version = "1.0.0"
description = "ATProto based pastebin"
readme = {file = "README.md", content-type = "text/markdown"}
authors = [
Expand All @@ -16,6 +16,7 @@ dependencies = [
"python-magic>=0.4.27",
"python-multipart>=0.0.19",
"pygments>=2.18.0",
"pydantic>=2.10.2",
"markdown2>=2.5.1",
]

Expand Down
24 changes: 20 additions & 4 deletions src/atprotobin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Annotated

import markdown2
from pydantic import BaseModel
from atproto import AsyncClient, models
from fastapi import FastAPI, File, UploadFile, Request, Response
from fastapi.responses import HTMLResponse
Expand Down Expand Up @@ -80,7 +81,7 @@ async def create(file: UploadFile):
None, encode, file_data, file.filename,
)
post = await client.send_image(
text=data_as_image_hash,
text=mimetype,
image=data_as_image,
image_alt=data_as_image_hash,
)
Expand All @@ -91,6 +92,12 @@ async def create(file: UploadFile):
"cid": post.cid,
}

class Blob(BaseModel):
hash_alg: str
hash_value: str
mimetype: str
contents: bytes

async def load_and_decode(post_id: str) -> tuple[str, bytes]:
post = await client.get_post(post_id)
blob = await client.com.atproto.sync.get_blob(
Expand All @@ -99,14 +106,23 @@ async def load_and_decode(post_id: str) -> tuple[str, bytes]:
did=did_plcs[atproto_handle],
),
)
return await asyncio.get_event_loop().run_in_executor(
data_as_image_hash = post.value.embed.images[0].alt
hash_alg = data_as_image_hash.split(":", maxsplit=1)[0]
hash_value = data_as_image_hash.split(":", maxsplit=1)[1]
mimetype, contents = await asyncio.get_event_loop().run_in_executor(
None, decode, blob,
)
return Blob(
hash_alg=hash_alg,
hash_value=hash_value,
mimetype=mimetype,
contents=contents,
)

@app.get("/{post_id}")
async def get(post_id: str):
mimetype, output_bytes = await load_and_decode(post_id)
return Response(content=output_bytes, media_type=mimetype)
blob = await load_and_decode(post_id)
return Response(content=blob.contents, media_type=blob.mimetype)

@app.get("/", response_class=HTMLResponse)
async def root():
Expand Down
5 changes: 4 additions & 1 deletion src/atprotobin/zip_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ def create_zip_of_files(file_contents, file_name: str = None):
zip_buffer = BytesIO()
mimetype = magic.from_buffer(file_contents, mime=True)
if mimetype == "text/plain" and file_name is not None:
mimetype = mimetypes.guess_file_type(file_name)
if hasattr(mimetypes, "guess_file_type"):
mimetype = mimetypes.guess_file_type(file_name)
else:
mimetype, _encoding = mimetypes.guess_type(file_name)
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zipf:
zipf.writestr(mimetype, file_contents)
zip_buffer.seek(0)
Expand Down

0 comments on commit 1094755

Please sign in to comment.