#!/usr/bin/env -S uv run --script # /// script # requires-python = ">=3.12" # dependencies = [] # /// import base64 import json import os import sqlite3 import sys from urllib.error import HTTPError from urllib.parse import urlencode from urllib.request import Request, urlopen base, db = sys.argv[1:] token = os.environ['ZDS_SMOKE_TOKEN'] def call(method, body=None, mime='application/json', status=200, params=None): headers = {'authorization': 'Bearer ' + token} if mime is not None: headers['content-type'] = mime if isinstance(body, dict): body = json.dumps(body).encode() url = base + '/xrpc/' + method + ('?' + urlencode(params) if params else '') try: with urlopen(Request(url, data=body, headers=headers), timeout=30) as response: code, data, content_type = response.status, response.read(), response.headers.get('content-type', '') except HTTPError as exc: code, data, content_type = exc.code, exc.read(), exc.headers.get('content-type', '') assert code == status, (method, code, data) return json.loads(data) if 'json' in content_type else data did = call('com.atproto.server.getSession')['did'] png = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAFgQIAH3yN3wAAAABJRU5ErkJggg==') for hint in ('image/png', '*/*', 'application/octet-stream', 'text/plain'): blob = call('com.atproto.repo.uploadBlob', png, hint)['blob'] assert blob['mimeType'] == 'image/png', blob with sqlite3.connect(db) as connection: assert connection.execute('select mime_type from blobs where did = ? and cid = ?', (did, blob['ref']['$link'])).fetchone()[0] == 'image/png' for data, expected in ((b'fLaC' + bytes(100), 'audio/flac'), (b'\0\0\0\x18ftypisom' + bytes(100), 'video/mp4'), (b'RIFF\0\0\0\0WEBP' + bytes(100), 'image/webp')): assert call('com.atproto.repo.uploadBlob', data, 'application/octet-stream')['blob']['mimeType'] == expected assert call('com.atproto.repo.uploadBlob', b'plain text', 'text/plain')['blob']['mimeType'] == 'text/plain' assert call('com.atproto.repo.uploadBlob', b'', 'application/octet-stream')['blob']['size'] == 0 payload = png + bytes(128 * 1024) blob = call('com.atproto.repo.uploadBlob', payload, '*/*')['blob'] assert blob['size'] == len(payload) call('com.atproto.repo.createRecord', {'repo': did, 'collection': 'test.blob.record', 'record': {'$type': 'test.blob.record', 'file': blob}}) assert call('com.atproto.sync.getBlob', params={'did': did, 'cid': blob['ref']['$link']}) == payload with sqlite3.connect(db) as connection: connection.execute("insert into oauth_tokens (family_id, access_token, refresh_token, did, client_id, scope, expires_at, access_expires_at, refresh_expires_at) values (?, ?, 'mime-refresh', ?, 'https://mime.test', 'atproto blob:image/*', unixepoch()+600, unixepoch()+600, unixepoch()+600)", (token, token, did)) try: assert call('com.atproto.repo.uploadBlob', png, 'text/plain')['blob']['mimeType'] == 'image/png' call('com.atproto.repo.uploadBlob', b'fLaC' + bytes(101), 'image/png', status=403) call('com.atproto.repo.uploadBlob', b'unknown', 'text/plain', status=403) finally: with sqlite3.connect(db) as connection: connection.execute('delete from oauth_tokens where access_token = ?', (token,)) print('blob MIME detection, streaming round-trip, storage and OAuth scope checks passed')