Skip to content

Commit

Permalink
uploadBlob: handle duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBuchanan314 committed Nov 12, 2024
1 parent df4bc4b commit 9a6d548
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/millipds/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import hashlib

import apsw
import aiohttp
from aiohttp import web
import jwt
Expand Down Expand Up @@ -331,7 +332,15 @@ async def repo_upload_blob(request: web.Request):
break
digest = hasher.digest()
cid = cbrrr.CID(cbrrr.CID.CIDV1_RAW_SHA256_32_PFX + digest)
db.con.execute("UPDATE blob SET cid=? WHERE id=?", (bytes(cid), blob_id)) # TODO: check for duplicate blobs! (the unique constraint should get violated, we just need to clean up)
try:
db.con.execute("UPDATE blob SET cid=? WHERE id=?", (bytes(cid), blob_id))
except apsw.ConstraintError:
# this means the blob already existed, we need to clean up the duplicate
# TODO: if we were using transactions this could happen automagically
db.con.execute("DELETE FROM blob_part WHERE blob=?", (blob_id,))
db.con.execute("DELETE FROM blob WHERE id=?", (blob_id,))
logger.info("uploaded blob already existed, dropping duplicate")

return web.json_response({
"blob": {
"$type": "blob",
Expand Down
7 changes: 4 additions & 3 deletions tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@


blob = os.urandom(0x100000)
r = s.post(PDS + "/xrpc/com.atproto.repo.uploadBlob", data=blob, headers=authn|{"content-type": "blah"})
print(r.json())
assert r.ok
for _ in range(2): # test reupload is nop
r = s.post(PDS + "/xrpc/com.atproto.repo.uploadBlob", data=blob, headers=authn|{"content-type": "blah"})
print(r.json())
assert r.ok


r = s.get(PDS + "/xrpc/com.atproto.sync.getRepo", params={"did": "did:web:alice.test"})
Expand Down

0 comments on commit 9a6d548

Please sign in to comment.