Compare commits

...
7 Commits
Author SHA1 Message Date
yiling 7c3412686c Release v0.1.5 2026-07-02 19:01:12 +08:00
Yiling-J 297c4903fe Merge pull request #165 from shumaiOne/refactor/friendly-storage-filenames
refactor(storage): use original filenames for stored files
2026-07-02 18:18:00 +08:00
yiling b7367bb129 refactor(storage): use original filenames and fix download UX
- Store uploaded files as files/{ulid}/{original-name} instead of files/{ulid}/raw
- Store transcoded videos as {stem}-{resolution}.mp4 (e.g. foo-720p.mp4)
- Store transcoded images as {stem}-{width}p.webp (e.g. foo-1920p.webp)
- Add download?: boolean param to presign - only sets Content-Disposition: attachment when true
- Local storage handler only sets Content-Disposition when ?download=1 query param is present
- Add POST /api/files/download-url endpoint for key-based single file downloads
  - Uses ResourceType.Asset for permission check
  - Validates key belongs to the asset's storage directory
- Fix existing download-links API to pass download: true to presign
- Fix breadcrumb navbar download: video shows resolution submenu, others download directly
- Fix video player download: calls download-url API, shows correct file type label
- Remove filename query param hack from presign URLs
- Add sanitizeFilename() and stemFromKey() utility helpers
- Add key field to VideoTranscode DTO interface
2026-07-02 17:39:23 +08:00
yiling 114cdca4ee update aliyun acr image 2026-07-02 10:51:06 +08:00
Yiling-J 9d7b142129 Improve formatting of commands in README_CN.md
Fix formatting issues in README_CN.md for clarity.
2026-07-02 10:44:57 +08:00
Yiling-J 91fafafeff Merge pull request #164 from shumaiOne/feature/update-release-workflow
ci(release): add Aliyun ACR push to release workflow
2026-07-02 10:10:32 +08:00
yiling d5b9439371 ci(release): add Aliyun ACR push to release workflow 2026-07-02 10:02:13 +08:00
40 changed files with 606 additions and 118 deletions
+11 -1
View File
@@ -27,12 +27,22 @@ jobs:
username: shumaione
password: ${{ secrets.DOCKERHUB_TOKEN }}
# Log in to Aliyun ACR
- name: Log in to Aliyun ACR
uses: docker/login-action@v3
with:
registry: crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com
username: njjyl723
password: ${{ secrets.ALI_ACR_TOKEN }}
# Extract metadata (tags, labels) for Docker
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: shumaione/shumai
images: |
shumaione/shumai
crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com/shumai-one/shumai
tags: |
type=semver,pattern={{version}}
type=ref,event=branch
+17 -17
View File
@@ -51,44 +51,44 @@
```bash
mkdir shumai && cd shumai
```
```
2. 下载 `docker-compose.yaml` 配置文件:
```bash
curl -o docker-compose.yaml [https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose-aliyun.yaml](https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.yaml)
```bash
curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose-aliyun.yaml
```
```
3. 配置环境变量(可选):
* `SHUMAI_SERVER_PORT` 用于控制 Shumai 服务监听的端口,默认值为 `3000`。
* 默认情况下,该 Docker Compose 部署会自动启用集成的本地存储。若想切换至外部 S3 兼容存储,请将 `AWS_ENDPOINT_URL_S3` 设置为对应云厂商的 Endpoint URL,并根据需要配置相应的 AWS 凭证和存储桶(Bucket)参数:
```
AWS_ENDPOINT_URL_S3: S3_ENDPOINT_URL
S3_REGION: S3_REGION
S3_BUCKET: BUCKET_NAME
S3_ACCESS_KEY_ID: ID
S3_SECRET_ACCESS_KEY: KEY
```
AWS_ENDPOINT_URL_S3: S3_ENDPOINT_URL
S3_REGION: S3_REGION
S3_BUCKET: BUCKET_NAME
S3_ACCESS_KEY_ID: ID
S3_SECRET_ACCESS_KEY: KEY
```
```
* 默认情况下 `AWS_ENDPOINT_URL_S3` 指向 `http://localhost:{SHUMAI_SERVER_PORT}`。如果您使用本地存储,但希望在自定义的主机名或端口上公开访问 Shumai,请务必将 `AWS_ENDPOINT_URL_S3` 修改为浏览器访问该服务时所使用的外部 URL。
例如,如果您将 `docker-compose.yaml` 中的端口映射从 `3000:3000` 修改为了 `12345:3000`,且服务部署在 IP 为 `12.34.56.78` 的服务器上,请进行如下设置:
```
AWS_ENDPOINT_URL_S3: [http://12.34.56.78:12345](http://12.34.56.78:12345)
```
AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345
```
```
此地址必须能够从客户端浏览器正常访问,且必须包含对外暴露的实际端口号。
4. 在后台启动所有服务:
```bash
docker compose up -d
```bash
docker compose up -d
```
```
5. 打开浏览器,访问 `http://localhost:3000` 即可开始使用(若是远程部署,请访问 `http://<您的服务器IP>:3000`)。
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent-app",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"bin": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/cli",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"bin": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/transcode-app",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"bin": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/web-app",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"dependencies": {
@@ -17,7 +17,7 @@ services:
retries: 5
shumai:
image: crpi-gd059yeolk1as59f.cn-shanghai.personal.cr.aliyuncs.com/shumaione/shumai:latest
image: crpi-000nkafzu6xvpwsc.cn-hongkong.personal.cr.aliyuncs.com/shumai-one/shumai:latest
container_name: shumai_app
ports:
- '3000:3000'
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "shumai",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"repository": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/agent",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"main": "./src/index.ts",
+2 -1
View File
@@ -6,6 +6,7 @@ import * as path from 'path'
import { s3Service } from '@shumai/core/src/s3/s3'
import { detectSupportedMimeType } from '@shumai/core/src/utils/mime'
import { ulid } from 'ulid'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
function getMimeType(filePath: string): string {
try {
@@ -64,7 +65,7 @@ export function createCreateFileTool(userId: string): AgentTool<typeof createFil
const mimeType = getMimeType(absolutePath)
// Generate compliant S3 key matching normal file upload format
const s3Key = `files/${ulid()}/raw`
const s3Key = `files/${ulid()}/${sanitizeFilename(path.basename(absolutePath))}`
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
const result = await executeAgentToolWorkflow({
+2 -1
View File
@@ -6,6 +6,7 @@ import * as path from 'path'
import { s3Service } from '@shumai/core/src/s3/s3'
import { detectSupportedMimeType } from '@shumai/core/src/utils/mime'
import { ulid } from 'ulid'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
function getMimeType(filePath: string): string {
try {
@@ -65,7 +66,7 @@ export function createCreateVersionTool(userId: string): AgentTool<typeof create
const mimeType = getMimeType(absolutePath)
// Generate compliant S3 key matching normal file upload format
const s3Key = `files/${ulid()}/raw`
const s3Key = `files/${ulid()}/${sanitizeFilename(path.basename(absolutePath))}`
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
const result = await executeAgentToolWorkflow({
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/api",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"main": "./src/index.ts",
+27
View File
@@ -299,5 +299,32 @@ const route = new Hono<{ Variables: { user: User } }>()
const files = await assetService.getDownloadLinks(req.ids)
return c.json({ files })
})
.post(
'/files/download-url',
zValidator('json', z.object({ key: z.string().min(1), assetId: z.string().min(1) })),
async (c) => {
const user = c.get('user')
const req = c.req.valid('json')
const { key, assetId } = req
// Validate key format
if (!key.startsWith('files/')) {
return c.json({ error: 'Invalid key' }, 400)
}
// Verify user has read access to the asset
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
// Verify key belongs to the asset and generate download URL
const url = await assetService.getDownloadUrl(assetId, key)
return c.json({ url })
},
)
export default route
+22
View File
@@ -147,6 +147,28 @@ const route = app
return handlePublicShareError(c, err)
}
})
.post(
'/shares/:shareId/files/:fileId/download-url',
zValidator('json', z.object({ key: z.string().min(1) })),
async (c) => {
const fileId = c.req.param('fileId')
const password = c.req.header('x-share-password')
const { key } = c.req.valid('json')
try {
await shareService.verifyPublicAccess(fileId, password)
if (!key.startsWith('files/')) {
return c.json({ error: 'Invalid key' }, 400)
}
const url = await assetService.getDownloadUrl(fileId, key)
return c.json({ url })
} catch (err) {
return handlePublicShareError(c, err)
}
},
)
.get(
'/shares/:shareId/files/:fileId/comments',
zValidator('query', paginationParamsSchema),
+19 -11
View File
@@ -39,7 +39,7 @@ describe('S3 API', () => {
}
})
it('GET /files/* with filename query parameter sets Content-Disposition header', async () => {
it('GET /files/* sets Content-Disposition: attachment only when download=1 query param is present', async () => {
// We need to capture the onFound callback from serveStatic options
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let onFoundCallback: ((path: string, c: any) => void) | undefined
@@ -54,26 +54,34 @@ describe('S3 API', () => {
const { default: route } = await import('./s3')
const app = new Hono().route('/files', route)
// First, verify onFound is called through the mock
await app.request('/files/b1/test.webp?filename=my-file.txt')
await app.request('/files/b1/test.webp?download=1')
expect(onFoundCallback).toBeDefined()
// Test the callback directly
const mockContext = {
req: {
query: (key: string) => (key === 'filename' ? 'my-file.txt' : undefined),
},
// Test with download=1
const mockContextWithDownload = {
req: { query: (key: string) => (key === 'download' ? '1' : undefined) },
header: vi.fn(),
}
if (onFoundCallback) {
onFoundCallback('/files/b1/test.webp', mockContext)
expect(mockContext.header).toHaveBeenCalledWith(
onFoundCallback('/files/b1/test.webp', mockContextWithDownload)
expect(mockContextWithDownload.header).toHaveBeenCalledWith(
'Content-Disposition',
'attachment; filename="my-file.txt"',
'attachment',
)
}
// Test without download param
const mockContextWithout = {
req: { query: () => undefined },
header: vi.fn(),
}
if (onFoundCallback) {
onFoundCallback('/files/b1/test.webp', mockContextWithout)
expect(mockContextWithout.header).not.toHaveBeenCalled()
}
})
it('PUT /files/:bucket/:key calls s3Service.putObject', async () => {
+2 -4
View File
@@ -9,10 +9,8 @@ const route = new Hono()
root: './data',
rewriteRequestPath: (path) => path.replace(/^\/files\//, ''),
onFound: (_path, c) => {
const filename = c.req.query('filename')
if (filename) {
const safeFilename = filename.replace(/["\r\n]/g, '_')
c.header('Content-Disposition', `attachment; filename="${safeFilename}"`)
if (c.req.query('download') === '1') {
c.header('Content-Disposition', 'attachment')
}
},
}),
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/core",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"main": "./src/index.ts",
+99
View File
@@ -2222,4 +2222,103 @@ describe('AssetService — natural sort by name', () => {
expect(childActiveDb?.status).toBe('uploaded')
})
})
describe('getDownloadUrl', () => {
it('should return a presigned download URL for a key in the same directory', async () => {
const asset = await prisma.asset.create({
data: {
name: 'test.mp4',
type: AssetType.file,
status: AssetStatus.uploaded,
storageKey: {
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
},
},
})
const url = await assetService.getDownloadUrl(
asset.id,
'files/01TESTULID000000000000000/test-720p.mp4',
)
expect(url).toBe('http://mock-s3-url')
})
it('should return a presigned download URL for the original key', async () => {
const asset = await prisma.asset.create({
data: {
name: 'test.mp4',
type: AssetType.file,
status: AssetStatus.uploaded,
storageKey: {
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
},
},
})
const url = await assetService.getDownloadUrl(
asset.id,
'files/01TESTULID000000000000000/test.mp4',
)
expect(url).toBe('http://mock-s3-url')
})
it('should throw if the key does not share the same parent directory', async () => {
const asset = await prisma.asset.create({
data: {
name: 'test.mp4',
type: AssetType.file,
status: AssetStatus.uploaded,
storageKey: {
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
},
},
})
await expect(
assetService.getDownloadUrl(asset.id, 'files/01OTHERULIDXXXXXXXXXX/hack.mp4'),
).rejects.toThrow('Key does not belong to this asset')
})
it('should throw if the key contains path traversal segments', async () => {
const asset = await prisma.asset.create({
data: {
name: 'test.mp4',
type: AssetType.file,
status: AssetStatus.uploaded,
storageKey: {
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
},
},
})
await expect(
assetService.getDownloadUrl(
asset.id,
'files/01TESTULID000000000000000/../01OTHERULIDXXXXXXXXXX/secret.mp4',
),
).rejects.toThrow('Key does not belong to this asset')
})
it('should throw if the asset does not exist', async () => {
await expect(
assetService.getDownloadUrl('nonexistent-id', 'files/01TESTULID000000000000000/test.mp4'),
).rejects.toThrow('Asset not found or has no storage key')
})
it('should throw if the asset has no storage key', async () => {
const asset = await prisma.asset.create({
data: {
name: 'folder',
type: AssetType.folder,
status: AssetStatus.uploaded,
},
})
await expect(
assetService.getDownloadUrl(asset.id, 'files/01TESTULID000000000000000/test.mp4'),
).rejects.toThrow('Asset not found or has no storage key')
})
})
})
+37 -20
View File
@@ -842,7 +842,6 @@ export class AssetService {
process.env.S3_BUCKET || 'shumai',
info.media.original.key,
'GET',
a.name,
)
}
}
@@ -1583,7 +1582,7 @@ export class AssetService {
}
}
const media = latestVersion.media as PrismaJson.MediaInfo | null
let media = latestVersion.media as PrismaJson.MediaInfo | null
if (media && media.videoPreview?.key) {
media.videoPreview.url = await s3Service.presign(
process.env.S3_BUCKET || 'shumai',
@@ -1593,21 +1592,19 @@ export class AssetService {
}
const key = latestVersion.storageKey?.key
if (media && key) {
const fileName =
a.type === AssetType.version_stack && (a.name === '' || !a.name)
? latestVersion.name
: a.name
if (key) {
if (!media) {
media = {
original: null,
videoTranscodes: [],
imageTranscodes: [],
} as unknown as PrismaJson.MediaInfo
}
media.original = {
key,
downloadUrl: await s3Service.presign(
process.env.S3_BUCKET || 'shumai',
key,
'GET',
fileName,
),
downloadUrl: await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET'),
filesizeInBytes: latestVersion.sizeByte,
codec: '', // TODO: extract from metadata if needed
codec: '',
}
}
@@ -1845,12 +1842,7 @@ export class AssetService {
const key = file.storageKeyId ? storageKeyMap.get(file.storageKeyId) : null
if (!key) return null
const url = await s3Service.presign(
process.env.S3_BUCKET || 'shumai',
key,
'GET',
file.name,
)
const url = await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET', true)
return {
id: file.id,
@@ -1863,6 +1855,31 @@ export class AssetService {
return downloadLinks.filter((link): link is { id: string; name: string; url: string } => !!link)
}
/**
* 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.)
* that belong to an asset they have access to.
*/
async getDownloadUrl(assetId: string, key: string): Promise<string> {
const asset = await this.prismaClient.asset.findUnique({
where: { id: assetId },
select: { storageKey: { select: { key: true } } },
})
if (!asset?.storageKey?.key) {
throw new Error('Asset not found or has no storage key')
}
// Verify the requested key shares the same parent directory as the asset's storage key
const storageKey = asset.storageKey.key
const assetDir = storageKey.substring(0, storageKey.lastIndexOf('/') + 1)
if (key.includes('..') || !key.startsWith(assetDir)) {
throw new Error('Key does not belong to this asset')
}
return s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET', true)
}
private async toPreviewInfo(asset: Asset | AssetWithIncludes): Promise<PreviewInfo | null> {
if (!asset.media) return null
+33 -6
View File
@@ -117,17 +117,44 @@ describe('S3Service implementations', () => {
delete process.env.PRESIGNED_URL_EXPIRES_IN
})
it('should pass filename to presign', async () => {
it('should not set contentDisposition for normal GET presign', async () => {
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
await s3.presign('bucket', 'key', 'GET', 'test.txt')
await s3.presign('bucket', 'key', 'GET')
expect(s3PresignSpy).toHaveBeenCalledWith(
'key',
expect.not.objectContaining({
contentDisposition: expect.anything(),
}),
)
})
it('should set contentDisposition attachment when download is true', async () => {
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
await s3.presign('bucket', 'key', 'GET', true)
expect(s3PresignSpy).toHaveBeenCalledWith(
'key',
expect.objectContaining({
contentDisposition: 'attachment; filename="test.txt"',
contentDisposition: 'attachment',
}),
)
})
it('should not cache download presign URLs', async () => {
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
s3PresignSpy.mockClear()
s3PresignSpy.mockReturnValueOnce('http://download-url-1')
s3PresignSpy.mockReturnValueOnce('http://download-url-2')
const url1 = await s3.presign('bucket', 'key', 'GET', true)
const url2 = await s3.presign('bucket', 'key', 'GET', true)
expect(url1).toBe('http://download-url-1')
expect(url2).toBe('http://download-url-2')
expect(s3PresignSpy).toHaveBeenCalledTimes(2)
})
})
describe('LocalStorageService', () => {
@@ -182,9 +209,9 @@ describe('S3Service implementations', () => {
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt')
})
it('should generate a local presign URL with filename', async () => {
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', 'original.txt')
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt?filename=original.txt')
it('should generate a local presign URL with download param', async () => {
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true)
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt?download=1')
})
it('should throw an error for unsupported presign methods', async () => {
+10 -10
View File
@@ -54,7 +54,7 @@ export interface S3Service {
listObjects(bucket: string, prefix: string): Promise<string[]>
uploadFile(filePath: string, contentType: string): Promise<string>
uploadFileToKey(filePath: string, key: string, contentType: string): Promise<void>
presign(bucket: string, key: string, method: string, filename?: string): Promise<string>
presign(bucket: string, key: string, method: string, download?: boolean): Promise<string>
}
export class S3StorageService implements S3Service {
@@ -203,16 +203,16 @@ export class S3StorageService implements S3Service {
})
}
async presign(bucket: string, key: string, method: string, filename?: string): Promise<string> {
async presign(bucket: string, key: string, method: string, download?: boolean): Promise<string> {
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const expiresInSeconds = expireHours * 3600
// Cache time is 2/3 of expire time, rounded to minute
const cacheMinutes = Math.round((expireHours * 60 * 2) / 3)
const cacheTtlMs = cacheMinutes * 60 * 1000
const cacheKey = `${bucket}:${key}:${filename || ''}`
const cacheKey = `${bucket}:${key}`
if (method === 'GET') {
if (method === 'GET' && !download) {
const cached = this.presignCache.get(cacheKey)
if (cached) return cached
}
@@ -225,13 +225,13 @@ export class S3StorageService implements S3Service {
method: method as any,
}
if (filename) {
options.contentDisposition = `attachment; filename="${filename}"`
if (download) {
options.contentDisposition = 'attachment'
}
const url = this.client.presign(key, options)
if (method === 'GET') {
if (method === 'GET' && !download) {
this.presignCache.set(cacheKey, url, cacheTtlMs)
}
@@ -435,7 +435,7 @@ export class LocalStorageService implements S3Service {
await fs.promises.copyFile(filePath, destPath)
}
async presign(bucket: string, key: string, method: string, filename?: string): Promise<string> {
async presign(bucket: string, key: string, method: string, download?: boolean): Promise<string> {
if (method !== 'GET' && method !== 'PUT') {
throw new Error(`Invalid method: ${method}`)
}
@@ -443,8 +443,8 @@ export class LocalStorageService implements S3Service {
return `${this.endpoint}${signLocalUrl(bucket, key)}`
}
let url = `${this.endpoint}/files/${bucket}/${key}`
if (filename) {
url += `?filename=${encodeURIComponent(filename)}`
if (download) {
url += '?download=1'
}
return url
}
+2 -1
View File
@@ -21,6 +21,7 @@ import {
import { ImageTranscoder, VideoTranscoder } from '@shumai/transcode'
import { generateKeyBetween } from 'jittered-fractional-indexing'
import { ulid } from 'ulid'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
export class UploadService {
constructor(private readonly prismaClient: typeof prisma = prisma) {}
@@ -130,7 +131,7 @@ export class UploadService {
const assetType = file.type === 'folder' ? AssetType.folder : AssetType.file
let key: string | null = null
if (assetType === AssetType.file) {
key = `files/${ulid()}/raw`
key = `files/${ulid()}/${sanitizeFilename(file.name)}`
}
const newAsset = await tx.asset.create({
+66
View File
@@ -0,0 +1,66 @@
import { describe, it, expect } from 'vitest'
import { sanitizeFilename, stemFromKey } from './filename'
describe('sanitizeFilename', () => {
it('returns the filename unchanged when it is already safe', () => {
expect(sanitizeFilename('foo.mp4')).toBe('foo.mp4')
expect(sanitizeFilename('my-video_final.mov')).toBe('my-video_final.mov')
})
it('replaces slashes and backslashes with underscores', () => {
expect(sanitizeFilename('path/to/file.mp4')).toBe('path_to_file.mp4')
expect(sanitizeFilename('path\\to\\file.mp4')).toBe('path_to_file.mp4')
})
it('replaces NUL bytes and control characters', () => {
expect(sanitizeFilename('foo\x00bar.mp4')).toBe('foo_bar.mp4')
expect(sanitizeFilename('foo\x01bar.mp4')).toBe('foo_bar.mp4')
})
it('trims leading and trailing dots and whitespace', () => {
expect(sanitizeFilename('.hidden')).toBe('hidden')
expect(sanitizeFilename('...dotted...')).toBe('dotted')
expect(sanitizeFilename(' spaced ')).toBe('spaced')
expect(sanitizeFilename(' .mixed. ')).toBe('mixed')
})
it('collapses multiple consecutive underscores', () => {
expect(sanitizeFilename('a//b')).toBe('a_b')
expect(sanitizeFilename('a///b')).toBe('a_b')
})
it('returns "file" for empty or whitespace-only input', () => {
expect(sanitizeFilename('')).toBe('file')
expect(sanitizeFilename(' ')).toBe('file')
expect(sanitizeFilename('...')).toBe('file')
})
it('preserves unicode characters', () => {
expect(sanitizeFilename('视频文件.mp4')).toBe('视频文件.mp4')
expect(sanitizeFilename('café.png')).toBe('café.png')
})
it('preserves spaces within the filename', () => {
expect(sanitizeFilename('my video file.mp4')).toBe('my video file.mp4')
})
})
describe('stemFromKey', () => {
it('extracts the stem from a key with extension', () => {
expect(stemFromKey('files/01ABC/foo.mp4')).toBe('foo')
expect(stemFromKey('files/01ABC/my-video.mov')).toBe('my-video')
})
it('extracts the stem from a key without extension', () => {
expect(stemFromKey('files/01ABC/raw')).toBe('raw')
})
it('handles keys with multiple dots in filename', () => {
expect(stemFromKey('files/01ABC/my.video.file.mp4')).toBe('my.video.file')
})
it('handles keys with no directory', () => {
expect(stemFromKey('foo.mp4')).toBe('foo')
expect(stemFromKey('raw')).toBe('raw')
})
})
+36
View File
@@ -0,0 +1,36 @@
import path from 'path'
/**
* Sanitize a filename for safe use as an S3 key segment.
* - Replaces characters unsafe for S3/filesystems with '_'
* - Trims leading/trailing whitespace and dots
* - Falls back to 'file' if the result is empty
*/
export function sanitizeFilename(filename: string): string {
// Replace NUL bytes, slashes, backslashes, and control characters
// eslint-disable-next-line no-control-regex
let safe = filename.replace(/[\x00-\x1f\x7f/\\]/g, '_')
// Trim leading/trailing whitespace and dots
safe = safe.replace(/^[\s.]+|[\s.]+$/g, '')
// Collapse multiple underscores
safe = safe.replace(/_+/g, '_')
if (!safe) {
return 'file'
}
return safe
}
/**
* Extract the filename stem (without extension) from a storage key.
* e.g. 'files/01ABC.../foo.mp4' → 'foo'
* 'files/01ABC.../raw' → 'raw'
*/
export function stemFromKey(key: string): string {
const basename = path.basename(key)
const ext = path.extname(basename)
return ext ? basename.slice(0, -ext.length) : basename
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/db",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"main": "./src/index.ts",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/dtos",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"main": "./src/index.ts",
+1
View File
@@ -279,6 +279,7 @@ export interface ImageTranscode {
export interface VideoTranscode {
id: string
url: string
key: string
width: number
height: number
size: number
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/tableprinter",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"main": "./src/index.ts",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/transcode",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"main": "./src/index.ts",
+11 -4
View File
@@ -2,6 +2,7 @@ import { prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { transcodeService } from '@shumai/core'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { stemFromKey } from '@shumai/core/src/utils/filename'
import { ApplicationFailure } from '@temporalio/activity'
import * as path from 'path'
import * as fs from 'fs'
@@ -162,7 +163,9 @@ export async function transcodeVideoActivity(
params: VideoActivityParams,
): Promise<PrismaJson.VideoTranscode> {
const bucket = process.env.S3_BUCKET || 'shumai'
const key = path.join(path.dirname(params.assetKey), `video-${params.videoSpec.height}p.mp4`)
const stem = stemFromKey(params.assetKey)
const res = params.videoSpec.resolution || `${params.videoSpec.height}p`
const key = path.posix.join(path.posix.dirname(params.assetKey), `${stem}-${res}.mp4`)
try {
await s3Service.headObject(bucket, key)
@@ -172,7 +175,7 @@ export async function transcodeVideoActivity(
}
const tmpDir = path.dirname(params.filePath)
const outputFile = path.join(tmpDir, `video-${params.videoSpec.height}p.mp4`)
const outputFile = path.join(tmpDir, `${stem}-${res}.mp4`)
try {
let targetFps: number | string = params.originalFps || 30
@@ -237,7 +240,11 @@ export async function transcodeImageActivity(
params: ImageActivityParams,
): Promise<PrismaJson.ImageTranscode> {
const bucket = process.env.S3_BUCKET || 'shumai'
const key = path.join(path.dirname(params.assetKey), `image-${params.imageSpec.width}p.webp`)
const stem = stemFromKey(params.assetKey)
const key = path.posix.join(
path.posix.dirname(params.assetKey),
`${stem}-${params.imageSpec.width}p.webp`,
)
try {
await s3Service.headObject(bucket, key)
@@ -247,7 +254,7 @@ export async function transcodeImageActivity(
}
const tmpDir = path.dirname(params.filePath)
const outputFile = path.join(tmpDir, `image-${params.imageSpec.width}p.webp`)
const outputFile = path.join(tmpDir, `${stem}-${params.imageSpec.width}p.webp`)
try {
await transcodeService.transcodeImage(
+97 -3
View File
@@ -1,8 +1,10 @@
import { Badge } from '@/ui/components/ui/badge'
import { client } from '@/ui/api/client'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuPortal,
DropdownMenuSeparator,
DropdownMenuSub,
@@ -44,6 +46,15 @@ interface BreadcrumbNavProps {
shareId?: string
onFolderClick?: (folderId: string) => void
fileId?: string
downloadInfo?: {
originalKey?: string
videoTranscodes?: Array<{
key: string
width: number
height: number
isRaw?: boolean
}>
}
versions?: Array<{
id: string
version: number
@@ -69,6 +80,8 @@ export function BreadcrumbNav({
isPublic = false,
onFolderClick,
fileId,
shareId,
downloadInfo,
versions,
}: BreadcrumbNavProps) {
const navigate = useNavigate()
@@ -83,6 +96,33 @@ export function BreadcrumbNav({
})
}
const handleDownload = async (key: string) => {
if (!fileId) return
try {
const res = shareId
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
param: { shareId, fileId },
json: { key },
})
: await client.api.files['download-url'].$post({
json: { key, assetId: fileId },
})
if (!res.ok) return
const { url } = await res.json()
const link = document.createElement('a')
link.href = url
link.download = ''
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch {
// silently fail
}
}
const hasVideoTranscodes =
downloadInfo?.videoTranscodes && downloadInfo.videoTranscodes.filter((t) => !t.isRaw).length > 0
const breadcrumbs: { name: string; path?: string; id?: string; isMuted?: boolean }[] = isPublic
? [
{
@@ -155,9 +195,63 @@ export function BreadcrumbNav({
<ChevronDown className="h-4 w-4" />
</DropdownMenuTrigger>
<DropdownMenuContent className="w-56">
<DropdownMenuItem onClick={() => console.log('Download')}>
Download
</DropdownMenuItem>
{hasVideoTranscodes ? (
<DropdownMenuSub>
<DropdownMenuSubTrigger className="flex items-center gap-2">
<span>{m.download()}</span>
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent className="w-48">
<DropdownMenuLabel>{m.download()}</DropdownMenuLabel>
{downloadInfo?.videoTranscodes
?.filter((t) => !t.isRaw)
.map((t) => {
const longSide = Math.max(t.width, t.height)
let resolution = `${t.height}p`
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
return (
<DropdownMenuItem
key={resolution}
onClick={() => handleDownload(t.key)}
className="flex items-center justify-between"
>
<span>{resolution}</span>
<span className="text-xs text-muted-foreground">MP4</span>
</DropdownMenuItem>
)
})}
{downloadInfo?.originalKey && (
<>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() => handleDownload(downloadInfo.originalKey!)}
className="flex items-center justify-between"
>
<span>Original</span>
<span className="text-xs text-muted-foreground">
{currentAsset.name?.split('.').pop()?.toUpperCase() || 'RAW'}
</span>
</DropdownMenuItem>
</>
)}
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
) : (
<DropdownMenuItem
onClick={() =>
downloadInfo?.originalKey && handleDownload(downloadInfo.originalKey)
}
disabled={!downloadInfo?.originalKey}
>
{m.download()}
</DropdownMenuItem>
)}
{canEdit && (
<DropdownMenuItem onClick={() => console.log('Rename')}>
Rename
+29 -11
View File
@@ -1,4 +1,5 @@
import { useScreenSize } from '@/ui/hooks/useScreenSize'
import { client } from '@/ui/api/client'
import { getBestTranscode } from '@/ui/lib/media'
import type { AssetInfo } from '@shumai/dtos'
import { Check, Copy, Download, Minus, Plus } from 'lucide-react'
@@ -19,6 +20,7 @@ type FileViewerProps = {
onTimeUpdate?: (time: number) => void
annotations?: Annotation[]
startTime?: number
shareId?: string
children?: ReactNode
}
@@ -30,6 +32,7 @@ export function FileViewer({
onTimeUpdate,
annotations,
startTime,
shareId,
children,
}: FileViewerProps) {
const { width: screenWidth } = useScreenSize()
@@ -133,15 +136,29 @@ export function FileViewer({
}
}
const handleDownload = () => {
const url = file.media?.original?.downloadUrl
if (!url) return
const link = document.createElement('a')
link.href = url
link.download = file.name || 'image'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
const handleDownload = async () => {
const key = file.media?.original?.key
if (!key || !file.id) return
try {
const res = shareId
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
param: { shareId, fileId: file.id },
json: { key },
})
: await client.api.files['download-url'].$post({
json: { key, assetId: file.id },
})
if (!res.ok) return
const { url } = await res.json()
const link = document.createElement('a')
link.href = url
link.download = ''
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch {
// silently fail
}
}
const handleCopy = async () => {
@@ -210,7 +227,7 @@ export function FileViewer({
<div className="relative px-4 py-3 bg-card border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200">
<button
onClick={handleDownload}
disabled={!file.media?.original?.downloadUrl}
disabled={!file.media?.original?.key}
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
title="Download original file"
>
@@ -291,7 +308,7 @@ export function FileViewer({
</button>
<button
onClick={handleDownload}
disabled={!file.media?.original?.downloadUrl}
disabled={!file.media?.original?.key}
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
title="Download original image"
>
@@ -311,6 +328,7 @@ export function FileViewer({
onTimeUpdate={onTimeUpdate}
annotations={displayAnnotations}
startTime={startTime}
shareId={shareId}
>
{children}
</VideoPlayer>
@@ -350,6 +350,17 @@ export function PublicShareManager({
isPublic: true,
shareId,
fileId: viewingFileId || undefined,
downloadInfo: viewingFileData
? {
originalKey: viewingFileData.media?.original?.key,
videoTranscodes: viewingFileData.media?.videoTranscodes?.map((t) => ({
key: t.key,
width: t.width,
height: t.height,
isRaw: t.isRaw,
})),
}
: undefined,
onFolderClick: handleBreadcrumbClick,
isRightSidebarCollapsed,
onRightSidebarToggle: () => setIsRightSidebarCollapsed((prev) => !prev),
@@ -458,6 +469,7 @@ export function PublicShareManager({
onTimeUpdate={setCurrentTime}
annotations={annotations}
startTime={startTime}
shareId={shareId}
/>
)}
</div>
+2
View File
@@ -19,6 +19,7 @@ export function TopNav() {
currentAsset,
isRootFolder,
fileId,
downloadInfo,
versions,
isPublic,
shareId,
@@ -87,6 +88,7 @@ export function TopNav() {
isPublic={isPublic}
shareId={shareId}
fileId={fileId}
downloadInfo={downloadInfo}
versions={versions}
onFolderClick={onFolderClick}
/>
@@ -1,4 +1,5 @@
import type { AssetInfo, VideoTranscode } from '@shumai/dtos'
import { client } from '@/ui/api/client'
import { cn } from '@/ui/lib/utils'
import {
Check,
@@ -58,6 +59,7 @@ interface VideoPlayerProps {
onTimeUpdate?: (time: number) => void
annotations?: Annotation[]
startTime?: number
shareId?: string
children?: React.ReactNode
}
@@ -76,7 +78,7 @@ interface ControlBarProps {
handleVolumeChange: (newVolume: number) => void
changePlaybackRate: (rate: number) => void
changeResolution: (res: DisplayTranscode) => void
handleDownload: (url: string, resolution: string) => void
handleDownload: (key: string) => void
toggleFullScreen: () => void
onZoomChange: (zoom: number) => void
onZoomReset: () => void
@@ -344,11 +346,13 @@ const ControlBar: React.FC<ControlBarProps> = ({
{resolutions.map((res) => (
<DropdownMenuItem
key={res.resolution}
onClick={() => handleDownload(res.url ?? '', res.resolution)}
onClick={() => handleDownload(res.key ?? '')}
className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground"
>
<span>{res.resolution}</span>
<span className="text-xs text-muted-foreground">MP4</span>
<span className="text-xs text-muted-foreground">
{res.isRaw ? data.name?.split('.').pop()?.toUpperCase() || 'RAW' : 'MP4'}
</span>
</DropdownMenuItem>
))}
</DropdownMenuContent>
@@ -376,6 +380,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
onTimeUpdate,
annotations,
startTime,
shareId,
children,
}) => {
const localPlayerRef = useRef<Player | null>(null)
@@ -456,12 +461,16 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
}
// Initial resolution setup
const originalRes = {
const originalRes: DisplayTranscode = {
id: 'original',
url: data.media.original.downloadUrl,
key: data.media.original.key ?? '',
width: data.media.metadata.originalWidth ?? 0,
height: data.media.metadata.originalHeight ?? 0,
size: 0,
isRaw: true,
resolution: 'Original',
} as DisplayTranscode
}
const resolutions: DisplayTranscode[] = (data.media.videoTranscodes ?? []).map((t) => {
const longSide = Math.max(t.width, t.height)
@@ -837,13 +846,27 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
}
}
const handleDownload = (url: string, resolution: string) => {
const link = document.createElement('a')
link.href = url
link.download = `${data.name}-${resolution}.mp4`
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
const handleDownload = async (key: string) => {
try {
const res = shareId
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
param: { shareId, fileId: data.id! },
json: { key },
})
: await client.api.files['download-url'].$post({
json: { key, assetId: data.id! },
})
if (!res.ok) return
const { url } = await res.json()
const link = document.createElement('a')
link.href = url
link.download = ''
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch {
// silently fail
}
}
// Listen for fullscreen change events (ESC key)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/webui",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"main": "./index.html",
@@ -148,6 +148,15 @@ function FileViewPage() {
},
isRootFolder: false,
fileId,
downloadInfo: {
originalKey: fileData.media?.original?.key,
videoTranscodes: fileData.media?.videoTranscodes?.map((t) => ({
key: t.key,
width: t.width,
height: t.height,
isRaw: t.isRaw,
})),
},
versions: versionsDataList,
onFolderClick: (id: string) => {
navigate({
+9
View File
@@ -13,6 +13,15 @@ export interface TopNavProjectState {
}
isRootFolder: boolean
fileId?: string
downloadInfo?: {
originalKey?: string
videoTranscodes?: Array<{
key: string
width: number
height: number
isRaw?: boolean
}>
}
versions?: Array<{
id: string
version: number
+1 -1
View File
@@ -1 +1 @@
export const APP_VERSION = '0.1.4'
export const APP_VERSION = '0.1.5'
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@shumai/workflow-core",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"main": "./src/index.ts",