mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 19:27:14 +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:
@@ -3,6 +3,7 @@ import { Hono, type Context, type Next } from 'hono'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import fileRoute from './file'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { projectService } from '@shumai/core/src/project/project'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { notificationService } from '@shumai/core/src/notification/notification'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
@@ -871,6 +872,127 @@ describe('file api', () => {
|
||||
mockGetProjectIds.mockRestore()
|
||||
})
|
||||
|
||||
describe('POST /files/storage-paths', () => {
|
||||
it('returns empty paths if ids are empty', async () => {
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/storage-paths', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids: [] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json).toEqual({ paths: [] })
|
||||
})
|
||||
|
||||
it('rejects if assets belong to multiple projects', async () => {
|
||||
const mockGetProjectIds = vi
|
||||
.spyOn(assetService, 'getProjectIds')
|
||||
.mockResolvedValue(['proj-1', 'proj-2'])
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/storage-paths', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids: ['asset-1', 'asset-2'] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
const json = await res.json()
|
||||
expect(json.error).toBe('All selected items must belong to the same project')
|
||||
|
||||
mockGetProjectIds.mockRestore()
|
||||
})
|
||||
|
||||
it('denies access if user is not a team owner (admin)', async () => {
|
||||
const mockGetProjectIds = vi
|
||||
.spyOn(assetService, 'getProjectIds')
|
||||
.mockResolvedValue(['proj-1'])
|
||||
const mockGetProjectTeam = vi
|
||||
.spyOn(projectService, 'getProjectTeam')
|
||||
.mockResolvedValue('team-1')
|
||||
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(
|
||||
new HTTPException(403, { message: 'Permission denied' }),
|
||||
)
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/storage-paths', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids: ['asset-1'] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(403)
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user1', name: 'Test User' },
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Team,
|
||||
id: 'team-1',
|
||||
})
|
||||
|
||||
mockGetProjectIds.mockRestore()
|
||||
mockGetProjectTeam.mockRestore()
|
||||
})
|
||||
|
||||
it('returns storage paths if user has team admin permission', async () => {
|
||||
const mockGetProjectIds = vi
|
||||
.spyOn(assetService, 'getProjectIds')
|
||||
.mockResolvedValue(['proj-1'])
|
||||
const mockGetProjectTeam = vi
|
||||
.spyOn(projectService, 'getProjectTeam')
|
||||
.mockResolvedValue('team-1')
|
||||
const mockGetStoragePaths = vi.spyOn(assetService, 'getStoragePaths').mockResolvedValue({
|
||||
paths: [
|
||||
{
|
||||
id: 'asset-1',
|
||||
name: 'clip.mp4',
|
||||
path: 's3://shumai/files/1/clip.mp4',
|
||||
backend: 's3',
|
||||
isArchived: false,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/storage-paths', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids: ['asset-1'] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json.paths).toHaveLength(1)
|
||||
expect(json.paths[0].name).toBe('clip.mp4')
|
||||
expect(json.paths[0].path).toBe('s3://shumai/files/1/clip.mp4')
|
||||
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user1', name: 'Test User' },
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Team,
|
||||
id: 'team-1',
|
||||
})
|
||||
expect(assetService.getStoragePaths).toHaveBeenCalledWith(['asset-1'])
|
||||
|
||||
mockGetProjectIds.mockRestore()
|
||||
mockGetProjectTeam.mockRestore()
|
||||
mockGetStoragePaths.mockRestore()
|
||||
})
|
||||
|
||||
it('rejects if more than 100 ids are provided', async () => {
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const ids = Array.from({ length: 101 }, (_, i) => `asset-${i}`)
|
||||
const res = await app.request('/files/storage-paths', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /files/:fileId/m3u8 routes', () => {
|
||||
it('returns master playlist with correct content-type', async () => {
|
||||
const mockMaster = vi
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { projectService } from '@shumai/core/src/project/project'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { commentService } from '@shumai/core/src/comment/comment'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
@@ -26,6 +27,7 @@ import {
|
||||
updateFileRequestSchema,
|
||||
uploadFileRequestSchema,
|
||||
getDownloadLinksRequestSchema,
|
||||
getStoragePathsRequestSchema,
|
||||
completeCommentRequestSchema,
|
||||
exportCommentsRequestSchema,
|
||||
addCommentReactionRequestSchema,
|
||||
@@ -456,6 +458,37 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
...(skippedArchivedCount > 0 ? { skippedArchivedCount } : {}),
|
||||
})
|
||||
})
|
||||
.post('/files/storage-paths', zValidator('json', getStoragePathsRequestSchema), async (c) => {
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
if (req.ids.length === 0) {
|
||||
return c.json({ paths: [] })
|
||||
}
|
||||
|
||||
const projectIds = await assetService.getProjectIds(req.ids)
|
||||
if (projectIds.length === 0) {
|
||||
return c.json({ paths: [] })
|
||||
}
|
||||
|
||||
if (projectIds.length !== 1) {
|
||||
return c.json({ error: 'All selected items must belong to the same project' }, 400)
|
||||
}
|
||||
|
||||
const projectId = projectIds[0]
|
||||
const teamId = await projectService.getProjectTeam(projectId)
|
||||
|
||||
// Verify user has Admin (owner) permission on the team
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Admin,
|
||||
type: ResourceType.Team,
|
||||
id: teamId,
|
||||
})
|
||||
|
||||
const result = await assetService.getStoragePaths(req.ids)
|
||||
return c.json(result)
|
||||
})
|
||||
.post(
|
||||
'/files/download-url',
|
||||
zValidator('json', z.object({ key: z.string().min(1), assetId: z.string().min(1) })),
|
||||
|
||||
Reference in New Issue
Block a user