api: Add qr-std-upload api

This commit is contained in:
Fam Zheng 2025-05-14 22:10:38 +01:00
parent 4d211b8305
commit 3133e81650
3 changed files with 40 additions and 0 deletions

View File

@ -161,11 +161,14 @@ if ENV == "prod":
FEATURES_BUCKET = "emblem-features-prod"
ARCHIVE_BUCKET = "emblem-archive-prod"
FRAMES_BUCKET = "emblem-frames-prod"
OSS_QRS_BUCKET = "emblem-qrs"
else:
OSS = aliyun_dev_key
FEATURES_BUCKET = "emblem-features-dev-1"
ARCHIVE_BUCKET = "emblem-oss-archive-dev-1"
FRAMES_BUCKET = "emblem-frames-dev"
OSS_QRS_BUCKET = ARCHIVE_BUCKET
IPINFO_TOKEN = '537dea9ec5c99a'

View File

@ -1609,6 +1609,35 @@ class EstorFeatureRoi(BaseView):
"saved": 1,
})
class QrStdUploadView(BaseView):
name = 'qr-std-upload'
auth_check = None
def post(self, request):
files = request.FILES
for f in files.values():
try:
QrStdUploadView.save_qr(f.name, f.read())
except Exception as e:
return http400(f'{f.name}: {str(e)}')
return JsonResponse({
'ok': True,
'nfiles': len(files),
})
def save_qr(fname, body):
basename = os.path.basename(fname)
name, ext = os.path.splitext(basename)
if ext != ".jpg":
raise Exception("invalid file extension")
if len(body) > 1024 * 1024 * 10:
raise Exception("file too large")
if len(name) < 2:
raise Exception("invalid file name")
pref = name[:2]
key = f"v5/{pref}/{fname}"
r = oss_put(key, body, bucket=settings.OSS_QRS_BUCKET)
class EstorArchive(BaseView):
name = 'estor-archive'
auth_check = None

8
api/tests/test_qr_std.py Normal file
View File

@ -0,0 +1,8 @@
from django.test import TestCase
from products.views import QrStdUploadView
class QrStdTestCase(TestCase):
def test_qr_std_upload(self):
fname = '99999999.jpg'
body = b'1234'
r = QrStdUploadView.save_qr(fname, body)