mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +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:
@@ -4,12 +4,16 @@ import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { uploadService } from './upload'
|
||||
import { gotenbergService } from '@shumai/core/src/gotenberg/gotenberg'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { AssetStatus, AssetType, WorkflowTaskType } from '@shumai/db'
|
||||
import { AssetStatus, AssetType, TaskStatus, WorkflowTaskType } from '@shumai/db'
|
||||
|
||||
vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||
getStorageBackend: vi.fn().mockReturnValue('s3'),
|
||||
s3Service: {
|
||||
presign: vi.fn().mockResolvedValue('http://presigned-url.com'),
|
||||
getObjectSize: vi.fn().mockResolvedValue(100),
|
||||
presignMultipart: vi.fn().mockResolvedValue({ url: 'http://signed-multipart-url.com' }),
|
||||
abortMultipartUpload: vi.fn().mockResolvedValue(undefined),
|
||||
deleteObject: vi.fn().mockResolvedValue(1),
|
||||
},
|
||||
}))
|
||||
|
||||
@@ -615,4 +619,293 @@ describe('UploadService', () => {
|
||||
const song2 = await prisma.asset.findFirst({ where: { name: 'other_song.WMA' } })
|
||||
expect(song2?.mediaType).toBe('audio/x-ms-asf')
|
||||
})
|
||||
|
||||
describe('signS3Upload', () => {
|
||||
it('should sign multipart request and store uploadId on asset', async () => {
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/video.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
storageKeyId: storageKey.id,
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const res = await uploadService.signS3Upload(teamId, userId, {
|
||||
key: 'files/test/video.mp4',
|
||||
method: 'PUT',
|
||||
uploadId: 'upload-id-xyz',
|
||||
partNumber: 1,
|
||||
fileId: asset.id,
|
||||
})
|
||||
|
||||
expect(res.url).toBe('http://signed-multipart-url.com')
|
||||
|
||||
const updatedAsset = await prisma.asset.findUnique({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
expect(updatedAsset?.uploadId).toBe('upload-id-xyz')
|
||||
})
|
||||
|
||||
it('should reject signing if requested key does not match asset storage key', async () => {
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/video.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
storageKeyId: storageKey.id,
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
uploadService.signS3Upload(teamId, userId, {
|
||||
key: 'files/unauthorized/other.mp4',
|
||||
method: 'PUT',
|
||||
fileId: asset.id,
|
||||
}),
|
||||
).rejects.toThrow('Requested key does not match asset storage key')
|
||||
})
|
||||
|
||||
it('should reject signing if asset is not in uploading status', async () => {
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/video.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
storageKeyId: storageKey.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
uploadService.signS3Upload(teamId, userId, {
|
||||
key: 'files/test/video.mp4',
|
||||
method: 'PUT',
|
||||
fileId: asset.id,
|
||||
}),
|
||||
).rejects.toThrow('Asset is not currently uploading')
|
||||
})
|
||||
|
||||
it('should reject signing if asset belongs to another team', async () => {
|
||||
const otherTeam = await prisma.team.create({ data: { name: 'other-team' } })
|
||||
const otherProject = await prisma.project.create({
|
||||
data: { name: 'other-project', teamId: otherTeam.id },
|
||||
})
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/other.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'other.mp4',
|
||||
type: AssetType.file,
|
||||
projectId: otherProject.id,
|
||||
storageKeyId: storageKey.id,
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
uploadService.signS3Upload(teamId, userId, {
|
||||
key: 'files/test/other.mp4',
|
||||
method: 'PUT',
|
||||
fileId: asset.id,
|
||||
}),
|
||||
).rejects.toThrow('Asset does not belong to team')
|
||||
})
|
||||
})
|
||||
|
||||
describe('abortUpload', () => {
|
||||
it('should abort multipart upload, delete asset, and fail task', async () => {
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'task-1',
|
||||
total: 1,
|
||||
status: TaskStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/abort.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'abort.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
storageKeyId: storageKey.id,
|
||||
taskId: task.id,
|
||||
uploadId: 'upload-abc',
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const res = await uploadService.abortUpload(teamId, userId, task.id, {
|
||||
fileId: asset.id,
|
||||
uploadId: 'upload-abc',
|
||||
key: 'files/test/abort.mp4',
|
||||
})
|
||||
|
||||
expect(res.success).toBe(true)
|
||||
expect(s3Service.abortMultipartUpload).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
'files/test/abort.mp4',
|
||||
'upload-abc',
|
||||
)
|
||||
|
||||
const deletedAsset = await prisma.asset.findUnique({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
expect(deletedAsset).toBeNull()
|
||||
|
||||
const updatedTask = await prisma.task.findUnique({
|
||||
where: { id: task.id },
|
||||
})
|
||||
expect(updatedTask?.status).toBe(TaskStatus.failed)
|
||||
})
|
||||
|
||||
it('should reject aborting if asset taskId does not match', async () => {
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'task-1',
|
||||
total: 1,
|
||||
status: TaskStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const otherTask = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'other-task',
|
||||
total: 1,
|
||||
status: TaskStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/abort-mismatch.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'abort-mismatch.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
storageKeyId: storageKey.id,
|
||||
taskId: otherTask.id,
|
||||
uploadId: 'upload-abc',
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
uploadService.abortUpload(teamId, userId, task.id, {
|
||||
fileId: asset.id,
|
||||
uploadId: 'upload-abc',
|
||||
key: 'files/test/abort-mismatch.mp4',
|
||||
}),
|
||||
).rejects.toThrow('Asset does not belong to specified task')
|
||||
})
|
||||
|
||||
it('should delete object from storage when aborting single/local upload without uploadId', async () => {
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'task-2',
|
||||
total: 1,
|
||||
status: TaskStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/single.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'single.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
storageKeyId: storageKey.id,
|
||||
taskId: task.id,
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const res = await uploadService.abortUpload(teamId, userId, task.id, {
|
||||
fileId: asset.id,
|
||||
key: 'files/test/single.mp4',
|
||||
})
|
||||
|
||||
expect(res.success).toBe(true)
|
||||
expect(s3Service.deleteObject).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
'files/test/single.mp4',
|
||||
)
|
||||
|
||||
const deletedAsset = await prisma.asset.findUnique({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
expect(deletedAsset).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('confirmFileUpload with errorMessage', () => {
|
||||
it('should abort multipart upload in S3 and delete asset on failure', async () => {
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'task-err',
|
||||
total: 1,
|
||||
status: TaskStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/fail.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fail.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
storageKeyId: storageKey.id,
|
||||
taskId: task.id,
|
||||
uploadId: 'upload-fail-id',
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
await uploadService.confirmFileUpload(userId, task.id, {
|
||||
fileId: asset.id,
|
||||
errorMessage: 'Network failed',
|
||||
})
|
||||
|
||||
expect(s3Service.abortMultipartUpload).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
'files/test/fail.mp4',
|
||||
'upload-fail-id',
|
||||
)
|
||||
|
||||
const deletedAsset = await prisma.asset.findUnique({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
expect(deletedAsset).toBeNull()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user