mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 13:34:33 +00:00
fix local storage video preview
This commit is contained in:
+37
-39
@@ -1,59 +1,57 @@
|
|||||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||||
import { s3Service } from '@/services/s3/s3'
|
import { s3Service } from '@/services/s3/s3'
|
||||||
import route from './s3'
|
|
||||||
import { Hono } from 'hono'
|
import { Hono } from 'hono'
|
||||||
|
|
||||||
describe('S3 API', () => {
|
const { mockServeStatic } = vi.hoisted(() => ({
|
||||||
const app = new Hono().route('/files', route)
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
mockServeStatic: vi.fn<(args: any) => any>(() => (c: any) => c.text('static-file')),
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock('hono/bun', () => ({
|
||||||
|
serveStatic: mockServeStatic,
|
||||||
|
}))
|
||||||
|
|
||||||
|
describe('S3 API', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('GET /files/:bucket/:key sets Content-Type', async () => {
|
it('GET /files/* calls serveStatic with correct options', async () => {
|
||||||
const mockHead = vi.spyOn(s3Service, 'headObject').mockResolvedValue({
|
const { default: route } = await import('./s3')
|
||||||
key: 'test.webp',
|
const app = new Hono().route('/files', route)
|
||||||
size: 100,
|
|
||||||
lastModified: new Date(),
|
|
||||||
contentType: 'image/webp',
|
|
||||||
eTag: '"test"',
|
|
||||||
})
|
|
||||||
const mockGet = vi
|
|
||||||
.spyOn(s3Service, 'getObject')
|
|
||||||
.mockResolvedValue({ buffer: Buffer.from('test'), contentType: 'image/webp' })
|
|
||||||
|
|
||||||
const res = await app.request('/files/b1/test.webp')
|
const res = await app.request('/files/b1/test.webp')
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.headers.get('Content-Type')).toBe('image/webp')
|
expect(await res.text()).toBe('static-file')
|
||||||
expect(mockHead).toHaveBeenCalledWith('b1', 'test.webp')
|
expect(mockServeStatic).toHaveBeenCalledWith(
|
||||||
expect(mockGet).toHaveBeenCalledWith('b1', 'test.webp')
|
expect.objectContaining({
|
||||||
|
root: './data',
|
||||||
|
rewriteRequestPath: expect.any(Function),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Verify rewriteRequestPath logic
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
const options = vi.mocked(mockServeStatic).mock.calls[0][0] as any
|
||||||
|
if (options && options.rewriteRequestPath) {
|
||||||
|
expect(options.rewriteRequestPath('/files/b1/test.webp')).toBe('b1/test.webp')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it('GET /files/:bucket/:key handles missing contentType', async () => {
|
it('PUT /files/:bucket/:key calls s3Service.putObject', async () => {
|
||||||
vi.spyOn(s3Service, 'headObject').mockResolvedValue({
|
const { default: route } = await import('./s3')
|
||||||
key: 'test',
|
const app = new Hono().route('/files', route)
|
||||||
size: 100,
|
const mockPut = vi.spyOn(s3Service, 'putObject').mockResolvedValue(undefined)
|
||||||
lastModified: new Date(),
|
const body = new TextEncoder().encode('test-data')
|
||||||
contentType: 'application/octet-stream',
|
|
||||||
eTag: '"test"',
|
|
||||||
})
|
|
||||||
vi.spyOn(s3Service, 'getObject').mockResolvedValue({
|
|
||||||
buffer: Buffer.from('test'),
|
|
||||||
contentType: 'application/octet-stream',
|
|
||||||
})
|
|
||||||
|
|
||||||
const res = await app.request('/files/b1/test')
|
const res = await app.request('/files/b1/test.webp', {
|
||||||
|
method: 'PUT',
|
||||||
|
body,
|
||||||
|
})
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.headers.get('Content-Type')).toBe('application/octet-stream')
|
expect(await res.text()).toBe('OK')
|
||||||
})
|
expect(mockPut).toHaveBeenCalledWith('b1', 'test.webp', expect.any(Buffer), body.byteLength)
|
||||||
|
|
||||||
it('GET /files/:bucket/:key returns 404 if not found', async () => {
|
|
||||||
vi.spyOn(s3Service, 'headObject').mockRejectedValue(new Error('NoSuchKey'))
|
|
||||||
|
|
||||||
const res = await app.request('/files/b1/not-found')
|
|
||||||
|
|
||||||
expect(res.status).toBe(404)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
+8
-24
@@ -1,31 +1,15 @@
|
|||||||
import { Hono } from 'hono'
|
import { Hono } from 'hono'
|
||||||
import { s3Service } from '@/services/s3/s3'
|
import { s3Service } from '@/services/s3/s3'
|
||||||
|
import { serveStatic } from 'hono/bun'
|
||||||
|
|
||||||
const route = new Hono()
|
const route = new Hono()
|
||||||
.get('/:bucket/:key{.+}', async (c) => {
|
.get(
|
||||||
const bucket = c.req.param('bucket')
|
'/*',
|
||||||
const key = c.req.param('key')
|
serveStatic({
|
||||||
|
root: './data',
|
||||||
try {
|
rewriteRequestPath: (path) => path.replace(/^\/files\//, ''),
|
||||||
const info = await s3Service.headObject(bucket, key)
|
}),
|
||||||
const { buffer: data, contentType } = await s3Service.getObject(bucket, key)
|
)
|
||||||
|
|
||||||
if (contentType) {
|
|
||||||
c.header('Content-Type', contentType)
|
|
||||||
} else if (info.contentType) {
|
|
||||||
c.header('Content-Type', info.contentType)
|
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
return c.body(data as any)
|
|
||||||
} catch (e: unknown) {
|
|
||||||
const error = e as Error
|
|
||||||
if (error.message?.includes('NoSuchKey')) {
|
|
||||||
return c.text('Not Found', 404)
|
|
||||||
}
|
|
||||||
return c.text(error.message || 'Internal Server Error', 500)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.put('/:bucket/:key{.+}', async (c) => {
|
.put('/:bucket/:key{.+}', async (c) => {
|
||||||
const bucket = c.req.param('bucket')
|
const bucket = c.req.param('bucket')
|
||||||
const key = c.req.param('key')
|
const key = c.req.param('key')
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
|
import { prisma } from '@/db'
|
||||||
|
import { WorkflowTaskStatus, WorkflowTaskType } from '@/generated/prisma/client'
|
||||||
|
import '@/prisma-json-types'
|
||||||
import { exec } from 'child_process'
|
import { exec } from 'child_process'
|
||||||
import { promisify } from 'util'
|
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import sharp from 'sharp'
|
import sharp from 'sharp'
|
||||||
import { prisma } from '@/db'
|
import { promisify } from 'util'
|
||||||
import { WorkflowTaskType, WorkflowTaskStatus } from '@/generated/prisma/client'
|
|
||||||
import '@/prisma-json-types'
|
|
||||||
|
|
||||||
const execAsync = promisify(exec)
|
const execAsync = promisify(exec)
|
||||||
|
|
||||||
@@ -267,7 +267,7 @@ export class TranscodeService {
|
|||||||
args.push('-c:a', 'aac', '-b:a', '128k')
|
args.push('-c:a', 'aac', '-b:a', '128k')
|
||||||
}
|
}
|
||||||
|
|
||||||
args.push('-max_muxing_queue_size', '1024', `"${params.outputFile}"`)
|
args.push('-movflags', '+faststart', '-max_muxing_queue_size', '1024', `"${params.outputFile}"`)
|
||||||
|
|
||||||
await execAsync(`ffmpeg -y ${args.join(' ')}`)
|
await execAsync(`ffmpeg -y ${args.join(' ')}`)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user