mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 09:39:22 +00:00
feat(storage): add archive storage support for assets (#514)
* feat(storage): add archive storage support for assets * fix(webui): show archive icon in front of file name in breadcrumb bar * fix(webui): display archive icon in breadcrumbs when restoring * refactor(transcode): simplify restore asset workflow to single while loop * docs(configuration): add archive storage documentation and readme entries * fix(webui): show archive icon in front of version names and prevent version card overflow * fix(webui): fix overflow in manage versions dialog scroll area * fix(webui): prevent manage versions dialog overflow on long file names * docs(configuration): fix archive storage environment variables and defaults * fix(storage): address code review findings for archive storage
This commit is contained in:
@@ -28,6 +28,7 @@ describe('authn api', () => {
|
||||
expect(await res.json()).toEqual({
|
||||
initialized: true,
|
||||
demoMode: false,
|
||||
archiveStorageEnabled: false,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Hono } from 'hono'
|
||||
import { teamService } from '@shumai/core/src/team/team'
|
||||
import { userService } from '@shumai/core/src/user/user'
|
||||
import { auth } from '@shumai/core/src/auth/auth'
|
||||
import { isArchiveStorageEnabled } from '@shumai/core/src/s3/archive-storage'
|
||||
import { z } from 'zod'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { ulid } from 'ulid'
|
||||
@@ -19,6 +20,7 @@ const route = app
|
||||
return c.json({
|
||||
initialized: info.initialized,
|
||||
demoMode: info.demoMode,
|
||||
archiveStorageEnabled: isArchiveStorageEnabled(),
|
||||
})
|
||||
})
|
||||
.get('/me', async (c) => {
|
||||
|
||||
@@ -11,8 +11,10 @@ import { authMiddleware } from './middleware/auth'
|
||||
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import {
|
||||
archiveAssetsOp,
|
||||
renameAssetOp,
|
||||
resolveCommentOp,
|
||||
restoreArchivedAssetsOp,
|
||||
runOperation,
|
||||
updateAssetMetadataOp,
|
||||
} from '@shumai/core/src/operations'
|
||||
@@ -817,10 +819,13 @@ describe('file api', () => {
|
||||
.spyOn(assetService, 'getProjectIds')
|
||||
.mockResolvedValue(['project-id-1'])
|
||||
|
||||
const mockGetDownloadLinks = vi.spyOn(assetService, 'getDownloadLinks').mockResolvedValue([
|
||||
{ id: 'file1', name: 'file1.txt', url: 'http://link1' },
|
||||
{ id: 'file2', name: 'file2.txt', url: 'http://link2' },
|
||||
])
|
||||
const mockGetDownloadLinks = vi.spyOn(assetService, 'getDownloadLinks').mockResolvedValue({
|
||||
files: [
|
||||
{ id: 'file1', name: 'file1.txt', url: 'http://link1' },
|
||||
{ id: 'file2', name: 'file2.txt', url: 'http://link2' },
|
||||
],
|
||||
skippedArchivedCount: 0,
|
||||
})
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/download-links', {
|
||||
@@ -908,4 +913,64 @@ describe('file api', () => {
|
||||
mockVariant.mockRestore()
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /files/archive and POST /files/restore-archive routes', () => {
|
||||
const validUlid = '01ARZ3NDEKTSV4RRFFQ69G5FAV'
|
||||
|
||||
it('archives assets via archiveAssetsOp', async () => {
|
||||
vi.mocked(runOperation).mockResolvedValueOnce({
|
||||
archivedAssetIds: [validUlid],
|
||||
taskIds: ['task-arch-1'],
|
||||
skippedCount: 0,
|
||||
})
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/archive', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids: [validUlid] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json).toEqual({
|
||||
archivedAssetIds: [validUlid],
|
||||
taskIds: ['task-arch-1'],
|
||||
skippedCount: 0,
|
||||
})
|
||||
expect(runOperation).toHaveBeenCalledWith(
|
||||
archiveAssetsOp,
|
||||
{ userId: 'user1', via: 'api' },
|
||||
{ assetIds: [validUlid] },
|
||||
)
|
||||
})
|
||||
|
||||
it('restores archived assets via restoreArchivedAssetsOp', async () => {
|
||||
vi.mocked(runOperation).mockResolvedValueOnce({
|
||||
restoredAssetIds: [validUlid],
|
||||
taskIds: ['task-rest-1'],
|
||||
skippedCount: 0,
|
||||
})
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/restore-archive', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids: [validUlid] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json).toEqual({
|
||||
restoredAssetIds: [validUlid],
|
||||
taskIds: ['task-rest-1'],
|
||||
skippedCount: 0,
|
||||
})
|
||||
expect(runOperation).toHaveBeenCalledWith(
|
||||
restoreArchivedAssetsOp,
|
||||
{ userId: 'user1', via: 'api' },
|
||||
{ assetIds: [validUlid] },
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,16 +6,20 @@ import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import {
|
||||
archiveAssetsOp,
|
||||
renameAssetOp,
|
||||
resolveCommentOp,
|
||||
restoreArchivedAssetsOp,
|
||||
runOperation,
|
||||
updateAssetMetadataOp,
|
||||
} from '@shumai/core/src/operations'
|
||||
import {
|
||||
archiveAssetsRequestSchema,
|
||||
createCommentRequestSchema,
|
||||
deleteFilesRequestSchema,
|
||||
batchGetFilesRequestSchema,
|
||||
paginationParamsSchema,
|
||||
restoreArchivedAssetsRequestSchema,
|
||||
restoreFilesRequestSchema,
|
||||
updateAssetMetadataRequestSchema,
|
||||
updateAssetOrderRequestSchema,
|
||||
@@ -343,6 +347,28 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
await assetService.restoreAssets(req.ids)
|
||||
return c.body(null, 204)
|
||||
})
|
||||
.post('/files/archive', zValidator('json', archiveAssetsRequestSchema), async (c) => {
|
||||
const req = c.req.valid('json')
|
||||
const result = await runOperation(
|
||||
archiveAssetsOp,
|
||||
{ userId: c.get('user').id, via: 'api' },
|
||||
{ assetIds: req.ids },
|
||||
)
|
||||
return c.json(result)
|
||||
})
|
||||
.post(
|
||||
'/files/restore-archive',
|
||||
zValidator('json', restoreArchivedAssetsRequestSchema),
|
||||
async (c) => {
|
||||
const req = c.req.valid('json')
|
||||
const result = await runOperation(
|
||||
restoreArchivedAssetsOp,
|
||||
{ userId: c.get('user').id, via: 'api' },
|
||||
{ assetIds: req.ids },
|
||||
)
|
||||
return c.json(result)
|
||||
},
|
||||
)
|
||||
.post('/teams/:teamId/files', zValidator('form', uploadFileRequestSchema), async (c) => {
|
||||
const teamId = c.req.param('teamId')
|
||||
const user = c.get('user')
|
||||
@@ -424,8 +450,11 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
})
|
||||
|
||||
// 4. Generate download links
|
||||
const files = await assetService.getDownloadLinks(req.ids)
|
||||
return c.json({ files })
|
||||
const { files, skippedArchivedCount } = await assetService.getDownloadLinks(req.ids)
|
||||
return c.json({
|
||||
files,
|
||||
...(skippedArchivedCount > 0 ? { skippedArchivedCount } : {}),
|
||||
})
|
||||
})
|
||||
.post(
|
||||
'/files/download-url',
|
||||
|
||||
@@ -53,6 +53,7 @@ describe('MCP server', () => {
|
||||
const { tools } = await client.listTools()
|
||||
|
||||
expect(tools.map((t) => t.name).sort()).toEqual([
|
||||
'archive_assets',
|
||||
'complete_upload',
|
||||
'copy_assets',
|
||||
'create_comment',
|
||||
@@ -74,6 +75,7 @@ describe('MCP server', () => {
|
||||
'remove_version',
|
||||
'rename_asset',
|
||||
'resolve_comment',
|
||||
'restore_archived_assets',
|
||||
'search_assets',
|
||||
'update_asset_metadata',
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user