mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 16:09:02 +00:00
feat(upload): S3 multipart upload with offline resume and wake lock (#391)
* feat(upload): implement S3 multipart upload with offline resume and wake lock * fix(upload): harden S3 sign and abort authorization, await confirmation, and clean up failed multipart uploads * fix(webui): resume multipart upload on network recovery and log online event * fix(webui): remove offline and online toast notifications
This commit is contained in:
@@ -139,4 +139,85 @@ describe('Upload API', () => {
|
||||
itemId: 'file1',
|
||||
})
|
||||
})
|
||||
|
||||
it('POST /teams/:teamId/upload/sign', async () => {
|
||||
vi.spyOn(uploadService, 'signS3Upload').mockResolvedValue({
|
||||
url: 'http://signed-url.com',
|
||||
})
|
||||
|
||||
const res = await app.request('/teams/team1/upload/sign', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: 'Bearer test' },
|
||||
body: JSON.stringify({
|
||||
key: 'files/test.mp4',
|
||||
method: 'PUT',
|
||||
fileId: 'file1',
|
||||
uploadId: 'up1',
|
||||
partNumber: 1,
|
||||
}),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json).toEqual({ url: 'http://signed-url.com' })
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: ResourceType.Asset,
|
||||
id: 'file1',
|
||||
permission: Permission.Edit,
|
||||
}),
|
||||
)
|
||||
expect(uploadService.signS3Upload).toHaveBeenCalledWith('team1', 'user1', {
|
||||
key: 'files/test.mp4',
|
||||
method: 'PUT',
|
||||
fileId: 'file1',
|
||||
uploadId: 'up1',
|
||||
partNumber: 1,
|
||||
})
|
||||
})
|
||||
|
||||
it('POST /teams/:teamId/upload/sign fails validation when fileId is missing', async () => {
|
||||
const res = await app.request('/teams/team1/upload/sign', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: 'Bearer test' },
|
||||
body: JSON.stringify({
|
||||
key: 'files/test.mp4',
|
||||
method: 'PUT',
|
||||
}),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
})
|
||||
|
||||
it('POST /teams/:teamId/upload/tasks/:taskId/abort', async () => {
|
||||
vi.spyOn(uploadService, 'abortUpload').mockResolvedValue({
|
||||
success: true,
|
||||
})
|
||||
|
||||
const res = await app.request('/teams/team1/upload/tasks/task1/abort', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: 'Bearer test' },
|
||||
body: JSON.stringify({
|
||||
fileId: 'file1',
|
||||
uploadId: 'up1',
|
||||
key: 'files/test.mp4',
|
||||
}),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json).toEqual({ success: true })
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: ResourceType.Asset,
|
||||
id: 'file1',
|
||||
permission: Permission.Edit,
|
||||
}),
|
||||
)
|
||||
expect(uploadService.abortUpload).toHaveBeenCalledWith('team1', 'user1', 'task1', {
|
||||
fileId: 'file1',
|
||||
uploadId: 'up1',
|
||||
key: 'files/test.mp4',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -2,9 +2,11 @@ import { Hono } from 'hono'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { uploadService } from '@shumai/core/src/upload/upload'
|
||||
import {
|
||||
abortUploadRequestSchema,
|
||||
confirmFileUploadRequestSchema,
|
||||
createUploadTaskRequestSchema,
|
||||
localUploadQuerySchema,
|
||||
s3SignRequestSchema,
|
||||
} from '@shumai/dtos'
|
||||
import { paginationParamsSchema, AuditAction } from '@shumai/dtos'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
@@ -124,5 +126,40 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
return c.json({ success: true })
|
||||
},
|
||||
)
|
||||
.post('/teams/:teamId/upload/sign', zValidator('json', s3SignRequestSchema), async (c) => {
|
||||
const teamId = c.req.param('teamId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id: req.fileId,
|
||||
})
|
||||
|
||||
const resp = await uploadService.signS3Upload(teamId, user.id, req)
|
||||
return c.json(resp)
|
||||
})
|
||||
.post(
|
||||
'/teams/:teamId/upload/tasks/:taskId/abort',
|
||||
zValidator('json', abortUploadRequestSchema),
|
||||
async (c) => {
|
||||
const teamId = c.req.param('teamId')
|
||||
const taskId = c.req.param('taskId')
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Asset,
|
||||
id: req.fileId,
|
||||
})
|
||||
|
||||
const resp = await uploadService.abortUpload(teamId, user.id, taskId, req)
|
||||
return c.json(resp)
|
||||
},
|
||||
)
|
||||
|
||||
export default route
|
||||
|
||||
Reference in New Issue
Block a user