Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

2.0 KB
Raw Blame
import type { FastifyInstance } from 'fastify';
import type { PlatformConnectionService } from '../platform-connections/service.js';
import { apiError } from './errors.js';

export async function registerPlatformConnectionRoutes(server: FastifyInstance, youtube: PlatformConnectionService, douyin?: PlatformConnectionService) {
  server.addHook('onSend', async (request, reply, payload) => {
    if (request.url.startsWith('/platform-connections/')) reply.header('Cache-Control', 'no-store');
    return payload;
  });
  server.post('/platform-connections/youtube/complete-login', () => youtube.completeLogin());
  for (const [platform, connection] of [['youtube', youtube], ['douyin', douyin]] as const) {
    if (connection === undefined) continue;
    const path = `/platform-connections/${platform}`;
    server.get(path, () => connection.getState());
    server.post<{ Body: { profileId?: unknown } }>(`${path}/connect`, (request, reply) => {
      const profileId = request.body?.profileId;
      if (profileId !== undefined && (typeof profileId !== 'string' || !/^[a-f0-9]{24}$/.test(profileId))) {
        return reply.code(400).send(apiError('VALIDATION_FAILED', 'Select a valid browser profile.'));
      }
      return connection.connect(profileId as string | undefined);
    });
    server.post(`${path}/cancel`, () => connection.cancel());
    server.delete(path, () => connection.disconnect());
    server.post<{ Body: { cookies?: unknown } }>(`${path}/import`, async (request, reply) => {
      if (typeof request.body?.cookies !== 'string' || Buffer.byteLength(request.body.cookies) > 1024 * 1024) {
        return reply.code(400).send(apiError('VALIDATION_FAILED', 'Select a valid cookie file.'));
      }
      try { return await connection.importCookies(request.body.cookies); }
      catch { return reply.code(400).send(apiError('VALIDATION_FAILED', `The file contains no valid ${platform === 'youtube' ? 'YouTube' : 'Douyin'} session. Check the file or reconnect.`)); }
    });
  }
}