mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 19:49:42 +00:00
refactor(file-browser): address code review feedback for copy submenu
This commit is contained in:
@@ -4,7 +4,8 @@ import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
|
||||
import { AssetType, AssetStatus, Prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { s3Service, getStorageBackend } from '@shumai/core/src/s3/s3'
|
||||
import * as archiveStorageModule from '@shumai/core/src/s3/archive-storage'
|
||||
import { uploadService } from '@shumai/core/src/upload/upload'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { AssetService } from './asset'
|
||||
@@ -20,6 +21,7 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||
deleteObject: vi.fn().mockResolvedValue(1),
|
||||
deletePrefix: vi.fn().mockResolvedValue(1),
|
||||
copyObject: vi.fn().mockResolvedValue(undefined),
|
||||
getStoragePath: vi.fn((bucket: string, key: string) => `/mock/data/${bucket}/${key}`),
|
||||
},
|
||||
getStorageBackend: vi.fn().mockReturnValue('local'),
|
||||
}))
|
||||
@@ -3179,6 +3181,144 @@ describe('AssetService — natural sort by name', () => {
|
||||
expect(stackPath.id).toBe(latestChild.id)
|
||||
expect(stackPath.path).toContain('files/ulid3/stack_v2.png')
|
||||
})
|
||||
|
||||
it('returns S3 path when main storage backend is s3', async () => {
|
||||
vi.mocked(getStorageBackend).mockReturnValueOnce('s3')
|
||||
vi.mocked(s3Service.getStoragePath).mockReturnValueOnce('s3://shumai/files/test/s3-file.mp4')
|
||||
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'P-S3', team: { create: { name: 'T-S3' } } },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'U-S3', email: `s3-${Date.now()}@example.com` },
|
||||
})
|
||||
const file = await prisma.asset.create({
|
||||
data: {
|
||||
name: 's3-file.mp4',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: project.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.processed,
|
||||
storageKey: { create: { key: 'files/test/s3-file.mp4' } },
|
||||
},
|
||||
})
|
||||
|
||||
const res = await assetService.getStoragePaths([file.id])
|
||||
expect(res.paths).toEqual([
|
||||
{
|
||||
id: file.id,
|
||||
name: 's3-file.mp4',
|
||||
path: 's3://shumai/files/test/s3-file.mp4',
|
||||
backend: 's3',
|
||||
isArchived: false,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('returns archive S3 path when asset is archived and archive backend is s3', async () => {
|
||||
vi.spyOn(archiveStorageModule, 'getArchiveStoragePath').mockReturnValueOnce({
|
||||
path: 's3://my-archive-bucket/files/archived/clip.mp4',
|
||||
backend: 's3',
|
||||
})
|
||||
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'P-Archive-S3', team: { create: { name: 'T-Archive-S3' } } },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'U-Arch', email: `arch-${Date.now()}@example.com` },
|
||||
})
|
||||
const file = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'clip.mp4',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: project.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.archived,
|
||||
storageKey: { create: { key: 'files/archived/clip.mp4', isArchived: true } },
|
||||
},
|
||||
})
|
||||
|
||||
const res = await assetService.getStoragePaths([file.id])
|
||||
expect(res.paths).toEqual([
|
||||
{
|
||||
id: file.id,
|
||||
name: 'clip.mp4',
|
||||
path: 's3://my-archive-bucket/files/archived/clip.mp4',
|
||||
backend: 's3',
|
||||
isArchived: true,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('returns archive local path when asset is archived and archive backend is local', async () => {
|
||||
vi.spyOn(archiveStorageModule, 'getArchiveStoragePath').mockReturnValueOnce({
|
||||
path: '/mnt/archive/files/archived/clip.mp4',
|
||||
backend: 'local',
|
||||
})
|
||||
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'P-Archive-Local', team: { create: { name: 'T-Archive-Local' } } },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'U-ArchLocal', email: `archloc-${Date.now()}@example.com` },
|
||||
})
|
||||
const file = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'clip.mp4',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: project.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.archived,
|
||||
storageKey: { create: { key: 'files/archived/clip.mp4', isArchived: true } },
|
||||
},
|
||||
})
|
||||
|
||||
const res = await assetService.getStoragePaths([file.id])
|
||||
expect(res.paths).toEqual([
|
||||
{
|
||||
id: file.id,
|
||||
name: 'clip.mp4',
|
||||
path: '/mnt/archive/files/archived/clip.mp4',
|
||||
backend: 'local',
|
||||
isArchived: true,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('marks isArchived: true with main storage path when archive storage is disabled', async () => {
|
||||
vi.spyOn(archiveStorageModule, 'getArchiveStoragePath').mockReturnValueOnce(null)
|
||||
vi.mocked(s3Service.getStoragePath).mockReturnValueOnce(
|
||||
'/mock/data/shumai/files/archived/clip.mp4',
|
||||
)
|
||||
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'P-DisabledArchive', team: { create: { name: 'T-DisabledArchive' } } },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'U-DisArch', email: `disarch-${Date.now()}@example.com` },
|
||||
})
|
||||
const file = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'clip.mp4',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: project.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.archived,
|
||||
storageKey: { create: { key: 'files/archived/clip.mp4', isArchived: true } },
|
||||
},
|
||||
})
|
||||
|
||||
const res = await assetService.getStoragePaths([file.id])
|
||||
expect(res.paths).toEqual([
|
||||
{
|
||||
id: file.id,
|
||||
name: 'clip.mp4',
|
||||
path: '/mock/data/shumai/files/archived/clip.mp4',
|
||||
backend: 'local',
|
||||
isArchived: true,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('getProjectIds', () => {
|
||||
|
||||
@@ -49,7 +49,7 @@ import { s3Service, getStorageBackend } from '@shumai/core/src/s3/s3'
|
||||
import {
|
||||
getArchiveStorageService,
|
||||
isArchiveStorageEnabled,
|
||||
getArchiveStorageConfig,
|
||||
getArchiveStoragePath,
|
||||
} from '@shumai/core/src/s3/archive-storage'
|
||||
import { assetArchiveService } from './asset-archive'
|
||||
import {
|
||||
@@ -1251,6 +1251,75 @@ export class AssetService {
|
||||
return currentId
|
||||
}
|
||||
|
||||
async resolveLatestVersionIds(assetIds: string[]): Promise<string[]> {
|
||||
if (assetIds.length === 0) return []
|
||||
|
||||
const initialAssets = await this.prismaClient.asset.findMany({
|
||||
where: { id: { in: assetIds }, isDeleted: false },
|
||||
select: { id: true, type: true, targetId: true },
|
||||
})
|
||||
const assetMap = new Map(initialAssets.map((a) => [a.id, a]))
|
||||
|
||||
// Find symlinks to resolve
|
||||
const symlinks = initialAssets.filter((a) => a.type === AssetType.symlink && a.targetId)
|
||||
const targetIds = Array.from(new Set(symlinks.map((s) => s.targetId!)))
|
||||
|
||||
const symlinkTargetMap = new Map<string, { id: string; type: AssetType }>()
|
||||
if (targetIds.length > 0) {
|
||||
const targets = await this.prismaClient.asset.findMany({
|
||||
where: { id: { in: targetIds }, isDeleted: false },
|
||||
select: { id: true, type: true },
|
||||
})
|
||||
for (const t of targets) {
|
||||
symlinkTargetMap.set(t.id, t)
|
||||
}
|
||||
}
|
||||
|
||||
// Determine current ID and type for each input ID
|
||||
const intermediate = assetIds.map((id) => {
|
||||
const asset = assetMap.get(id)
|
||||
if (!asset) return { originalId: id, currentId: id, type: null }
|
||||
if (asset.type === AssetType.symlink && asset.targetId) {
|
||||
const target = symlinkTargetMap.get(asset.targetId)
|
||||
if (target) {
|
||||
return { originalId: id, currentId: target.id, type: target.type }
|
||||
}
|
||||
}
|
||||
return { originalId: id, currentId: asset.id, type: asset.type }
|
||||
})
|
||||
|
||||
// Find version stacks to resolve to their latest child
|
||||
const versionStackIds = Array.from(
|
||||
new Set(
|
||||
intermediate
|
||||
.filter((item) => item.type === AssetType.version_stack)
|
||||
.map((item) => item.currentId),
|
||||
),
|
||||
)
|
||||
|
||||
const latestChildByParent = new Map<string, string>()
|
||||
if (versionStackIds.length > 0) {
|
||||
const children = await this.prismaClient.asset.findMany({
|
||||
where: { parentId: { in: versionStackIds }, isDeleted: false },
|
||||
select: { id: true, parentId: true, sortIndex: true },
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
})
|
||||
for (const child of children) {
|
||||
if (child.parentId && !latestChildByParent.has(child.parentId)) {
|
||||
latestChildByParent.set(child.parentId, child.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return intermediate.map((item) => {
|
||||
if (item.type === AssetType.version_stack) {
|
||||
const childId = latestChildByParent.get(item.currentId)
|
||||
if (childId) return childId
|
||||
}
|
||||
return item.currentId
|
||||
})
|
||||
}
|
||||
|
||||
async updateAssetName(req: UpdateAssetNameRequest): Promise<AssetInfo> {
|
||||
const existing = await this.prismaClient.asset.findUnique({
|
||||
where: { id: req.id },
|
||||
@@ -2892,8 +2961,7 @@ export class AssetService {
|
||||
async getStoragePaths(ids: string[]): Promise<GetStoragePathsResponse> {
|
||||
if (ids.length === 0) return { paths: [] }
|
||||
|
||||
const targetAssetIds = await Promise.all(ids.map(async (id) => this.resolveLatestVersionId(id)))
|
||||
|
||||
const targetAssetIds = await this.resolveLatestVersionIds(ids)
|
||||
const uniqueTargetIds = Array.from(new Set(targetAssetIds))
|
||||
|
||||
const assets = await this.prismaClient.asset.findMany({
|
||||
@@ -2917,10 +2985,8 @@ export class AssetService {
|
||||
},
|
||||
})
|
||||
|
||||
const archiveConfig = getArchiveStorageConfig()
|
||||
const defaultBackend = getStorageBackend()
|
||||
const mainBucket = process.env.S3_BUCKET || 'shumai'
|
||||
|
||||
const paths: StoragePathItem[] = []
|
||||
|
||||
for (const asset of assets) {
|
||||
@@ -2933,42 +2999,24 @@ export class AssetService {
|
||||
asset.status === AssetStatus.restoring ||
|
||||
sk.isArchived
|
||||
|
||||
if (isArchived && archiveConfig.enabled) {
|
||||
if (archiveConfig.backend === 's3') {
|
||||
paths.push({
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
path: `s3://${archiveConfig.bucket}/${sk.key}`,
|
||||
backend: 's3',
|
||||
isArchived: true,
|
||||
})
|
||||
} else {
|
||||
paths.push({
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
path: path.join(archiveConfig.localRoot, sk.key),
|
||||
backend: 'local',
|
||||
isArchived: true,
|
||||
})
|
||||
}
|
||||
const archivePathInfo = isArchived ? getArchiveStoragePath(sk.key) : null
|
||||
if (archivePathInfo) {
|
||||
paths.push({
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
path: archivePathInfo.path,
|
||||
backend: archivePathInfo.backend,
|
||||
isArchived: true,
|
||||
})
|
||||
} else {
|
||||
if (defaultBackend === 's3') {
|
||||
paths.push({
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
path: `s3://${mainBucket}/${sk.key}`,
|
||||
backend: 's3',
|
||||
isArchived: false,
|
||||
})
|
||||
} else {
|
||||
paths.push({
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
path: path.resolve('data', mainBucket, sk.key),
|
||||
backend: 'local',
|
||||
isArchived: false,
|
||||
})
|
||||
}
|
||||
const storagePath = s3Service.getStoragePath(mainBucket, sk.key)
|
||||
paths.push({
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
path: storagePath,
|
||||
backend: defaultBackend,
|
||||
isArchived,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -303,6 +303,17 @@ export function validateArchiveStorageConfig(): ArchiveStorageConfig {
|
||||
return getArchiveStorageConfig()
|
||||
}
|
||||
|
||||
export function getArchiveStoragePath(
|
||||
key: string,
|
||||
): { path: string; backend: 's3' | 'local' } | null {
|
||||
const config = getArchiveStorageConfig()
|
||||
if (!config.enabled) return null
|
||||
if (config.backend === 's3') {
|
||||
return { path: `s3://${config.bucket}/${key}`, backend: 's3' }
|
||||
}
|
||||
return { path: path.join(config.localRoot, key), backend: 'local' }
|
||||
}
|
||||
|
||||
export class LocalArchiveStorageService implements ArchiveStorageService {
|
||||
readonly backend = 'local' as const
|
||||
readonly isSameS3Target = false
|
||||
|
||||
@@ -360,6 +360,7 @@ export interface S3Service {
|
||||
) => Promise<{ url: string }>
|
||||
abortMultipartUpload: (bucket: string, key: string, uploadId: string) => Promise<void>
|
||||
resolveInput: (bucket: string, key: string) => Promise<string>
|
||||
getStoragePath: (bucket: string, key: string) => string
|
||||
}
|
||||
|
||||
export class S3StorageService implements S3Service {
|
||||
@@ -760,6 +761,10 @@ export class S3StorageService implements S3Service {
|
||||
async resolveInput(bucket: string, key: string): Promise<string> {
|
||||
return this.presign(bucket, key, 'GET', false, undefined, true)
|
||||
}
|
||||
|
||||
getStoragePath(bucket: string, key: string): string {
|
||||
return `s3://${bucket}/${key}`
|
||||
}
|
||||
}
|
||||
|
||||
export class LocalStorageService implements S3Service {
|
||||
@@ -1049,6 +1054,10 @@ export class LocalStorageService implements S3Service {
|
||||
}
|
||||
return this.presign(bucket, key, 'GET')
|
||||
}
|
||||
|
||||
getStoragePath(bucket: string, key: string): string {
|
||||
return this.getFilePath(bucket, key)
|
||||
}
|
||||
}
|
||||
|
||||
export function getStorageBackend(): 's3' | 'local' {
|
||||
|
||||
Reference in New Issue
Block a user