diff --git a/src/api/s3.test.ts b/src/api/s3.test.ts index c79b71a8..c715103d 100644 --- a/src/api/s3.test.ts +++ b/src/api/s3.test.ts @@ -1,59 +1,57 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' import { s3Service } from '@/services/s3/s3' -import route from './s3' import { Hono } from 'hono' -describe('S3 API', () => { - const app = new Hono().route('/files', route) +const { mockServeStatic } = vi.hoisted(() => ({ + // 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(() => { vi.clearAllMocks() }) - it('GET /files/:bucket/:key sets Content-Type', async () => { - const mockHead = vi.spyOn(s3Service, 'headObject').mockResolvedValue({ - key: 'test.webp', - size: 100, - lastModified: new Date(), - contentType: 'image/webp', - eTag: '"test"', - }) - const mockGet = vi - .spyOn(s3Service, 'getObject') - .mockResolvedValue({ buffer: Buffer.from('test'), contentType: 'image/webp' }) + it('GET /files/* calls serveStatic with correct options', async () => { + const { default: route } = await import('./s3') + const app = new Hono().route('/files', route) const res = await app.request('/files/b1/test.webp') expect(res.status).toBe(200) - expect(res.headers.get('Content-Type')).toBe('image/webp') - expect(mockHead).toHaveBeenCalledWith('b1', 'test.webp') - expect(mockGet).toHaveBeenCalledWith('b1', 'test.webp') + expect(await res.text()).toBe('static-file') + expect(mockServeStatic).toHaveBeenCalledWith( + 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 () => { - vi.spyOn(s3Service, 'headObject').mockResolvedValue({ - key: 'test', - size: 100, - lastModified: new Date(), - contentType: 'application/octet-stream', - eTag: '"test"', - }) - vi.spyOn(s3Service, 'getObject').mockResolvedValue({ - buffer: Buffer.from('test'), - contentType: 'application/octet-stream', - }) + it('PUT /files/:bucket/:key calls s3Service.putObject', async () => { + const { default: route } = await import('./s3') + const app = new Hono().route('/files', route) + const mockPut = vi.spyOn(s3Service, 'putObject').mockResolvedValue(undefined) + const body = new TextEncoder().encode('test-data') - 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.headers.get('Content-Type')).toBe('application/octet-stream') - }) - - 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) + expect(await res.text()).toBe('OK') + expect(mockPut).toHaveBeenCalledWith('b1', 'test.webp', expect.any(Buffer), body.byteLength) }) }) diff --git a/src/api/s3.ts b/src/api/s3.ts index 6bc3febc..c87b4b06 100644 --- a/src/api/s3.ts +++ b/src/api/s3.ts @@ -1,31 +1,15 @@ import { Hono } from 'hono' import { s3Service } from '@/services/s3/s3' +import { serveStatic } from 'hono/bun' const route = new Hono() - .get('/:bucket/:key{.+}', async (c) => { - const bucket = c.req.param('bucket') - const key = c.req.param('key') - - try { - 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) - } - }) + .get( + '/*', + serveStatic({ + root: './data', + rewriteRequestPath: (path) => path.replace(/^\/files\//, ''), + }), + ) .put('/:bucket/:key{.+}', async (c) => { const bucket = c.req.param('bucket') const key = c.req.param('key') diff --git a/src/transcode/transcode.ts b/src/transcode/transcode.ts index 0e042426..f35773a8 100644 --- a/src/transcode/transcode.ts +++ b/src/transcode/transcode.ts @@ -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 { promisify } from 'util' import * as fs from 'fs' import * as os from 'os' import * as path from 'path' import sharp from 'sharp' -import { prisma } from '@/db' -import { WorkflowTaskType, WorkflowTaskStatus } from '@/generated/prisma/client' -import '@/prisma-json-types' +import { promisify } from 'util' const execAsync = promisify(exec) @@ -267,7 +267,7 @@ export class TranscodeService { 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(' ')}`) }