Skip to content

Commit ea8b175

Browse files
committed
cleanup around the house
1 parent 109831d commit ea8b175

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

mystbin/backend/models/responses.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class File(BaseModel):
3030
tab_id: int | None
3131
image: str | None
3232

33-
3433
class Config:
3534
schema_extra = {
3635
"example": {
@@ -39,7 +38,7 @@ class Config:
3938
"loc": 2,
4039
"charcount": 49,
4140
"tab_id": 0,
42-
"url": "..."
41+
"url": "...",
4342
}
4443
}
4544

mystbin/backend/mystbin_models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import TYPE_CHECKING, List, Optional, TypedDict
44

5+
import aiohttp
56
from fastapi import Request
67
from starlette.datastructures import State
78

@@ -37,6 +38,7 @@ class MystbinState(State):
3738

3839
db: Database
3940
user: Optional[_StateUser]
41+
session: aiohttp.ClientSession
4042

4143

4244
class MystbinRequest(Request):

mystbin/backend/routers/pastes.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"""
1919
from __future__ import annotations
2020

21-
import asyncio
2221
import datetime
2322
import json
2423
import pathlib
@@ -27,7 +26,7 @@
2726
from typing import Dict, List, Optional, Union
2827

2928
from asyncpg import Record
30-
from fastapi import APIRouter, Response, UploadFile, File
29+
from fastapi import APIRouter, File, Response, UploadFile
3130
from fastapi.responses import UJSONResponse
3231
from models import errors, payloads, responses
3332

@@ -177,43 +176,45 @@ async def put_pastes(
177176
paste = recursive_hook(paste.dict())
178177
return UJSONResponse(paste, status_code=201)
179178

179+
180180
@router.put(
181181
"/images/upload/{paste_id}",
182182
tags=["pastes"],
183183
responses={
184184
201: {"model": responses.PastePostResponse},
185185
401: {"model": errors.Unauthorized},
186-
404: {"model": errors.NotFound}
186+
404: {"model": errors.NotFound},
187187
},
188188
include_in_schema=False,
189189
)
190190
@limit("postpastes")
191-
async def get_image_upload_link(request: MystbinRequest, paste_id: str, password: Optional[str] = None, images: List[UploadFile] = File(...)):
192-
""" user = request.state.user
191+
async def get_image_upload_link(
192+
request: MystbinRequest, paste_id: str, password: Optional[str] = None, images: List[UploadFile] = File(...)
193+
):
194+
"""user = request.state.user
193195
if not user:
194196
return UJSONResponse({"error": "Unauthorized", "notice": "You must be signed in to use this route"}, status_code=401)"""
195197

196198
paste = await request.app.state.db.get_paste(paste_id, password)
197199
if paste is None:
198200
return UJSONResponse({"error": "Not Found"}, status_code=404)
199201

200-
headers = {
201-
"Content-Type": "application/octet-stream",
202-
"AccessKey": f"{__config['bunny_cdn']['token']}"
203-
}
202+
headers = {"Content-Type": "application/octet-stream", "AccessKey": f"{__config['bunny_cdn']['token']}"}
204203

205204
for image in images:
206205
i = image.filename[0]
207-
await request.app.state.db.update_paste_with_files(paste_id=paste_id, tab_id=i, url=f'https://mystbin.b-cdn.net/images/{image.filename}')
206+
await request.app.state.db.update_paste_with_files(
207+
paste_id=paste_id, tab_id=i, url=f"https://mystbin.b-cdn.net/images/{image.filename}"
208+
)
208209

209210
URL = f'https://storage.bunnycdn.com/{__config["bunny_cdn"]["hostname"]}/images/{image.filename}'
210211
data = await image.read()
211212

212-
async with request.app.state.client.put(URL, headers=headers, data=data) as resp:
213-
pass
213+
await request.app.state.client.put(URL, headers=headers, data=data)
214214

215215
return Response(status_code=201)
216216

217+
217218
desc = f"""Get a paste by ID.
218219
219220
This endpoint falls under the `getpaste` ratelimit bucket.

mystbin/backend/utils/db.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ async def get_paste(self, paste_id: str, password: Optional[str] = None) -> Opti
185185

186186
images = await self.get_images(paste_id=paste_id)
187187

188-
for index, file in enumerate(resp['files']):
188+
for index, file in enumerate(resp["files"]):
189189
try:
190-
file['image'] = images[index]['url']
191-
file['tab_id'] = images[index]['tab_id']
190+
file["image"] = images[index]["url"]
191+
file["tab_id"] = images[index]["tab_id"]
192192
except IndexError:
193193
pass
194194

@@ -311,7 +311,7 @@ async def get_images(self, *, paste_id: str):
311311

312312
query = """SELECT * FROM images WHERE parent_id = $1"""
313313
async with self.pool.acquire() as conn:
314-
return [dict(x) for x in await self._do_query(query, paste_id)]
314+
return [dict(x) for x in await self._do_query(query, paste_id)]
315315

316316
@wrapped_hook_callback
317317
async def edit_paste(
@@ -995,8 +995,4 @@ async def put_log(self, request: Request, response: Response) -> None:
995995
async def put_paste_images(self, parent_id, tab_id) -> None:
996996
query = """INSERT INTO images VALUES($1, $2)"""
997997

998-
await self._do_query(
999-
query,
1000-
parent_id,
1001-
tab_id
1002-
)
998+
await self._do_query(query, parent_id, tab_id)

0 commit comments

Comments
 (0)