Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

1.3 KB
Raw Blame
/** Stage potentially slow audio encoding outside the backup cut, then publish
 * its final bytes and track row under one admission lease. */
import { randomUUID } from 'node:crypto';
import { constants } from 'node:fs';
import { copyFile, mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { withBackupAssetPublication } from '../lib/backupSnapshotBoundary.js';
import { PATHS, ensureDir } from '../lib/fileUtils.js';
import { writeWavAudioFile } from '../lib/wavAudioFile.js';
import * as tracks from './tracks/index.js';

export async function publishMusicTake({ trackId, wav, take, patch = {} }) {
  const stageDir = await mkdtemp(join(tmpdir(), 'portos-music-take-'));
  try {
    const filename = await writeWavAudioFile(wav, stageDir, `music-${randomUUID()}`);
    return await withBackupAssetPublication(async () => {
      await ensureDir(PATHS.music);
      await copyFile(join(stageDir, filename), join(PATHS.music, filename), constants.COPYFILE_EXCL);
      const published = { ...take, audioFilename: filename };
      const track = await tracks.appendActiveTake(trackId, published, ...(Object.keys(patch).length ? [patch] : []));
      return { track, filename };
    });
  } finally {
    await rm(stageDir, { recursive: true, force: true });
  }
}