mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 09:29:23 +00:00
feat(transcode): add HLS live streaming support (#436)
* feat(transcode): add HLS live streaming support * fix(hls): resolve version stacks for public HLS and add workflow E2E test
This commit is contained in:
@@ -774,4 +774,47 @@ describe('file api', () => {
|
||||
|
||||
mockGetProjectIds.mockRestore()
|
||||
})
|
||||
|
||||
describe('GET /files/:fileId/m3u8 routes', () => {
|
||||
it('returns master playlist with correct content-type', async () => {
|
||||
const mockMaster = vi
|
||||
.spyOn(assetService, 'getHlsMasterPlaylist')
|
||||
.mockResolvedValue('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=4628000\n1080p/index.m3u8\n')
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/asset-1/m3u8/master.m3u8')
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
|
||||
const text = await res.text()
|
||||
expect(text).toContain('#EXTM3U')
|
||||
expect(text).toContain('1080p/index.m3u8')
|
||||
expect(mockMaster).toHaveBeenCalledWith({ assetId: 'asset-1' })
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user1', name: 'Test User' },
|
||||
permission: Permission.Read,
|
||||
type: 'asset',
|
||||
id: 'asset-1',
|
||||
})
|
||||
mockMaster.mockRestore()
|
||||
})
|
||||
|
||||
it('returns variant playlist with rewritten URLs', async () => {
|
||||
const mockVariant = vi
|
||||
.spyOn(assetService, 'getHlsVariantPlaylist')
|
||||
.mockResolvedValue(
|
||||
'#EXTM3U\n#EXT-X-MAP:URI="http://presigned/init.mp4"\nhttp://presigned/seg0.m4s\n',
|
||||
)
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/asset-1/m3u8/1080p/index.m3u8')
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
|
||||
const text = await res.text()
|
||||
expect(text).toContain('http://presigned/init.mp4')
|
||||
expect(mockVariant).toHaveBeenCalledWith({ assetId: 'asset-1', resolution: '1080p' })
|
||||
mockVariant.mockRestore()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -480,5 +480,70 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
return c.json({ url })
|
||||
},
|
||||
)
|
||||
.get('/files/:fileId/m3u8', async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
|
||||
const playlist = await assetService.getHlsMasterPlaylist({ assetId: fileId })
|
||||
c.header('Content-Type', 'application/vnd.apple.mpegurl')
|
||||
return c.body(playlist)
|
||||
})
|
||||
.get('/files/:fileId/m3u8/master.m3u8', async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
|
||||
const playlist = await assetService.getHlsMasterPlaylist({ assetId: fileId })
|
||||
c.header('Content-Type', 'application/vnd.apple.mpegurl')
|
||||
return c.body(playlist)
|
||||
})
|
||||
.get('/files/:fileId/m3u8/:resolution', async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
let resolution = c.req.param('resolution')
|
||||
if (resolution.endsWith('.m3u8')) {
|
||||
resolution = resolution.replace(/\.m3u8$/, '')
|
||||
}
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
|
||||
const playlist = await assetService.getHlsVariantPlaylist({ assetId: fileId, resolution })
|
||||
c.header('Content-Type', 'application/vnd.apple.mpegurl')
|
||||
return c.body(playlist)
|
||||
})
|
||||
.get('/files/:fileId/m3u8/:resolution/index.m3u8', async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const resolution = c.req.param('resolution')
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Asset,
|
||||
id: fileId,
|
||||
})
|
||||
|
||||
const playlist = await assetService.getHlsVariantPlaylist({ assetId: fileId, resolution })
|
||||
c.header('Content-Type', 'application/vnd.apple.mpegurl')
|
||||
return c.body(playlist)
|
||||
})
|
||||
|
||||
export default route
|
||||
|
||||
@@ -38,6 +38,8 @@ import type { AssetInfo } from '@shumai/dtos'
|
||||
async function applyWatermarkToAssetMedia(
|
||||
asset: AssetInfo,
|
||||
watermarkMedia: PrismaJson.MediaInfo | null,
|
||||
shareId?: string,
|
||||
password?: string,
|
||||
): Promise<AssetInfo> {
|
||||
if (!asset.media) return asset
|
||||
if (asset.proxyType !== 'video' && asset.proxyType !== 'image') return asset
|
||||
@@ -74,6 +76,19 @@ async function applyWatermarkToAssetMedia(
|
||||
)
|
||||
: []
|
||||
|
||||
if (watermarkMedia?.isHls && watermarkMedia.hls?.key && shareId) {
|
||||
const pwQuery = password ? `?p=${encodeURIComponent(password)}` : ''
|
||||
updatedMedia.isHls = true
|
||||
updatedMedia.hls = {
|
||||
key: watermarkMedia.hls.key,
|
||||
url: `/api/shares/${shareId}/files/${asset.id}/m3u8/master.m3u8${pwQuery}`,
|
||||
resolutions: watermarkMedia.hls.resolutions,
|
||||
}
|
||||
} else if (watermarkMedia) {
|
||||
updatedMedia.isHls = false
|
||||
delete updatedMedia.hls
|
||||
}
|
||||
|
||||
return {
|
||||
...asset,
|
||||
media: updatedMedia,
|
||||
@@ -170,7 +185,8 @@ const route = app
|
||||
zValidator('query', sharedChildrenRequestSchema),
|
||||
async (c) => {
|
||||
const folderId = c.req.param('folderId')
|
||||
const password = c.req.header('x-share-password')
|
||||
const password =
|
||||
c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
const req = c.req.valid('query')
|
||||
|
||||
try {
|
||||
@@ -197,7 +213,7 @@ const route = app
|
||||
// Resolve symlink targets once and reuse for the media lookup, so each
|
||||
// item costs a single query instead of two.
|
||||
const targetIds = await Promise.all(
|
||||
res.data.map((item) => assetService.resolveTargetAssetId(item.id)),
|
||||
res.data.map((item) => assetService.resolveLatestVersionId(item.id)),
|
||||
)
|
||||
const wfMap = await watermarkService.getCompletedWatermarkMediaMap(
|
||||
targetIds,
|
||||
@@ -206,9 +222,21 @@ const route = app
|
||||
|
||||
res.data = await Promise.all(
|
||||
res.data.map(async (item, index) =>
|
||||
applyWatermarkToAssetMedia(item, wfMap.get(targetIds[index]) ?? null),
|
||||
applyWatermarkToAssetMedia(
|
||||
item,
|
||||
wfMap.get(targetIds[index]) ?? null,
|
||||
shareLink.id,
|
||||
password,
|
||||
),
|
||||
),
|
||||
)
|
||||
} else if (res.data.length > 0) {
|
||||
const pwQuery = password ? `?p=${encodeURIComponent(password)}` : ''
|
||||
for (const item of res.data) {
|
||||
if (item.media?.isHls && item.media.hls?.key) {
|
||||
item.media.hls.url = `/api/shares/${shareLink.id}/files/${item.id}/m3u8/master.m3u8${pwQuery}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c.json(res)
|
||||
@@ -219,7 +247,7 @@ const route = app
|
||||
)
|
||||
.get('/shares/:shareId/files/:fileId', async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const password = c.req.header('x-share-password')
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
@@ -232,12 +260,20 @@ const route = app
|
||||
shareLink.watermarkConfigId &&
|
||||
(asset.proxyType === 'video' || asset.proxyType === 'image')
|
||||
) {
|
||||
const targetAssetId = await assetService.resolveTargetAssetId(fileId)
|
||||
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
|
||||
const wfMap = await watermarkService.getCompletedWatermarkMediaMap(
|
||||
[targetAssetId],
|
||||
shareLink.watermarkConfigId,
|
||||
)
|
||||
asset = await applyWatermarkToAssetMedia(asset, wfMap.get(targetAssetId) ?? null)
|
||||
asset = await applyWatermarkToAssetMedia(
|
||||
asset,
|
||||
wfMap.get(targetAssetId) ?? null,
|
||||
shareLink.id,
|
||||
password,
|
||||
)
|
||||
} else if (asset.media?.isHls && asset.media.hls?.key) {
|
||||
const pwQuery = password ? `?p=${encodeURIComponent(password)}` : ''
|
||||
asset.media.hls.url = `/api/shares/${shareLink.id}/files/${asset.id}/m3u8/master.m3u8${pwQuery}`
|
||||
}
|
||||
|
||||
return c.json(asset)
|
||||
@@ -360,5 +396,92 @@ const route = app
|
||||
}
|
||||
},
|
||||
)
|
||||
.get('/shares/:shareId/files/:fileId/m3u8', async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
|
||||
let playlist = await assetService.getHlsMasterPlaylist({
|
||||
assetId: targetAssetId,
|
||||
watermarkConfigId: shareLink.watermarkConfigId,
|
||||
})
|
||||
if (password) {
|
||||
playlist = playlist.replace(
|
||||
/([0-9]+p\/index\.m3u8)/g,
|
||||
`$1?p=${encodeURIComponent(password)}`,
|
||||
)
|
||||
}
|
||||
c.header('Content-Type', 'application/vnd.apple.mpegurl')
|
||||
return c.body(playlist)
|
||||
} catch (err) {
|
||||
return handlePublicShareError(c, err)
|
||||
}
|
||||
})
|
||||
.get('/shares/:shareId/files/:fileId/m3u8/master.m3u8', async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
|
||||
let playlist = await assetService.getHlsMasterPlaylist({
|
||||
assetId: targetAssetId,
|
||||
watermarkConfigId: shareLink.watermarkConfigId,
|
||||
})
|
||||
if (password) {
|
||||
playlist = playlist.replace(
|
||||
/([0-9]+p\/index\.m3u8)/g,
|
||||
`$1?p=${encodeURIComponent(password)}`,
|
||||
)
|
||||
}
|
||||
c.header('Content-Type', 'application/vnd.apple.mpegurl')
|
||||
return c.body(playlist)
|
||||
} catch (err) {
|
||||
return handlePublicShareError(c, err)
|
||||
}
|
||||
})
|
||||
.get('/shares/:shareId/files/:fileId/m3u8/:resolution', async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
let resolution = c.req.param('resolution')
|
||||
if (resolution.endsWith('.m3u8')) {
|
||||
resolution = resolution.replace(/\.m3u8$/, '')
|
||||
}
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
|
||||
const playlist = await assetService.getHlsVariantPlaylist({
|
||||
assetId: targetAssetId,
|
||||
resolution,
|
||||
watermarkConfigId: shareLink.watermarkConfigId,
|
||||
})
|
||||
c.header('Content-Type', 'application/vnd.apple.mpegurl')
|
||||
return c.body(playlist)
|
||||
} catch (err) {
|
||||
return handlePublicShareError(c, err)
|
||||
}
|
||||
})
|
||||
.get('/shares/:shareId/files/:fileId/m3u8/:resolution/index.m3u8', async (c) => {
|
||||
const fileId = c.req.param('fileId')
|
||||
const resolution = c.req.param('resolution')
|
||||
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
|
||||
|
||||
try {
|
||||
const shareLink = await shareService.verifyPublicAccess(fileId, password)
|
||||
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
|
||||
const playlist = await assetService.getHlsVariantPlaylist({
|
||||
assetId: targetAssetId,
|
||||
resolution,
|
||||
watermarkConfigId: shareLink.watermarkConfigId,
|
||||
})
|
||||
c.header('Content-Type', 'application/vnd.apple.mpegurl')
|
||||
return c.body(playlist)
|
||||
} catch (err) {
|
||||
return handlePublicShareError(c, err)
|
||||
}
|
||||
})
|
||||
|
||||
export default route
|
||||
|
||||
@@ -866,4 +866,81 @@ describe('Share API', () => {
|
||||
expect(getDownloadUrlSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /shares/:shareId/files/:fileId/m3u8 routes', () => {
|
||||
test('returns master playlist with query password preserved in variant URLs', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
id: 'share1',
|
||||
watermarkConfigId: null,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
vi.spyOn(assetService, 'getHlsMasterPlaylist').mockResolvedValue(
|
||||
'#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=4628000\n1080p/index.m3u8\n',
|
||||
)
|
||||
|
||||
const res = await app.request('/shares/share1/files/file1/m3u8/master.m3u8?p=secret123')
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
|
||||
const text = await res.text()
|
||||
expect(text).toContain('1080p/index.m3u8?p=secret123')
|
||||
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('file1', 'secret123')
|
||||
expect(assetService.getHlsMasterPlaylist).toHaveBeenCalledWith({
|
||||
assetId: 'file1',
|
||||
watermarkConfigId: null,
|
||||
})
|
||||
})
|
||||
|
||||
test('returns variant playlist with rewritten segment URLs', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
id: 'share1',
|
||||
watermarkConfigId: 'wm1',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
vi.spyOn(assetService, 'getHlsVariantPlaylist').mockResolvedValue(
|
||||
'#EXTM3U\n#EXT-X-MAP:URI="http://presigned/init.mp4"\nhttp://presigned/seg0.m4s\n',
|
||||
)
|
||||
|
||||
const res = await app.request('/shares/share1/files/file1/m3u8/1080p/index.m3u8')
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
|
||||
const text = await res.text()
|
||||
expect(text).toContain('http://presigned/init.mp4')
|
||||
expect(assetService.getHlsVariantPlaylist).toHaveBeenCalledWith({
|
||||
assetId: 'file1',
|
||||
resolution: '1080p',
|
||||
watermarkConfigId: 'wm1',
|
||||
})
|
||||
})
|
||||
|
||||
test('returns 401 when password is invalid', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockRejectedValue(
|
||||
new ShareLinkPasswordInvalidError('Invalid password'),
|
||||
)
|
||||
|
||||
const res = await app.request('/shares/share1/files/file1/m3u8/master.m3u8?p=wrong')
|
||||
expect(res.status).toBe(401)
|
||||
const body = await res.json()
|
||||
expect(body.error).toBe('Unauthorized')
|
||||
})
|
||||
|
||||
test('resolves latest version id when fileId is a symlink to a version stack', async () => {
|
||||
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
|
||||
id: 'share1',
|
||||
watermarkConfigId: null,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any)
|
||||
vi.spyOn(assetService, 'resolveLatestVersionId').mockResolvedValue('child-version-file-id')
|
||||
vi.spyOn(assetService, 'getHlsMasterPlaylist').mockResolvedValue(
|
||||
'#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=4628000\n1080p/index.m3u8\n',
|
||||
)
|
||||
|
||||
const res = await app.request('/shares/share1/files/symlink1/m3u8/master.m3u8')
|
||||
expect(res.status).toBe(200)
|
||||
expect(assetService.resolveLatestVersionId).toHaveBeenCalledWith('symlink1')
|
||||
expect(assetService.getHlsMasterPlaylist).toHaveBeenCalledWith({
|
||||
assetId: 'child-version-file-id',
|
||||
watermarkConfigId: null,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user