|
4 | 4 | import uuid |
5 | 5 | from datetime import datetime, timedelta, timezone |
6 | 6 |
|
| 7 | +from app.lua import ( |
| 8 | + json_remove_file_by_key, |
| 9 | + json_remove_upload_by_key, |
| 10 | + json_update_uploaded_bytes_by_key, |
| 11 | +) |
7 | 12 | from app.schemas.reverse import ( |
8 | 13 | ActiveUpload, |
9 | 14 | AddHostOut, |
@@ -199,21 +204,7 @@ async def remove_file(cls, room_id: str, file_key: str) -> RoomFileEntry | None: |
199 | 204 | """ |
200 | 205 | key = _room_key(room_id) |
201 | 206 | client = cls._client() |
202 | | - script = """ |
203 | | - local raw = redis.call('JSON.GET', KEYS[1], '$.files') |
204 | | - if not raw then return nil end |
205 | | - local outer = cjson.decode(raw) |
206 | | - local files = outer[1] |
207 | | - for i, f in ipairs(files) do |
208 | | - if f['key'] == ARGV[1] then |
209 | | - local removed = cjson.encode(f) |
210 | | - redis.call('JSON.DEL', KEYS[1], '$.files[' .. (i-1) .. ']') |
211 | | - return removed |
212 | | - end |
213 | | - end |
214 | | - return nil |
215 | | - """ |
216 | | - result = await client.eval(script, 1, key, file_key) # type: ignore[misc] |
| 207 | + result = await client.eval(json_remove_file_by_key.code, 1, key, file_key) # type: ignore[misc] |
217 | 208 | if result is None: |
218 | 209 | return None |
219 | 210 |
|
@@ -260,38 +251,14 @@ async def update_active_upload( |
260 | 251 | cls, room_id: str, upload_key: str, uploaded_bytes: int |
261 | 252 | ) -> None: |
262 | 253 | key = _room_key(room_id) |
263 | | - script = """ |
264 | | - local raw = redis.call('JSON.GET', KEYS[1], '$.active_uploads') |
265 | | - if not raw then return nil end |
266 | | - local outer = cjson.decode(raw) |
267 | | - local uploads = outer[1] |
268 | | - for i, u in ipairs(uploads) do |
269 | | - if u['upload_key'] == ARGV[1] then |
270 | | - redis.call('JSON.SET', KEYS[1], '$.active_uploads[' .. (i-1) .. '].uploaded_bytes', ARGV[2]) |
271 | | - return 1 |
272 | | - end |
273 | | - end |
274 | | - return nil |
275 | | - """ |
276 | | - await cls._client().eval(script, 1, key, upload_key, uploaded_bytes) # type:ignore[misc] |
| 254 | + await cls._client().eval( |
| 255 | + json_update_uploaded_bytes_by_key.code, 1, key, upload_key, uploaded_bytes |
| 256 | + ) # type:ignore[misc] |
277 | 257 |
|
278 | 258 | @classmethod |
279 | 259 | async def remove_active_upload(cls, room_id: str, upload_key: str) -> None: |
280 | 260 | key = _room_key(room_id) |
281 | | - script = """ |
282 | | - local raw = redis.call('JSON.GET', KEYS[1], '$.active_uploads') |
283 | | - if not raw then return nil end |
284 | | - local outer = cjson.decode(raw) |
285 | | - local uploads = outer[1] |
286 | | - for i, u in ipairs(uploads) do |
287 | | - if u['upload_key'] == ARGV[1] then |
288 | | - redis.call('JSON.DEL', KEYS[1], '$.active_uploads[' .. (i-1) .. ']') |
289 | | - return 1 |
290 | | - end |
291 | | - end |
292 | | - return nil |
293 | | - """ |
294 | | - await cls._client().eval(script, 1, key, upload_key) # type:ignore[misc] |
| 261 | + await cls._client().eval(json_remove_upload_by_key.code, 1, key, upload_key) # type:ignore[misc] |
295 | 262 |
|
296 | 263 | @classmethod |
297 | 264 | async def client_online(cls, room_id: str, client_id: str, is_host: bool) -> None: |
|
0 commit comments