mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-14 22:40:59 +00:00
feat: add api for uploading files
This commit is contained in:
@@ -2,6 +2,10 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import quart
|
import quart
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
import uuid
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import quart.datastructures
|
||||||
|
|
||||||
from .. import group
|
from .. import group
|
||||||
|
|
||||||
@@ -20,3 +24,22 @@ class FilesRouterGroup(group.RouterGroup):
|
|||||||
mime_type = 'image/jpeg'
|
mime_type = 'image/jpeg'
|
||||||
|
|
||||||
return quart.Response(image_bytes, mimetype=mime_type)
|
return quart.Response(image_bytes, mimetype=mime_type)
|
||||||
|
|
||||||
|
@self.route('/documents', methods=['POST'], auth_type=group.AuthType.USER_TOKEN)
|
||||||
|
async def _() -> quart.Response:
|
||||||
|
request = quart.request
|
||||||
|
# get file bytes from 'file'
|
||||||
|
file = (await request.files)['file']
|
||||||
|
assert isinstance(file, quart.datastructures.FileStorage)
|
||||||
|
|
||||||
|
file_bytes = await asyncio.to_thread(file.stream.read)
|
||||||
|
extension = file.filename.split('.')[-1]
|
||||||
|
|
||||||
|
file_key = str(uuid.uuid4()) + '.' + extension
|
||||||
|
# save file to storage
|
||||||
|
await self.ap.storage_mgr.storage_provider.save(file_key, file_bytes)
|
||||||
|
return self.success(
|
||||||
|
data={
|
||||||
|
'file_id': file_key,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user