File size: 724 Bytes
bda5f6b
 
deae345
bda5f6b
 
 
deae345
bda5f6b
e2472ff
 
 
deae345
 
bda5f6b
e2472ff
 
 
bda5f6b
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import path from 'node:path'
import fs from 'node:fs'
import { pendingVideosDirFilePath } from '../config.mts'

export const downloadVideo = async (remoteUrl: string, fileName: string): Promise<string> => {

  const filePath = path.resolve(pendingVideosDirFilePath, fileName)

  const controller = new AbortController()
  const timeoutId = setTimeout(() => controller.abort(), 15 * 60 * 60 * 1000) // 15 minutes

  // TODO finish the timeout?

  // download the video
  const response = await fetch(remoteUrl, {
    signal: controller.signal
  })

  // write it to the disk
  const arrayBuffer = await response.arrayBuffer()

  await fs.promises.writeFile(
    filePath,
    Buffer.from(arrayBuffer)
  )

  return fileName
}