feat(team): add integrations settings with turntable and gotenberg (#471)

* feat(team): add integrations settings with turntable and gotenberg

* fix(upload): pass teamId to gotenberg availability check during upload preflight
This commit is contained in:
Yiling-J
2026-10-12 15:38:15 +08:00
committed by GitHub
parent 43a2eced75
commit f952318b34
23 changed files with 1615 additions and 155 deletions
+109
View File
@@ -60,6 +60,14 @@ vi.mock('@shumai/core/src/turntable/turntable', () => ({
},
}))
vi.mock('@shumai/core/src/gotenberg/gotenberg', () => ({
gotenbergService: {
getSettings: vi.fn(),
updateSettings: vi.fn(),
testConnection: vi.fn(),
},
}))
describe('team api', () => {
const app = new Hono().use('*', authMiddleware).route('/', teamRoute)
let mockCreateTeam: any // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -748,5 +756,106 @@ describe('team api', () => {
expect(data.ok).toBe(true)
expect(data.blender).toBe('5.2.0')
})
it('GET /teams/:teamId/gotenberg-settings returns gotenberg settings', async () => {
const { gotenbergService } = await import('@shumai/core/src/gotenberg/gotenberg')
vi.mocked(gotenbergService.getSettings).mockResolvedValue({
url: 'http://localhost:3000',
username: 'user',
hasPassword: true,
isEnvConfigured: false,
})
const res = await app.request('/teams/t1/gotenberg-settings')
expect(res.status).toBe(200)
const data = await res.json()
expect(data).toEqual({
url: 'http://localhost:3000',
username: 'user',
hasPassword: true,
isEnvConfigured: false,
})
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({
permission: Permission.Read,
type: ResourceType.Team,
id: 't1',
}),
)
})
it('PUT /teams/:teamId/gotenberg-settings updates settings when not env configured', async () => {
const { gotenbergService } = await import('@shumai/core/src/gotenberg/gotenberg')
vi.mocked(gotenbergService.updateSettings).mockResolvedValue({
url: 'http://custom:3000',
username: 'newuser',
hasPassword: true,
isEnvConfigured: false,
})
const res = await app.request('/teams/t1/gotenberg-settings', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
url: 'http://custom:3000',
username: 'newuser',
password: 'secretpassword',
}),
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data.url).toBe('http://custom:3000')
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({
permission: Permission.Admin,
type: ResourceType.Team,
id: 't1',
}),
)
})
it('PUT /teams/:teamId/gotenberg-settings returns 400 when env configured', async () => {
const { gotenbergService } = await import('@shumai/core/src/gotenberg/gotenberg')
vi.mocked(gotenbergService.updateSettings).mockRejectedValue(
new Error(
'Gotenberg settings are configured via environment variables and cannot be edited',
),
)
const res = await app.request('/teams/t1/gotenberg-settings', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
url: 'http://custom:3000',
}),
})
expect(res.status).toBe(400)
const data = await res.json()
expect(data.message).toContain('configured via environment variables')
})
it('POST /teams/:teamId/gotenberg-settings/test tests connection', async () => {
const { gotenbergService } = await import('@shumai/core/src/gotenberg/gotenberg')
vi.mocked(gotenbergService.testConnection).mockResolvedValue({
ok: true,
version: '8.3.0',
})
const res = await app.request('/teams/t1/gotenberg-settings/test', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
url: 'http://custom:3000',
}),
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data.ok).toBe(true)
expect(data.version).toBe('8.3.0')
})
})
})
+59
View File
@@ -11,6 +11,7 @@ import {
listMembersQuerySchema,
updateSandboxSettingsRequestSchema,
updateTurntableSettingsRequestSchema,
updateGotenbergSettingsRequestSchema,
updateMeRequestSchema,
updateTeamMemberRoleRequestSchema,
createApiTokenRequestSchema,
@@ -25,6 +26,7 @@ import { apiTokenService } from '@shumai/core/src/user/api-token'
import { aiUsageService } from '@shumai/core/src/ai-usage/ai-usage'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
import { turntableService } from '@shumai/core/src/turntable/turntable'
import { gotenbergService } from '@shumai/core/src/gotenberg/gotenberg'
import { NotificationType } from '@shumai/db'
import type { Prisma } from '@shumai/db'
@@ -324,6 +326,63 @@ const route = new Hono<{ Variables: { user: User } }>()
return c.json(res)
},
)
.get('/teams/:teamId/gotenberg-settings', async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
const settings = await gotenbergService.getSettings(teamId)
return c.json(settings)
})
.put(
'/teams/:teamId/gotenberg-settings',
zValidator('json', updateGotenbergSettingsRequestSchema),
async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const req = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Admin,
type: ResourceType.Team,
id: teamId,
})
try {
const settings = await gotenbergService.updateSettings(teamId, req)
return c.json(settings)
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
return c.json({ message }, 400)
}
},
)
.post(
'/teams/:teamId/gotenberg-settings/test',
zValidator('json', updateGotenbergSettingsRequestSchema.partial()),
async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const req = c.req.valid('json')
await authzService.hasPermission({
user,
permission: Permission.Admin,
type: ResourceType.Team,
id: teamId,
})
const res = await gotenbergService.testConnection(req, teamId)
return c.json(res)
},
)
.get('/teams/:teamId/user-metadata', async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
@@ -2,9 +2,12 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { GotenbergService } from './gotenberg'
describe('GotenbergService', () => {
setupTestDbHooks()
const originalEnv = process.env
let tmpDir: string
let testFilePath: string
@@ -216,4 +219,148 @@ describe('GotenbergService', () => {
expect(indexHtmlText).toContain('{{ toHTML "file.md" }}')
})
})
describe('team settings & fallback', () => {
it('resolves config from team settings when env is not set', async () => {
delete process.env.GOTENBERG_URL
const team = await prisma.team.create({
data: {
name: 'Gotenberg Team',
settings: {
gotenberg: {
url: 'http://custom-gotenberg:4000',
username: 'team-user',
password: 'team-password',
},
},
},
})
const service = new GotenbergService()
const config = await service.resolveConfig(team.id)
expect(config).toEqual({
url: 'http://custom-gotenberg:4000',
username: 'team-user',
password: 'team-password',
isEnv: false,
})
})
it('prioritizes environment variables over team settings', async () => {
process.env.GOTENBERG_URL = 'http://env-gotenberg:3000'
const team = await prisma.team.create({
data: {
name: 'Gotenberg Team',
settings: {
gotenberg: {
url: 'http://custom-gotenberg:4000',
},
},
},
})
const service = new GotenbergService()
const config = await service.resolveConfig(team.id)
expect(config.url).toBe('http://env-gotenberg:3000')
expect(config.isEnv).toBe(true)
})
it('getSettings returns masked settings response', async () => {
delete process.env.GOTENBERG_URL
const team = await prisma.team.create({
data: {
name: 'Gotenberg Team',
settings: {
gotenberg: {
url: 'http://custom-gotenberg:4000',
username: 'team-user',
password: 'team-password',
},
},
},
})
const service = new GotenbergService()
const settings = await service.getSettings(team.id)
expect(settings).toEqual({
url: 'http://custom-gotenberg:4000',
username: 'team-user',
hasPassword: true,
isEnvConfigured: false,
})
})
it('updateSettings updates team settings and rejects edits when env is set', async () => {
delete process.env.GOTENBERG_URL
const team = await prisma.team.create({
data: { name: 'Gotenberg Team' },
})
const service = new GotenbergService()
const updated = await service.updateSettings(team.id, {
url: 'http://new-gotenberg:3000',
username: 'admin',
password: 'secret-password',
})
expect(updated.url).toBe('http://new-gotenberg:3000')
expect(updated.username).toBe('admin')
expect(updated.hasPassword).toBe(true)
// When env is set, reject update
process.env.GOTENBERG_URL = 'http://env-gotenberg:3000'
await expect(service.updateSettings(team.id, { url: 'http://another:3000' })).rejects.toThrow(
'Gotenberg settings are configured via environment variables and cannot be edited',
)
})
})
describe('testConnection', () => {
it('returns error when url is empty and no team settings exist', async () => {
delete process.env.GOTENBERG_URL
const service = new GotenbergService()
const result = await service.testConnection({})
expect(result.ok).toBe(false)
expect(result.error).toBe('URL is required')
})
it('successfully connects to /health and enriches with /version', async () => {
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(async (url) => {
const urlStr = String(url)
if (urlStr.endsWith('/health')) {
return { ok: true, status: 200 } as Response
}
if (urlStr.endsWith('/version')) {
return {
ok: true,
status: 200,
json: async () => ({ version: '8.3.0' }),
} as Response
}
return { ok: false, status: 404 } as Response
})
const service = new GotenbergService()
const result = await service.testConnection({
url: 'http://gotenberg.local:3000',
username: 'test-user',
password: 'test-pass',
})
expect(result.ok).toBe(true)
expect(result.version).toBe('8.3.0')
expect(fetchSpy).toHaveBeenCalledWith(
'http://gotenberg.local:3000/health',
expect.objectContaining({
method: 'GET',
headers: expect.objectContaining({
Authorization: expect.stringMatching(/^Basic /),
}),
}),
)
})
})
})
+126 -29
View File
@@ -1,40 +1,135 @@
import * as fs from 'fs'
import type { GotenbergSettingsResponse, UpdateGotenbergSettingsRequest } from '@shumai/dtos'
import {
IntegrationDefinition,
resolveIntegrationConfig,
getIntegrationSettings,
updateIntegrationSettings,
resolveTestConnectionConfig,
getIntegrationBasicAuthHeader,
ResolvedIntegrationConfig,
} from '@shumai/core/src/integrations/helper'
export const GOTENBERG_INTEGRATION_DEF: IntegrationDefinition<'gotenberg'> = {
name: 'Gotenberg',
settingsKey: 'gotenberg',
envKeys: {
url: ['GOTENBERG_URL'],
username: ['GOTENBERG_BASIC_AUTH_USERNAME'],
password: ['GOTENBERG_BASIC_AUTH_PASSWORD'],
},
}
export interface GotenbergConvertOptions {
landscape?: boolean
teamId?: string
}
export class GotenbergService {
private getUrl(): string | undefined {
const url = process.env.GOTENBERG_URL?.trim()
return url ? url.replace(/\/+$/, '') : undefined
async resolveConfig(teamId?: string): Promise<ResolvedIntegrationConfig> {
return resolveIntegrationConfig(GOTENBERG_INTEGRATION_DEF, teamId)
}
private getAuthHeader(): string | undefined {
const username = process.env.GOTENBERG_BASIC_AUTH_USERNAME
const password = process.env.GOTENBERG_BASIC_AUTH_PASSWORD
private getAuthHeader(config: { username?: string; password?: string }): string | undefined {
return getIntegrationBasicAuthHeader(config)
}
if (username && password) {
const credentials = Buffer.from(`${username}:${password}`).toString('base64')
return `Basic ${credentials}`
async getSettings(teamId: string): Promise<GotenbergSettingsResponse> {
return getIntegrationSettings(GOTENBERG_INTEGRATION_DEF, teamId)
}
async updateSettings(
teamId: string,
req: UpdateGotenbergSettingsRequest,
): Promise<GotenbergSettingsResponse> {
return updateIntegrationSettings(GOTENBERG_INTEGRATION_DEF, teamId, req)
}
async testConnection(
config?: UpdateGotenbergSettingsRequest,
teamId?: string,
): Promise<{ ok: boolean; version?: string; error?: string }> {
const target = await resolveTestConnectionConfig(GOTENBERG_INTEGRATION_DEF, config, teamId)
if (!target.resolvedUrl) {
return { ok: false, error: 'URL is required' }
}
const cleanUrl = target.resolvedUrl.replace(/\/+$/, '')
try {
const headers: Record<string, string> = {}
const authHeader = this.getAuthHeader({
username: target.resolvedUsername,
password: target.resolvedPassword,
})
if (authHeader) {
headers['Authorization'] = authHeader
}
const response = await fetch(`${cleanUrl}/health`, {
method: 'GET',
headers,
signal: AbortSignal.timeout(5000),
})
if (!response.ok) {
const errText = await response.text().catch(() => '')
return {
ok: false,
error: `Health check returned status ${response.status}: ${errText || response.statusText}`,
}
}
// Optionally try /version to obtain version string
let version: string | undefined
try {
const versionRes = await fetch(`${cleanUrl}/version`, {
method: 'GET',
headers,
signal: AbortSignal.timeout(2000),
})
if (versionRes.ok) {
const vData = (await versionRes.json().catch(() => null)) as
| { version?: string }
| string
| null
if (vData && typeof vData === 'object' && 'version' in vData) {
version = String(vData.version)
} else if (typeof vData === 'string') {
version = vData
}
}
} catch {
// Version endpoint is optional in some Gotenberg versions
}
return {
ok: true,
version,
}
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
return {
ok: false,
error: msg,
}
}
return undefined
}
async isAvailable(): Promise<boolean> {
const baseUrl = this.getUrl()
if (!baseUrl) {
async isAvailable(teamId?: string): Promise<boolean> {
const config = await this.resolveConfig(teamId)
if (!config.url) {
return false
}
try {
const headers: Record<string, string> = {}
const authHeader = this.getAuthHeader()
const authHeader = this.getAuthHeader(config)
if (authHeader) {
headers['Authorization'] = authHeader
}
const response = await fetch(`${baseUrl}/health`, {
const response = await fetch(`${config.url}/health`, {
method: 'GET',
headers,
signal: AbortSignal.timeout(3000),
@@ -50,9 +145,11 @@ export class GotenbergService {
filePath: string,
filename: string,
options: GotenbergConvertOptions = {},
teamId?: string,
): Promise<Buffer> {
const baseUrl = this.getUrl()
if (!baseUrl) {
const effectiveTeamId = teamId ?? options.teamId
const config = await this.resolveConfig(effectiveTeamId)
if (!config.url) {
throw new Error('Gotenberg URL is not configured.')
}
@@ -65,12 +162,12 @@ export class GotenbergService {
}
const headers: Record<string, string> = {}
const authHeader = this.getAuthHeader()
const authHeader = this.getAuthHeader(config)
if (authHeader) {
headers['Authorization'] = authHeader
}
const response = await fetch(`${baseUrl}/forms/libreoffice/convert`, {
const response = await fetch(`${config.url}/forms/libreoffice/convert`, {
method: 'POST',
headers,
body: formData,
@@ -88,9 +185,9 @@ export class GotenbergService {
return Buffer.from(arrayBuffer)
}
async convertHtmlToPdf(filePath: string, filename: string): Promise<Buffer> {
const baseUrl = this.getUrl()
if (!baseUrl) {
async convertHtmlToPdf(filePath: string, filename: string, teamId?: string): Promise<Buffer> {
const config = await this.resolveConfig(teamId)
if (!config.url) {
throw new Error('Gotenberg URL is not configured.')
}
@@ -103,12 +200,12 @@ export class GotenbergService {
formData.append('files', new Blob([fileBytes]), targetFilename)
const headers: Record<string, string> = {}
const authHeader = this.getAuthHeader()
const authHeader = this.getAuthHeader(config)
if (authHeader) {
headers['Authorization'] = authHeader
}
const response = await fetch(`${baseUrl}/forms/chromium/convert/html`, {
const response = await fetch(`${config.url}/forms/chromium/convert/html`, {
method: 'POST',
headers,
body: formData,
@@ -126,9 +223,9 @@ export class GotenbergService {
return Buffer.from(arrayBuffer)
}
async convertMarkdownToPdf(filePath: string, filename: string): Promise<Buffer> {
const baseUrl = this.getUrl()
if (!baseUrl) {
async convertMarkdownToPdf(filePath: string, filename: string, teamId?: string): Promise<Buffer> {
const config = await this.resolveConfig(teamId)
if (!config.url) {
throw new Error('Gotenberg URL is not configured.')
}
@@ -151,12 +248,12 @@ export class GotenbergService {
formData.append('files', new Blob([fileBytes]), mdFilename)
const headers: Record<string, string> = {}
const authHeader = this.getAuthHeader()
const authHeader = this.getAuthHeader(config)
if (authHeader) {
headers['Authorization'] = authHeader
}
const response = await fetch(`${baseUrl}/forms/chromium/convert/markdown`, {
const response = await fetch(`${config.url}/forms/chromium/convert/markdown`, {
method: 'POST',
headers,
body: formData,
+1
View File
@@ -32,3 +32,4 @@ export * from './media-generation/media-generation'
export * from './sandbox'
export * from './asset/comments-export'
export * from './turntable/turntable'
export * from './integrations/helper'
@@ -0,0 +1,271 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import {
IntegrationDefinition,
resolveIntegrationConfig,
getIntegrationSettings,
updateIntegrationSettings,
resolveTestConnectionConfig,
getIntegrationBasicAuthHeader,
} from './helper'
const TEST_INTEGRATION_DEF: IntegrationDefinition<'turntable'> = {
name: 'TestIntegration',
settingsKey: 'turntable',
envKeys: {
url: ['TEST_INTEGRATION_URL', 'FALLBACK_INTEGRATION_URL'],
username: ['TEST_INTEGRATION_USERNAME'],
password: ['TEST_INTEGRATION_PASSWORD'],
},
}
describe('Integration Config Helper', () => {
setupTestDbHooks()
const originalEnv = process.env
beforeEach(() => {
process.env = { ...originalEnv }
delete process.env.TEST_INTEGRATION_URL
delete process.env.FALLBACK_INTEGRATION_URL
delete process.env.TEST_INTEGRATION_USERNAME
delete process.env.TEST_INTEGRATION_PASSWORD
})
afterEach(() => {
process.env = originalEnv
})
describe('resolveIntegrationConfig', () => {
it('prioritizes environment variables over database settings', async () => {
const team = await prisma.team.create({
data: {
name: 'Test Team',
settings: {
turntable: {
url: 'http://db-server:1234',
username: 'db-user',
password: 'db-password',
},
},
},
})
process.env.TEST_INTEGRATION_URL = 'http://env-server:5678/'
process.env.TEST_INTEGRATION_USERNAME = 'env-user'
process.env.TEST_INTEGRATION_PASSWORD = 'env-pass'
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF, team.id)
expect(config).toEqual({
url: 'http://env-server:5678',
username: 'env-user',
password: 'env-pass',
isEnv: true,
})
})
it('falls back to second env key if first is missing', async () => {
process.env.FALLBACK_INTEGRATION_URL = 'http://fallback-server:8080'
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF)
expect(config.url).toBe('http://fallback-server:8080')
expect(config.isEnv).toBe(true)
})
it('reads from team settings when environment variables are not set', async () => {
const team = await prisma.team.create({
data: {
name: 'Test Team',
settings: {
turntable: {
url: 'http://db-server:1234/',
username: 'db-user',
password: 'db-password',
},
},
},
})
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF, team.id)
expect(config).toEqual({
url: 'http://db-server:1234',
username: 'db-user',
password: 'db-password',
isEnv: false,
})
})
it('returns isEnv: false and undefined fields when neither env nor DB are set', async () => {
const team = await prisma.team.create({
data: { name: 'Empty Team' },
})
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF, team.id)
expect(config).toEqual({
isEnv: false,
})
})
})
describe('getIntegrationSettings', () => {
it('returns safe response masking password', async () => {
const team = await prisma.team.create({
data: {
name: 'Test Team',
settings: {
turntable: {
url: 'http://db-server:1234',
username: 'db-user',
password: 'db-password',
},
},
},
})
const res = await getIntegrationSettings(TEST_INTEGRATION_DEF, team.id)
expect(res).toEqual({
url: 'http://db-server:1234',
username: 'db-user',
hasPassword: true,
isEnvConfigured: false,
})
})
})
describe('updateIntegrationSettings', () => {
it('throws error when environment variable is configured', async () => {
const team = await prisma.team.create({
data: { name: 'Test Team' },
})
process.env.TEST_INTEGRATION_URL = 'http://env-server:5678'
await expect(
updateIntegrationSettings(TEST_INTEGRATION_DEF, team.id, {
url: 'http://new-url:1234',
}),
).rejects.toThrow(
'TestIntegration settings are configured via environment variables and cannot be edited',
)
})
it('updates settings and preserves existing password when not supplied', async () => {
const team = await prisma.team.create({
data: {
name: 'Test Team',
settings: {
turntable: {
url: 'http://original:1234',
username: 'original-user',
password: 'secret-password',
},
},
},
})
const res = await updateIntegrationSettings(TEST_INTEGRATION_DEF, team.id, {
url: 'http://updated:5678/',
username: 'new-user',
})
expect(res).toEqual({
url: 'http://updated:5678',
username: 'new-user',
hasPassword: true,
isEnvConfigured: false,
})
const config = await resolveIntegrationConfig(TEST_INTEGRATION_DEF, team.id)
expect(config.password).toBe('secret-password')
})
it('clears settings in database when URL is empty or whitespace', async () => {
const team = await prisma.team.create({
data: {
name: 'Test Team',
settings: {
turntable: {
url: 'http://to-be-cleared:1234',
username: 'user',
password: 'pwd',
},
},
},
})
const res = await updateIntegrationSettings(TEST_INTEGRATION_DEF, team.id, {
url: ' ',
})
expect(res).toEqual({
url: undefined,
username: undefined,
hasPassword: false,
isEnvConfigured: false,
})
const updatedTeam = await prisma.team.findUnique({
where: { id: team.id },
})
// Check that turntable is removed from team settings
expect((updatedTeam?.settings as Record<string, unknown>)?.turntable).toBeUndefined()
})
})
describe('resolveTestConnectionConfig', () => {
it('uses provided credentials directly when supplied', async () => {
const target = await resolveTestConnectionConfig(TEST_INTEGRATION_DEF, {
url: 'http://direct:1234',
username: 'direct-user',
password: 'direct-password',
})
expect(target).toEqual({
resolvedUrl: 'http://direct:1234',
resolvedUsername: 'direct-user',
resolvedPassword: 'direct-password',
})
})
it('falls back to saved database settings when testing with partial inputs', async () => {
const team = await prisma.team.create({
data: {
name: 'Test Team',
settings: {
turntable: {
url: 'http://db:1234',
username: 'db-user',
password: 'db-password',
},
},
},
})
const target = await resolveTestConnectionConfig(
TEST_INTEGRATION_DEF,
{ url: 'http://new-url:9999' },
team.id,
)
expect(target).toEqual({
resolvedUrl: 'http://new-url:9999',
resolvedUsername: 'db-user',
resolvedPassword: 'db-password',
})
})
})
describe('getIntegrationBasicAuthHeader', () => {
it('generates base64 basic auth header when username and password are provided', () => {
const header = getIntegrationBasicAuthHeader({ username: 'admin', password: 'secret' })
expect(header).toBe(`Basic ${Buffer.from('admin:secret').toString('base64')}`)
})
it('returns undefined when username or password is missing', () => {
expect(getIntegrationBasicAuthHeader({ username: 'admin' })).toBeUndefined()
expect(getIntegrationBasicAuthHeader({ password: 'secret' })).toBeUndefined()
expect(getIntegrationBasicAuthHeader({})).toBeUndefined()
})
})
})
+188
View File
@@ -0,0 +1,188 @@
import { prisma } from '@shumai/db'
import '@shumai/db/src/prisma-json-types'
import { logger } from '@shumai/core/src/logger'
export interface IntegrationEnvKeys {
url: string[]
username?: string[]
password?: string[]
}
export interface IntegrationDefinition<TKey extends keyof PrismaJson.Settings> {
name: string
settingsKey: TKey
envKeys: IntegrationEnvKeys
}
export interface ResolvedIntegrationConfig {
url?: string
username?: string
password?: string
isEnv: boolean
}
export interface IntegrationSettingsResponse {
url?: string
username?: string
hasPassword: boolean
isEnvConfigured: boolean
}
export interface UpdateIntegrationSettingsRequest {
url?: string
username?: string
password?: string
}
function getFirstEnvValue(keys?: string[]): string | undefined {
if (!keys) return undefined
for (const key of keys) {
const val = process.env[key]?.trim()
if (val) return val
}
return undefined
}
export async function resolveIntegrationConfig<TKey extends keyof PrismaJson.Settings>(
def: IntegrationDefinition<TKey>,
teamId?: string,
): Promise<ResolvedIntegrationConfig> {
const envUrl = getFirstEnvValue(def.envKeys.url)
const envUsername = getFirstEnvValue(def.envKeys.username)
const envPassword = getFirstEnvValue(def.envKeys.password)
if (envUrl) {
return {
url: envUrl.replace(/\/+$/, ''),
username: envUsername || undefined,
password: envPassword || undefined,
isEnv: true,
}
}
if (teamId) {
try {
const team = await prisma.team.findUnique({
where: { id: teamId },
select: { settings: true },
})
const teamSettings = team?.settings as PrismaJson.Settings | null
const serviceSettings = teamSettings?.[def.settingsKey] as
| { url?: string; username?: string; password?: string }
| undefined
if (serviceSettings?.url?.trim()) {
return {
url: serviceSettings.url.trim().replace(/\/+$/, ''),
username: serviceSettings.username?.trim() || undefined,
password: serviceSettings.password?.trim() || undefined,
isEnv: false,
}
}
} catch (err) {
logger.error({ err, teamId }, `Failed to load team ${def.name} settings`)
}
}
return {
isEnv: false,
}
}
export async function getIntegrationSettings<TKey extends keyof PrismaJson.Settings>(
def: IntegrationDefinition<TKey>,
teamId: string,
): Promise<IntegrationSettingsResponse> {
const config = await resolveIntegrationConfig(def, teamId)
return {
url: config.url,
username: config.username,
hasPassword: Boolean(config.password),
isEnvConfigured: config.isEnv,
}
}
export async function updateIntegrationSettings<TKey extends keyof PrismaJson.Settings>(
def: IntegrationDefinition<TKey>,
teamId: string,
req: UpdateIntegrationSettingsRequest,
): Promise<IntegrationSettingsResponse> {
const currentConfig = await resolveIntegrationConfig(def, teamId)
if (currentConfig.isEnv) {
throw new Error(
`${def.name} settings are configured via environment variables and cannot be edited`,
)
}
const team = await prisma.team.findUnique({
where: { id: teamId },
select: { settings: true },
})
const settings = { ...((team?.settings || {}) as PrismaJson.Settings) }
const prevSettings = (settings[def.settingsKey] || {}) as {
url?: string
username?: string
password?: string
}
const trimmedUrl = req.url?.trim()
if (!trimmedUrl) {
// If URL is empty/unset, clear the integration settings
delete settings[def.settingsKey]
} else {
const updatedServiceSettings: { url: string; username?: string; password?: string } = {
url: trimmedUrl.replace(/\/+$/, ''),
username:
req.username !== undefined ? req.username.trim() || undefined : prevSettings.username,
password: req.password !== undefined ? req.password : prevSettings.password,
}
settings[def.settingsKey] = updatedServiceSettings as unknown as PrismaJson.Settings[TKey]
}
await prisma.team.update({
where: { id: teamId },
data: { settings },
})
return getIntegrationSettings(def, teamId)
}
export async function resolveTestConnectionConfig<TKey extends keyof PrismaJson.Settings>(
def: IntegrationDefinition<TKey>,
config?: UpdateIntegrationSettingsRequest,
teamId?: string,
): Promise<{ resolvedUrl?: string; resolvedUsername?: string; resolvedPassword?: string }> {
let resolvedUrl = config?.url?.trim()
let resolvedUsername = config?.username?.trim()
let resolvedPassword = config?.password
if (teamId && (!resolvedUrl || resolvedPassword === undefined)) {
const savedConfig = await resolveIntegrationConfig(def, teamId)
if (!resolvedUrl) {
resolvedUrl = savedConfig.url
}
if (!resolvedUsername && savedConfig.username) {
resolvedUsername = savedConfig.username
}
if (resolvedPassword === undefined) {
resolvedPassword = savedConfig.password
}
}
return {
resolvedUrl,
resolvedUsername,
resolvedPassword,
}
}
export function getIntegrationBasicAuthHeader(config: {
username?: string
password?: string
}): string | undefined {
if (config.username && config.password) {
const credentials = Buffer.from(`${config.username}:${config.password}`).toString('base64')
return `Basic ${credentials}`
}
return undefined
}
+18
View File
@@ -381,6 +381,12 @@ describe('TeamService', () => {
url: undefined,
username: undefined,
},
gotenberg: {
hasPassword: false,
isEnvConfigured: false,
url: undefined,
username: undefined,
},
appearance: { hideAgent: false },
})
@@ -404,6 +410,12 @@ describe('TeamService', () => {
url: undefined,
username: undefined,
},
gotenberg: {
hasPassword: false,
isEnvConfigured: false,
url: undefined,
username: undefined,
},
appearance: { hideAgent: false },
})
@@ -436,6 +448,12 @@ describe('TeamService', () => {
url: undefined,
username: undefined,
},
gotenberg: {
hasPassword: false,
isEnvConfigured: false,
url: undefined,
username: undefined,
},
appearance: { hideAgent: true },
semanticSearchEnabled: false,
})
+2
View File
@@ -8,6 +8,7 @@ import { HTTPException } from 'hono/http-exception'
import { getAllowedAgentRoles } from '@shumai/core/src/agent/permissions'
import { sandboxService } from '@shumai/core/src/sandbox/sandbox-service'
import { turntableService } from '@shumai/core/src/turntable/turntable'
import { gotenbergService } from '@shumai/core/src/gotenberg/gotenberg'
import {
ServiceCreateTeamRequest,
ServiceGetUserTeamsRequest,
@@ -345,6 +346,7 @@ export class TeamService {
}
settings.turntable = await turntableService.getSettings(teamId)
settings.gotenberg = await gotenbergService.getSettings(teamId)
return settings
}
+31 -104
View File
@@ -1,7 +1,23 @@
import * as fs from 'fs'
import { prisma } from '@shumai/db'
import type { TurntableSettingsResponse, UpdateTurntableSettingsRequest } from '@shumai/dtos'
import { logger } from '../logger'
import {
IntegrationDefinition,
resolveIntegrationConfig,
getIntegrationSettings,
updateIntegrationSettings,
resolveTestConnectionConfig,
getIntegrationBasicAuthHeader,
} from '@shumai/core/src/integrations/helper'
export const TURNTABLE_INTEGRATION_DEF: IntegrationDefinition<'turntable'> = {
name: 'Turntable',
settingsKey: 'turntable',
envKeys: {
url: ['TURNTABLE_RENDERER_URL'],
username: ['TURNTABLE_RENDERER_BASIC_AUTH_USERNAME', 'TURNTABLE_RENDERER_USERNAME'],
password: ['TURNTABLE_RENDERER_BASIC_AUTH_PASSWORD', 'TURNTABLE_RENDERER_PASSWORD'],
},
}
export interface TurntableConfig {
url?: string
@@ -67,55 +83,11 @@ export interface TurntableTaskResponse {
export class TurntableService {
async resolveConfig(teamId?: string): Promise<TurntableConfig> {
const envUrl = process.env.TURNTABLE_RENDERER_URL?.trim()
const envUsername =
process.env.TURNTABLE_RENDERER_BASIC_AUTH_USERNAME?.trim() ||
process.env.TURNTABLE_RENDERER_USERNAME?.trim()
const envPassword =
process.env.TURNTABLE_RENDERER_BASIC_AUTH_PASSWORD?.trim() ||
process.env.TURNTABLE_RENDERER_PASSWORD?.trim()
if (envUrl) {
return {
url: envUrl.replace(/\/+$/, ''),
username: envUsername || undefined,
password: envPassword || undefined,
isEnv: true,
}
}
if (teamId) {
try {
const team = await prisma.team.findUnique({
where: { id: teamId },
select: { settings: true },
})
const teamSettings = team?.settings as PrismaJson.Settings | null
const turntable = teamSettings?.turntable
if (turntable?.url?.trim()) {
return {
url: turntable.url.trim().replace(/\/+$/, ''),
username: turntable.username?.trim() || undefined,
password: turntable.password?.trim() || undefined,
isEnv: false,
}
}
} catch (err) {
logger.error({ err, teamId }, 'Failed to load team turntable settings')
}
}
return {
isEnv: false,
}
return resolveIntegrationConfig(TURNTABLE_INTEGRATION_DEF, teamId)
}
private getAuthHeader(config: TurntableConfig): string | undefined {
if (config.username && config.password) {
const credentials = Buffer.from(`${config.username}:${config.password}`).toString('base64')
return `Basic ${credentials}`
}
return undefined
return getIntegrationBasicAuthHeader(config)
}
async isAvailable(teamId?: string): Promise<boolean> {
@@ -144,45 +116,14 @@ export class TurntableService {
}
async getSettings(teamId: string): Promise<TurntableSettingsResponse> {
const config = await this.resolveConfig(teamId)
return {
url: config.url,
username: config.username,
hasPassword: Boolean(config.password),
isEnvConfigured: config.isEnv,
}
return getIntegrationSettings(TURNTABLE_INTEGRATION_DEF, teamId)
}
async updateSettings(
teamId: string,
req: UpdateTurntableSettingsRequest,
): Promise<TurntableSettingsResponse> {
const currentConfig = await this.resolveConfig(teamId)
if (currentConfig.isEnv) {
throw new Error(
'Turntable settings are configured via environment variables and cannot be edited',
)
}
const team = await prisma.team.findUnique({
where: { id: teamId },
select: { settings: true },
})
const settings = (team?.settings || {}) as PrismaJson.Settings
const prevTurntable = settings.turntable || {}
settings.turntable = {
url: req.url !== undefined ? req.url : prevTurntable.url,
username: req.username !== undefined ? req.username : prevTurntable.username,
password: req.password !== undefined ? req.password : prevTurntable.password,
}
await prisma.team.update({
where: { id: teamId },
data: { settings },
})
return this.getSettings(teamId)
return updateIntegrationSettings(TURNTABLE_INTEGRATION_DEF, teamId, req)
}
async testConnection(
@@ -193,35 +134,21 @@ export class TurntableService {
},
teamId?: string,
): Promise<{ ok: boolean; version?: string; blender?: string; error?: string }> {
let resolvedUrl = config?.url?.trim()
let resolvedUsername = config?.username?.trim()
let resolvedPassword = config?.password
const target = await resolveTestConnectionConfig(TURNTABLE_INTEGRATION_DEF, config, teamId)
if (teamId && (!resolvedUrl || resolvedPassword === undefined)) {
const savedConfig = await this.resolveConfig(teamId)
if (!resolvedUrl) {
resolvedUrl = savedConfig.url
}
if (!resolvedUsername && savedConfig.username) {
resolvedUsername = savedConfig.username
}
if (resolvedPassword === undefined) {
resolvedPassword = savedConfig.password
}
}
if (!resolvedUrl) {
if (!target.resolvedUrl) {
return { ok: false, error: 'URL is required' }
}
const cleanUrl = resolvedUrl.replace(/\/+$/, '')
const cleanUrl = target.resolvedUrl.replace(/\/+$/, '')
try {
const headers: Record<string, string> = {}
if (resolvedUsername && resolvedPassword) {
const credentials = Buffer.from(`${resolvedUsername}:${resolvedPassword}`).toString(
'base64',
)
headers['Authorization'] = `Basic ${credentials}`
const authHeader = getIntegrationBasicAuthHeader({
username: target.resolvedUsername,
password: target.resolvedPassword,
})
if (authHeader) {
headers['Authorization'] = authHeader
}
const response = await fetch(`${cleanUrl}/version`, {
+87
View File
@@ -451,6 +451,93 @@ describe('UploadService', () => {
expect(workflowTask).toBeNull()
})
it('should create transcode_pdf for office documents when Gotenberg is configured in team settings without global env', async () => {
vi.restoreAllMocks()
vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {
const url = String(input)
if (url.includes('/health')) {
return new Response(JSON.stringify({ status: 'up' }), { status: 200 })
}
return new Response('Not Found', { status: 404 })
})
const originalEnv = process.env.GOTENBERG_URL
delete process.env.GOTENBERG_URL
try {
await prisma.team.update({
where: { id: teamId },
data: {
settings: {
gotenberg: {
url: 'http://gotenberg.local:3000',
},
},
},
})
const task = await prisma.task.create({
data: { creatorId: userId, total: 1, uploaded: 0, type: 'upload' },
})
const asset = await prisma.asset.create({
data: {
name: 'report.docx',
type: AssetType.file,
project: { connect: { id: projectId } },
parent: { connect: { id: parentId } },
status: AssetStatus.uploading,
storageKey: {
connectOrCreate: {
where: { key: 'test-key-doc-team-settings' },
create: { key: 'test-key-doc-team-settings' },
},
},
mediaType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
},
})
await uploadService.confirmFileUpload(userId, task.id, { fileId: asset.id })
const workflowTask = await prisma.workflowTask.findFirst({
where: { assetId: asset.id, type: WorkflowTaskType.transcode_pdf },
})
expect(workflowTask).not.toBeNull()
expect(workflowTask?.status).toBe(TaskStatus.pending)
// Test HTML document as well
const htmlTask = await prisma.task.create({
data: { creatorId: userId, total: 1, uploaded: 0, type: 'upload' },
})
const htmlAsset = await prisma.asset.create({
data: {
name: 'index.html',
type: AssetType.file,
project: { connect: { id: projectId } },
parent: { connect: { id: parentId } },
status: AssetStatus.uploading,
storageKey: {
connectOrCreate: {
where: { key: 'test-key-html-team-settings' },
create: { key: 'test-key-html-team-settings' },
},
},
mediaType: 'text/html',
},
})
await uploadService.confirmFileUpload(userId, htmlTask.id, { fileId: htmlAsset.id })
const htmlWorkflowTask = await prisma.workflowTask.findFirst({
where: { assetId: htmlAsset.id, type: WorkflowTaskType.transcode_pdf },
})
expect(htmlWorkflowTask).not.toBeNull()
expect(htmlWorkflowTask?.status).toBe(TaskStatus.pending)
} finally {
if (originalEnv !== undefined) {
process.env.GOTENBERG_URL = originalEnv
}
}
})
it('should confirm file upload and create transcode_3d for 3D model when turntable is available', async () => {
vi.spyOn(turntableService, 'isAvailable').mockResolvedValue(true)
const task = await prisma.task.create({
+1 -1
View File
@@ -363,7 +363,7 @@ export class UploadService {
const isHtml = isHtmlDocument(asset.mediaType, asset.name)
if (isOffice || isHtml) {
const gotenbergAvailable = await gotenbergService.isAvailable()
const gotenbergAvailable = await gotenbergService.isAvailable(team.id)
if (!gotenbergAvailable) {
await tx.asset.update({
where: { id: asset.id },
+7
View File
@@ -109,9 +109,16 @@ declare global {
password?: string
}
export interface GotenbergSettings {
url?: string
username?: string
password?: string
}
export interface Settings {
transcode?: TranscodeSettings
turntable?: TurntableSettings
gotenberg?: GotenbergSettings
mediaGeneration?: MediaGenerationSettings
appearance?: AppearanceSettings
emailNotification?: EmailNotificationSettings
+16
View File
@@ -157,6 +157,21 @@ export const updateTurntableSettingsRequestSchema = z.object({
})
export type UpdateTurntableSettingsRequest = z.infer<typeof updateTurntableSettingsRequestSchema>
export const gotenbergSettingsResponseSchema = z.object({
url: z.string().optional(),
username: z.string().optional(),
hasPassword: z.boolean(),
isEnvConfigured: z.boolean(),
})
export type GotenbergSettingsResponse = z.infer<typeof gotenbergSettingsResponseSchema>
export const updateGotenbergSettingsRequestSchema = z.object({
url: z.string().optional(),
username: z.string().optional(),
password: z.string().optional(),
})
export type UpdateGotenbergSettingsRequest = z.infer<typeof updateGotenbergSettingsRequestSchema>
export interface TeamAppearanceSettings {
hideAgent?: boolean
}
@@ -171,6 +186,7 @@ export interface TeamSettingsResponse {
hlsResolutions?: HlsResolutionLadder[]
}
turntable?: TurntableSettingsResponse
gotenberg?: GotenbergSettingsResponse
appearance?: TeamAppearanceSettings
semanticSearchEnabled?: boolean
[key: string]: unknown
+37 -16
View File
@@ -8,7 +8,10 @@ import {
} from '@shumai/core/src/transcode/transcode'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { getDerivedArtifactDirectory, stemFromKey } from '@shumai/core/src/utils/filename'
import { gotenbergService } from '@shumai/core/src/gotenberg/gotenberg'
import {
gotenbergService,
type GotenbergConvertOptions,
} from '@shumai/core/src/gotenberg/gotenberg'
import { turntableService } from '@shumai/core/src/turntable/turntable'
import { parseCsvContent } from '@shumai/core/src/transcode/transcode'
import { resolutionToDimensions } from '../workflows/transcode-utils'
@@ -916,6 +919,7 @@ export async function generateSpriteActivity(params: GenerateSpriteActivityParam
}
export interface GeneratePdfProxyActivityParams {
teamId?: string | null
assetId: string
assetKey: string
filePath: string
@@ -942,8 +946,12 @@ export async function generatePdfProxyActivity(
const tmpDir = path.dirname(params.filePath)
const pdfFilePath = path.join(tmpDir, 'proxy.pdf')
const targetTeamId = params.teamId || undefined
try {
const gotenbergAvailable = await gotenbergService.isAvailable()
const gotenbergAvailable = await (targetTeamId
? gotenbergService.isAvailable(targetTeamId)
: gotenbergService.isAvailable())
if (isOffice) {
if (!gotenbergAvailable) {
@@ -951,10 +959,14 @@ export async function generatePdfProxyActivity(
'Gotenberg service is required for Office document conversion but is unavailable.',
)
}
const pdfBuffer = await gotenbergService.convertDocumentToPdf(
params.filePath,
params.filename,
)
const pdfBuffer = await (targetTeamId
? gotenbergService.convertDocumentToPdf(
params.filePath,
params.filename,
{ teamId: targetTeamId },
targetTeamId,
)
: gotenbergService.convertDocumentToPdf(params.filePath, params.filename))
fs.writeFileSync(pdfFilePath, pdfBuffer)
} else if (isHtml) {
if (!gotenbergAvailable) {
@@ -962,7 +974,9 @@ export async function generatePdfProxyActivity(
'Gotenberg service is required for HTML document conversion but is unavailable.',
)
}
const pdfBuffer = await gotenbergService.convertHtmlToPdf(params.filePath, params.filename)
const pdfBuffer = await (targetTeamId
? gotenbergService.convertHtmlToPdf(params.filePath, params.filename, targetTeamId)
: gotenbergService.convertHtmlToPdf(params.filePath, params.filename))
fs.writeFileSync(pdfFilePath, pdfBuffer)
} else if (isCsv) {
if (gotenbergAvailable) {
@@ -975,10 +989,14 @@ export async function generatePdfProxyActivity(
} catch {
// Fallback landscape calculation
}
const options: GotenbergConvertOptions = {
landscape: isLandscape,
...(targetTeamId ? { teamId: targetTeamId } : {}),
}
const pdfBuffer = await gotenbergService.convertDocumentToPdf(
params.filePath,
params.filename,
{ landscape: isLandscape },
options,
)
fs.writeFileSync(pdfFilePath, pdfBuffer)
} else {
@@ -986,20 +1004,23 @@ export async function generatePdfProxyActivity(
}
} else if (isMd) {
if (gotenbergAvailable) {
const pdfBuffer = await gotenbergService.convertMarkdownToPdf(
params.filePath,
params.filename,
)
const pdfBuffer = await (targetTeamId
? gotenbergService.convertMarkdownToPdf(params.filePath, params.filename, targetTeamId)
: gotenbergService.convertMarkdownToPdf(params.filePath, params.filename))
fs.writeFileSync(pdfFilePath, pdfBuffer)
} else {
await transcodeService.generatePdfFromText(params.filePath, pdfFilePath)
}
} else if (isTxt) {
if (gotenbergAvailable) {
const pdfBuffer = await gotenbergService.convertDocumentToPdf(
params.filePath,
params.filename,
)
const pdfBuffer = await (targetTeamId
? gotenbergService.convertDocumentToPdf(
params.filePath,
params.filename,
{ teamId: targetTeamId },
targetTeamId,
)
: gotenbergService.convertDocumentToPdf(params.filePath, params.filename))
fs.writeFileSync(pdfFilePath, pdfBuffer)
} else {
await transcodeService.generatePdfFromText(params.filePath, pdfFilePath)
@@ -42,6 +42,7 @@ export async function transcodePdfWorkflow(task: WorkflowTask): Promise<void> {
tmpDir = download.tmpDir
const pdfProxy = await executeActivity(workerQueue, generatePdfProxyActivity, {
teamId: task.teamId,
assetId: asset.id,
assetKey: key,
filePath: currentFilePath,
@@ -0,0 +1,210 @@
// @vitest-environment happy-dom
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { GotenbergSettingsCard } from './GotenbergSettingsCard'
import type { GotenbergSettingsResponse } from '@shumai/dtos'
const mockGetSettings = vi.fn()
const mockPutSettings = vi.fn()
const mockTestSettings = vi.fn()
vi.mock('@/ui/api/client', () => ({
client: {
api: {
teams: {
':teamId': {
'gotenberg-settings': {
$get: () => mockGetSettings(),
$put: (args: unknown) => mockPutSettings(args),
test: {
$post: (args: unknown) => mockTestSettings(args),
},
},
},
},
},
},
}))
vi.mock('sonner', () => ({
toast: {
success: vi.fn(),
error: vi.fn(),
},
}))
vi.mock('@/ui/paraglide/messages.js', () => ({
m: {
gotenberg_settings: () => 'Gotenberg Document Converter',
gotenberg_settings_description: () => 'Configure external Gotenberg service',
gotenberg_env_configured: () => 'Configured via Environment',
gotenberg_server_url: () => 'Server URL',
gotenberg_server_url_placeholder: () => 'http://localhost:3000',
gotenberg_username: () => 'Username',
gotenberg_password: () => 'Password',
gotenberg_password_placeholder: () => 'Saved password',
gotenberg_test_connection: () => 'Test Connection',
gotenberg_testing: () => 'Testing...',
gotenberg_connection_success: () => 'Connection successful',
gotenberg_connection_failed: () => 'Connection failed',
save: () => 'Save',
settings_updated: () => 'Settings updated',
failed_update_settings: () => 'Failed to update settings',
},
}))
const createWrapper = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
})
return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
}
describe('GotenbergSettingsCard', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
cleanup()
})
it('renders loaded settings with URL and username', async () => {
const settings: GotenbergSettingsResponse = {
isEnvConfigured: false,
url: 'http://gotenberg.local:3000',
username: 'gotenberg-user',
hasPassword: true,
}
mockGetSettings.mockResolvedValueOnce({
ok: true,
json: async () => settings,
})
render(<GotenbergSettingsCard teamId="team-123" />, { wrapper: createWrapper() })
await waitFor(() => {
expect(screen.getByDisplayValue('http://gotenberg.local:3000')).toBeDefined()
expect(screen.getByDisplayValue('gotenberg-user')).toBeDefined()
})
})
it('disables inputs and displays badge when configured via environment', async () => {
const settings: GotenbergSettingsResponse = {
isEnvConfigured: true,
url: 'http://env-gotenberg:3000',
username: 'env-user',
hasPassword: true,
}
mockGetSettings.mockResolvedValueOnce({
ok: true,
json: async () => settings,
})
render(<GotenbergSettingsCard teamId="team-123" />, { wrapper: createWrapper() })
await waitFor(() => {
expect(screen.getByText('Configured via Environment')).toBeDefined()
const urlInput = screen.getByDisplayValue('http://env-gotenberg:3000') as HTMLInputElement
expect(urlInput.disabled).toBe(true)
const saveBtn = screen.getByRole('button', { name: 'Save' }) as HTMLButtonElement
expect(saveBtn.disabled).toBe(true)
})
})
it('saves updated settings', async () => {
const settings: GotenbergSettingsResponse = {
isEnvConfigured: false,
url: '',
username: '',
hasPassword: false,
}
mockGetSettings.mockResolvedValueOnce({
ok: true,
json: async () => settings,
})
mockPutSettings.mockResolvedValueOnce({
ok: true,
json: async () => ({
isEnvConfigured: false,
url: 'http://custom:3000',
username: 'user1',
hasPassword: true,
}),
})
render(<GotenbergSettingsCard teamId="team-123" />, { wrapper: createWrapper() })
await waitFor(() => {
expect(screen.getByLabelText('Server URL')).toBeDefined()
})
const urlInput = screen.getByLabelText('Server URL')
const usernameInput = screen.getByLabelText('Username')
const passwordInput = document.getElementById('gotenberg-password') as HTMLInputElement
fireEvent.change(urlInput, { target: { value: 'http://custom:3000' } })
fireEvent.change(usernameInput, { target: { value: 'user1' } })
fireEvent.change(passwordInput, { target: { value: 'newpass' } })
const saveBtn = screen.getByRole('button', { name: 'Save' })
fireEvent.click(saveBtn)
await waitFor(() => {
expect(mockPutSettings).toHaveBeenCalledWith(
expect.objectContaining({
param: { teamId: 'team-123' },
json: {
url: 'http://custom:3000',
username: 'user1',
password: 'newpass',
},
}),
)
})
})
it('handles test connection with success result', async () => {
const settings: GotenbergSettingsResponse = {
isEnvConfigured: false,
url: 'http://test-gotenberg:3000',
username: 'user',
hasPassword: false,
}
mockGetSettings.mockResolvedValueOnce({
ok: true,
json: async () => settings,
})
mockTestSettings.mockResolvedValueOnce({
ok: true,
json: async () => ({ ok: true, version: '8.3.0' }),
})
render(<GotenbergSettingsCard teamId="team-123" />, { wrapper: createWrapper() })
await waitFor(() => {
expect(screen.getByDisplayValue('http://test-gotenberg:3000')).toBeDefined()
})
const testBtn = screen.getByRole('button', { name: 'Test Connection' })
fireEvent.click(testBtn)
await waitFor(() => {
expect(screen.getByText(/Connection successful/)).toBeDefined()
expect(screen.getByText(/\(v8\.3\.0\)/)).toBeDefined()
})
})
})
@@ -0,0 +1,242 @@
import React, { useState, useEffect } from 'react'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { client } from '@/ui/api/client'
import {
Card,
CardHeader,
CardTitle,
CardDescription,
CardContent,
CardFooter,
} from '@/ui/components/ui/card'
import { Input } from '@/ui/components/ui/input'
import { Button } from '@/ui/components/ui/button'
import { Badge } from '@/ui/components/ui/badge'
import { Loader2, CheckCircle2, XCircle, Info } from 'lucide-react'
import { toast } from 'sonner'
import { m } from '@/ui/paraglide/messages.js'
interface GotenbergSettingsCardProps {
teamId: string
}
export const GotenbergSettingsCard: React.FC<GotenbergSettingsCardProps> = ({ teamId }) => {
const queryClient = useQueryClient()
const [url, setUrl] = useState('')
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [testResult, setTestResult] = useState<{
ok: boolean
version?: string
error?: string
} | null>(null)
const [isTesting, setIsTesting] = useState(false)
const { data: gotenbergSettings, isLoading } = useQuery({
queryKey: ['teams', teamId, 'gotenberg-settings'],
queryFn: async () => {
const res = await client.api.teams[':teamId']['gotenberg-settings'].$get({
param: { teamId },
})
if (!res.ok) throw new Error('Failed to load gotenberg settings')
return await res.json()
},
enabled: !!teamId,
})
useEffect(() => {
if (gotenbergSettings) {
setUrl(gotenbergSettings.url || '')
setUsername(gotenbergSettings.username || '')
setPassword('')
setTestResult(null)
}
}, [gotenbergSettings])
const { mutateAsync: saveSettings, isPending: isSaving } = useMutation({
mutationFn: async () => {
const res = await client.api.teams[':teamId']['gotenberg-settings'].$put({
param: { teamId },
json: {
url: url.trim(),
username: username.trim() || undefined,
password: password || undefined,
},
})
if (!res.ok) {
const err = await res.json().catch(() => ({}))
throw new Error(
(err as { message?: string }).message || 'Failed to update gotenberg settings',
)
}
return await res.json()
},
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'gotenberg-settings'],
})
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.success(m.settings_updated())
},
onError: (err: Error) => {
toast.error(err.message || m.failed_update_settings())
},
})
const handleTestConnection = async () => {
setIsTesting(true)
setTestResult(null)
try {
const res = await client.api.teams[':teamId']['gotenberg-settings'].test.$post({
param: { teamId },
json: {
url: url.trim() || undefined,
username: username.trim() || undefined,
password: password || undefined,
},
})
if (!res.ok) {
const err = await res.json().catch(() => ({}))
const errorMsg = (err as { message?: string }).message || 'Request failed'
setTestResult({ ok: false, error: errorMsg })
toast.error(m.gotenberg_connection_failed())
return
}
const data = await res.json()
setTestResult(data)
if (data.ok) {
toast.success(m.gotenberg_connection_success())
} else {
toast.error(data.error || m.gotenberg_connection_failed())
}
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
setTestResult({ ok: false, error: msg })
toast.error(m.gotenberg_connection_failed())
} finally {
setIsTesting(false)
}
}
const isEnvConfigured = Boolean(gotenbergSettings?.isEnvConfigured)
return (
<Card>
<CardHeader>
<div className="flex items-center justify-between">
<CardTitle>{m.gotenberg_settings()}</CardTitle>
{isEnvConfigured && (
<Badge variant="secondary" className="gap-1">
<Info className="w-3 h-3" />
{m.gotenberg_env_configured()}
</Badge>
)}
</div>
<CardDescription>{m.gotenberg_settings_description()}</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{isLoading ? (
<div className="flex items-center justify-center py-6">
<Loader2 className="w-6 h-6 animate-spin text-muted-foreground" />
</div>
) : (
<>
<div className="space-y-1.5">
<label htmlFor="gotenberg-url" className="text-sm font-medium">
{m.gotenberg_server_url()}
</label>
<Input
id="gotenberg-url"
value={url}
onChange={(e) => setUrl(e.target.value)}
placeholder={m.gotenberg_server_url_placeholder()}
disabled={isEnvConfigured || isSaving}
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="space-y-1.5">
<label htmlFor="gotenberg-username" className="text-sm font-medium">
{m.gotenberg_username()}
</label>
<Input
id="gotenberg-username"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="admin"
disabled={isEnvConfigured || isSaving}
/>
</div>
<div className="space-y-1.5">
<label htmlFor="gotenberg-password" className="text-sm font-medium">
{m.gotenberg_password()}
</label>
<Input
id="gotenberg-password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder={
gotenbergSettings?.hasPassword ? m.gotenberg_password_placeholder() : '••••••••'
}
disabled={isEnvConfigured || isSaving}
/>
</div>
</div>
{testResult && (
<div
className={`flex items-center gap-2 p-3 rounded-lg border text-sm ${
testResult.ok
? 'border-emerald-500/30 bg-emerald-500/10 text-emerald-600 dark:text-emerald-400'
: 'border-destructive/30 bg-destructive/10 text-destructive'
}`}
>
{testResult.ok ? (
<>
<CheckCircle2 className="w-4 h-4 shrink-0" />
<span>
{m.gotenberg_connection_success()}
{testResult.version ? ` (v${testResult.version})` : ''}
</span>
</>
) : (
<>
<XCircle className="w-4 h-4 shrink-0" />
<span>
{m.gotenberg_connection_failed()}: {testResult.error}
</span>
</>
)}
</div>
)}
</>
)}
</CardContent>
<CardFooter className="flex justify-between border-t border-border pt-4">
<Button
type="button"
variant="outline"
onClick={handleTestConnection}
disabled={isLoading || isTesting || !url.trim()}
>
{isTesting && <Loader2 className="w-4 h-4 mr-2 animate-spin" />}
{isTesting ? m.gotenberg_testing() : m.gotenberg_test_connection()}
</Button>
<Button
type="button"
onClick={() => saveSettings()}
disabled={isEnvConfigured || isSaving || isLoading}
>
{isSaving && <Loader2 className="w-4 h-4 mr-2 animate-spin" />}
{m.save()}
</Button>
</CardFooter>
</Card>
)
}
@@ -60,8 +60,8 @@ export const TurntableSettingsCard: React.FC<TurntableSettingsCardProps> = ({ te
const res = await client.api.teams[':teamId']['turntable-settings'].$put({
param: { teamId },
json: {
url: url || undefined,
username: username || undefined,
url: url.trim(),
username: username.trim() || undefined,
password: password || undefined,
},
})
+14
View File
@@ -269,6 +269,20 @@
"turntable_connection_success": "Connected successfully",
"turntable_connection_failed": "Connection failed",
"turntable_testing": "Testing...",
"integrations": "Integrations",
"integrations_settings_description": "Configure third-party service integrations for media rendering and document processing.",
"gotenberg_settings": "Gotenberg Document Converter",
"gotenberg_settings_description": "Configure the external Gotenberg service for converting Office documents, Markdown, and HTML to PDF.",
"gotenberg_server_url": "Server URL",
"gotenberg_server_url_placeholder": "e.g. http://localhost:3000",
"gotenberg_username": "Username",
"gotenberg_password": "Password",
"gotenberg_password_placeholder": "Leave blank to keep existing password",
"gotenberg_env_configured": "Configured via environment variables (Read-only)",
"gotenberg_test_connection": "Test Connection",
"gotenberg_connection_success": "Connected successfully",
"gotenberg_connection_failed": "Connection failed",
"gotenberg_testing": "Testing...",
"hardware_acceleration_section_description": "Configure hardware-accelerated video encoding to offload workloads to GPUs, or use CPU software encoding.",
"mp4_transcoding": "MP4 Transcoding",
"mp4_transcoding_description": "Generate web-compatible MP4 files for file downloads and standard video playback.",
+14
View File
@@ -269,6 +269,20 @@
"turntable_connection_success": "连接成功",
"turntable_connection_failed": "连接失败",
"turntable_testing": "正在测试...",
"integrations": "集成",
"integrations_settings_description": "配置用于媒体渲染与文档处理的第三方服务集成。",
"gotenberg_settings": "Gotenberg 文档转换服务",
"gotenberg_settings_description": "配置用于将 Office 文档、Markdown 及 HTML 转换为 PDF 的外部 Gotenberg 服务。",
"gotenberg_server_url": "服务地址",
"gotenberg_server_url_placeholder": "例如 http://localhost:3000",
"gotenberg_username": "用户名",
"gotenberg_password": "密码",
"gotenberg_password_placeholder": "留空以保留当前密码",
"gotenberg_env_configured": "已通过环境变量配置(只读)",
"gotenberg_test_connection": "测试连接",
"gotenberg_connection_success": "连接成功",
"gotenberg_connection_failed": "连接失败",
"gotenberg_testing": "正在测试...",
"hardware_acceleration_section_description": "配置硬件加速视频编码以将工作负载分流至 GPU,或使用 CPU 软件编码。",
"mp4_transcoding": "MP4 转码",
"mp4_transcoding_description": "生成适用于文件下载和标准视频播放的 Web 兼容 MP4 文件。",
@@ -24,11 +24,13 @@ import { AppearanceSettings } from '@/ui/components/settings/AppearanceSettings'
import { EmailNotificationSettings } from '@/ui/components/settings/EmailNotificationSettings'
import { ImageVideoGenerationSettings } from '@/ui/components/settings/ImageVideoGenerationSettings'
import { TurntableSettingsCard } from '@/ui/components/settings/TurntableSettingsCard'
import { GotenbergSettingsCard } from '@/ui/components/settings/GotenbergSettingsCard'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/components/ui/card'
import { Slider } from '@/ui/components/ui/slider'
import { cn } from '@/ui/lib/utils'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import {
Blocks,
Bot,
Cpu,
Film,
@@ -67,6 +69,7 @@ type SettingsTab =
| 'quotas'
| 'appearance'
| 'email'
| 'integrations'
| 'skills'
| 'mcp'
| 'providers'
@@ -82,6 +85,7 @@ const VALID_SETTINGS_TABS: readonly SettingsTab[] = [
'quotas',
'appearance',
'email',
'integrations',
'skills',
'mcp',
'providers',
@@ -647,6 +651,22 @@ export function TeamSettingsPage() {
<div className="ml-auto w-1.5 h-1.5 rounded-full bg-sidebar-primary" />
)}
</button>
<button
onClick={() => handleTabChange('integrations')}
className={cn(
'w-full flex items-center gap-3 px-4 py-3 text-sm font-medium rounded-lg transition-all',
activeTab === 'integrations'
? 'bg-sidebar-accent text-sidebar-accent-foreground ring-1 ring-sidebar-border'
: 'text-sidebar-foreground/75 hover:bg-sidebar-accent hover:text-sidebar-accent-foreground',
)}
>
<Blocks className="w-5 h-5" />
{m.integrations()}
{activeTab === 'integrations' && (
<div className="ml-auto w-1.5 h-1.5 rounded-full bg-sidebar-primary" />
)}
</button>
</>
)}
@@ -768,6 +788,7 @@ export function TeamSettingsPage() {
{activeTab === 'quotas' && m.resource_quotas()}
{activeTab === 'appearance' && m.appearance_settings()}
{activeTab === 'email' && m.email_notifications()}
{activeTab === 'integrations' && m.integrations()}
{activeTab === 'skills' && m.skills_management()}
{activeTab === 'mcp' && m.mcp_servers()}
{activeTab === 'providers' && m.ai_providers()}
@@ -783,6 +804,7 @@ export function TeamSettingsPage() {
{activeTab === 'quotas' && m.resource_quotas_description()}
{activeTab === 'appearance' && m.appearance_settings_description()}
{activeTab === 'email' && m.email_notifications_description()}
{activeTab === 'integrations' && m.integrations_settings_description()}
{activeTab === 'skills' && m.skills_description()}
{activeTab === 'mcp' && m.mcp_servers_description()}
{activeTab === 'providers' && m.providers_description()}
@@ -1148,9 +1170,6 @@ export function TeamSettingsPage() {
</div>
</CardContent>
</Card>
{/* 3D Turntable Renderer Card */}
<TurntableSettingsCard teamId={teamId} />
</div>
)}
@@ -1191,6 +1210,13 @@ export function TeamSettingsPage() {
{activeTab === 'appearance' && <AppearanceSettings teamId={teamId} />}
{activeTab === 'email' && <EmailNotificationSettings teamId={teamId} />}
{activeTab === 'integrations' && (
<div className="h-full overflow-y-auto space-y-6 pr-1">
<TurntableSettingsCard teamId={teamId} />
<GotenbergSettingsCard teamId={teamId} />
</div>
)}
</div>
</div>
</main>