mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 19:04:23 +00:00
feat(file-browser): add context-aware copy submenu (#517)
* feat(file-browser): add context-aware copy submenu * refactor(file-browser): address code review feedback for copy submenu * fix(webui): align submenu trigger text and widen file context menu * test(e2e): guard clipboard permissions in copy-menu spec for cross-browser support
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,7 +21,9 @@ 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'),
|
||||
}))
|
||||
|
||||
vi.mock('@shumai/core/src/s3/preview-converter', async (importOriginal) => {
|
||||
@@ -3076,6 +3079,248 @@ describe('AssetService — natural sort by name', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('getStoragePaths', () => {
|
||||
it('returns empty array for empty inputs', async () => {
|
||||
const result = await assetService.getStoragePaths([])
|
||||
expect(result).toEqual({ paths: [] })
|
||||
})
|
||||
|
||||
it('resolves files, symlinks, and version stacks while skipping folders', async () => {
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
name: 'StoragePathUser',
|
||||
email: `storage-path-${Date.now()}@example.com`,
|
||||
},
|
||||
})
|
||||
const team = await prisma.team.create({ data: { name: 'StoragePathTeam' } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'StoragePathProject', teamId: team.id },
|
||||
})
|
||||
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'MyFolder',
|
||||
type: AssetType.folder,
|
||||
project: { connect: { id: project.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.processed,
|
||||
},
|
||||
})
|
||||
|
||||
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.processed,
|
||||
storageKey: { create: { key: 'files/ulid1/clip.mp4' } },
|
||||
},
|
||||
})
|
||||
|
||||
const symlink = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'clip-symlink',
|
||||
type: AssetType.symlink,
|
||||
project: { connect: { id: project.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.uploaded,
|
||||
target: { connect: { id: file.id } },
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'stack.png',
|
||||
type: AssetType.version_stack,
|
||||
project: { connect: { id: project.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.processed,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.asset.create({
|
||||
data: {
|
||||
name: 'stack_v1.png',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: project.id } },
|
||||
parent: { connect: { id: stack.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.processed,
|
||||
sortIndex: 'b',
|
||||
storageKey: { create: { key: 'files/ulid2/stack_v1.png' } },
|
||||
},
|
||||
})
|
||||
|
||||
const latestChild = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'stack_v2.png',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: project.id } },
|
||||
parent: { connect: { id: stack.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.processed,
|
||||
sortIndex: 'a',
|
||||
storageKey: { create: { key: 'files/ulid3/stack_v2.png' } },
|
||||
},
|
||||
})
|
||||
|
||||
const result = await assetService.getStoragePaths([folder.id, file.id, symlink.id, stack.id])
|
||||
|
||||
// folder should be skipped, symlink deduplicated to file, stack resolved to latest child
|
||||
expect(result.paths).toHaveLength(2)
|
||||
const names = result.paths.map((p) => p.name)
|
||||
expect(names).toContain('clip.mp4')
|
||||
expect(names).toContain('stack_v2.png')
|
||||
|
||||
const clipPath = result.paths.find((p) => p.name === 'clip.mp4')!
|
||||
expect(clipPath.path).toContain('files/ulid1/clip.mp4')
|
||||
expect(clipPath.isArchived).toBe(false)
|
||||
|
||||
const stackPath = result.paths.find((p) => p.name === 'stack_v2.png')!
|
||||
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', () => {
|
||||
it('returns empty lists for empty inputs', async () => {
|
||||
const result = await assetService.getProjectIds([])
|
||||
|
||||
@@ -23,6 +23,8 @@ import {
|
||||
CommentExportFormat,
|
||||
PreviewFormat,
|
||||
TRASH_RETENTION_DAYS,
|
||||
GetStoragePathsResponse,
|
||||
StoragePathItem,
|
||||
} from '@shumai/dtos'
|
||||
import {
|
||||
exportCommentsToFormat,
|
||||
@@ -43,10 +45,11 @@ import {
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { PaginatedData, paginateQuery, PaginationParams } from '@shumai/core/src/pagination'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { s3Service, getStorageBackend } from '@shumai/core/src/s3/s3'
|
||||
import {
|
||||
getArchiveStorageService,
|
||||
isArchiveStorageEnabled,
|
||||
getArchiveStoragePath,
|
||||
} from '@shumai/core/src/s3/archive-storage'
|
||||
import { assetArchiveService } from './asset-archive'
|
||||
import {
|
||||
@@ -1248,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 },
|
||||
@@ -2886,6 +2958,71 @@ export class AssetService {
|
||||
}
|
||||
}
|
||||
|
||||
async getStoragePaths(ids: string[]): Promise<GetStoragePathsResponse> {
|
||||
if (ids.length === 0) return { paths: [] }
|
||||
|
||||
const targetAssetIds = await this.resolveLatestVersionIds(ids)
|
||||
const uniqueTargetIds = Array.from(new Set(targetAssetIds))
|
||||
|
||||
const assets = await this.prismaClient.asset.findMany({
|
||||
where: {
|
||||
id: { in: uniqueTargetIds },
|
||||
isDeleted: false,
|
||||
type: {
|
||||
notIn: [AssetType.folder, AssetType.root, AssetType.share_root, AssetType.share],
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
status: true,
|
||||
storageKey: {
|
||||
select: {
|
||||
key: true,
|
||||
isArchived: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const defaultBackend = getStorageBackend()
|
||||
const mainBucket = process.env.S3_BUCKET || 'shumai'
|
||||
const paths: StoragePathItem[] = []
|
||||
|
||||
for (const asset of assets) {
|
||||
const sk = asset.storageKey
|
||||
if (!sk?.key) continue
|
||||
|
||||
const isArchived =
|
||||
asset.status === AssetStatus.archived ||
|
||||
asset.status === AssetStatus.archiving ||
|
||||
asset.status === AssetStatus.restoring ||
|
||||
sk.isArchived
|
||||
|
||||
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 {
|
||||
const storagePath = s3Service.getStoragePath(mainBucket, sk.key)
|
||||
paths.push({
|
||||
id: asset.id,
|
||||
name: asset.name,
|
||||
path: storagePath,
|
||||
backend: defaultBackend,
|
||||
isArchived,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return { paths }
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a presigned download URL for a given key, verifying it belongs to the asset's
|
||||
* storage directory. This ensures users can only download files (original, transcodes, etc.)
|
||||
|
||||
@@ -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