Skip to content

Commit 895f2dc

Browse files
author
Miriad
committed
fix: use Sanity assets instead of GCS for audio/video storage
Replaces GCS uploads with Sanity asset uploads. This eliminates the need for GCS_BUCKET, GCS_PROJECT_ID, GCS_CLIENT_EMAIL, GCS_PRIVATE_KEY environment variables. - New: lib/services/sanity-upload.ts (uploadAudioToSanity, uploadVideoToSanity) - Updated: lib/services/video-pipeline.ts (use Sanity uploads) - GCS service kept but no longer imported by pipeline
1 parent 9664e1b commit 895f2dc

2 files changed

Lines changed: 81 additions & 10 deletions

File tree

lib/services/sanity-upload.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Upload files to Sanity as assets.
3+
* Replaces GCS for audio and video storage.
4+
*/
5+
import { createClient } from 'next-sanity';
6+
import { apiVersion, dataset, projectId } from '@/sanity/lib/api';
7+
8+
interface UploadResult {
9+
/** Public URL of the uploaded asset */
10+
url: string;
11+
/** Sanity asset ID */
12+
assetId: string;
13+
/** File size in bytes */
14+
size: number;
15+
}
16+
17+
function getSanityClient() {
18+
const token = process.env.SANITY_API_TOKEN || process.env.SANITY_API_WRITE_TOKEN;
19+
if (!token) throw new Error('[SANITY-UPLOAD] Missing SANITY_API_TOKEN');
20+
return createClient({ projectId, dataset, apiVersion, token, useCdn: false });
21+
}
22+
23+
/**
24+
* Upload an audio file (MP3) to Sanity.
25+
*/
26+
export async function uploadAudioToSanity(
27+
buffer: Buffer,
28+
filename: string
29+
): Promise<UploadResult> {
30+
const client = getSanityClient();
31+
console.log(`[SANITY-UPLOAD] Uploading audio: ${filename} (${buffer.length} bytes)`);
32+
33+
const asset = await client.assets.upload('file', buffer, {
34+
filename,
35+
contentType: 'audio/mpeg',
36+
});
37+
38+
const url = asset.url;
39+
console.log(`[SANITY-UPLOAD] Audio uploaded: ${url}`);
40+
41+
return {
42+
url,
43+
assetId: asset._id,
44+
size: buffer.length,
45+
};
46+
}
47+
48+
/**
49+
* Upload a video file (MP4) to Sanity.
50+
*/
51+
export async function uploadVideoToSanity(
52+
buffer: Buffer,
53+
filename: string
54+
): Promise<UploadResult> {
55+
const client = getSanityClient();
56+
console.log(`[SANITY-UPLOAD] Uploading video: ${filename} (${buffer.length} bytes)`);
57+
58+
const asset = await client.assets.upload('file', buffer, {
59+
filename,
60+
contentType: 'video/mp4',
61+
});
62+
63+
const url = asset.url;
64+
console.log(`[SANITY-UPLOAD] Video uploaded: ${url}`);
65+
66+
return {
67+
url,
68+
assetId: asset._id,
69+
size: buffer.length,
70+
};
71+
}

lib/services/video-pipeline.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import { createClient, type SanityClient } from 'next-sanity';
1313
import { apiVersion, dataset, projectId } from '@/sanity/lib/api';
1414
import { generateSpeechFromScript } from '@/lib/services/elevenlabs';
15-
import { uploadAudio, uploadVideo } from '@/lib/services/gcs';
15+
import { uploadAudioToSanity, uploadVideoToSanity } from '@/lib/services/sanity-upload';
1616
import { getBRollForScenes } from '@/lib/services/pexels';
1717
import { renderBothFormats } from '@/lib/services/remotion';
1818

@@ -87,10 +87,10 @@ async function updateStatus(
8787
* 1. Fetch document from Sanity
8888
* 2. Validate script structure
8989
* 3. Generate TTS audio (ElevenLabs)
90-
* 4. Upload audio to GCS
90+
* 4. Upload audio to Sanity
9191
* 5. Fetch B-roll clips (Pexels)
9292
* 6. Render both video formats (Remotion Lambda)
93-
* 7. Upload videos to GCS
93+
* 7. Upload videos to Sanity
9494
* 8. Update Sanity with video URLs and status
9595
*
9696
* On failure: sets status to "flagged" with flaggedReason.
@@ -138,9 +138,9 @@ export async function processVideoProduction(documentId: string): Promise<void>
138138
});
139139
console.log(`[VIDEO-PIPELINE] TTS audio generated: ${audioBuffer.length} bytes`);
140140

141-
// Step 5: Upload audio to GCS
142-
console.log(`[VIDEO-PIPELINE] Uploading audio to GCS...`);
143-
const audioResult = await uploadAudio(audioBuffer, documentId);
141+
// Step 5: Upload audio to Sanity
142+
console.log(`[VIDEO-PIPELINE] Uploading audio to Sanity...`);
143+
const audioResult = await uploadAudioToSanity(audioBuffer, `${documentId}.mp3`);
144144
const audioUrl = audioResult.url;
145145
console.log(`[VIDEO-PIPELINE] Audio uploaded: ${audioUrl} (${audioResult.size} bytes)`);
146146

@@ -201,8 +201,8 @@ export async function processVideoProduction(documentId: string): Promise<void>
201201
`[VIDEO-PIPELINE] Render complete — main: ${renderResults.main.fileSizeBytes} bytes, short: ${renderResults.short.fileSizeBytes} bytes`
202202
);
203203

204-
// Step 11: Download rendered videos and upload to GCS
205-
console.log(`[VIDEO-PIPELINE] Downloading and re-uploading rendered videos to GCS...`);
204+
// Step 11: Download rendered videos and upload to Sanity
205+
console.log(`[VIDEO-PIPELINE] Downloading and re-uploading rendered videos to Sanity...`);
206206
const [mainVideoResponse, shortVideoResponse] = await Promise.all([
207207
fetch(renderResults.main.videoUrl),
208208
fetch(renderResults.short.videoUrl),
@@ -221,8 +221,8 @@ export async function processVideoProduction(documentId: string): Promise<void>
221221
]);
222222

223223
const [mainUploadResult, shortUploadResult] = await Promise.all([
224-
uploadVideo(mainVideoBuffer, documentId, 'main'),
225-
uploadVideo(shortVideoBuffer, documentId, 'short'),
224+
uploadVideoToSanity(mainVideoBuffer, `${documentId}-main.mp4`),
225+
uploadVideoToSanity(shortVideoBuffer, `${documentId}-short.mp4`),
226226
]);
227227

228228
console.log(

0 commit comments

Comments
 (0)