File size: 1,209 Bytes
e4e0e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
import tmpDir from "temp-dir"
import { validate as uuidValidate } from "uuid"

import { completedMetadataDirFilePath, completedFilesDirFilePath, pendingMetadataDirFilePath, pendingFilesDirFilePath } from "../config.mts"
import { deleteFilesWithName } from "../utils/deleteAllFilesWith.mts"


// note: we make sure ownerId and videoId are *VALID*
// otherwise an attacker could try to delete important files!
export const deleteVideo = async (ownerId: string, videoId?: string) => {
  if (!uuidValidate(ownerId)) {
    throw new Error(`fatal error: ownerId ${ownerId} is invalid!`)
  }

  if (videoId && !uuidValidate(videoId)) {
    throw new Error(`fatal error: videoId ${videoId} is invalid!`)
  }
  const id = videoId ? `${ownerId}_${videoId}` : ownerId

  // this should delete everything, including audio files
  // however we still have some temporary files with a name that is unique:
  // we should probably rename those
  await deleteFilesWithName(tmpDir, id)
  await deleteFilesWithName(completedMetadataDirFilePath, id)
  await deleteFilesWithName(completedFilesDirFilePath, id)
  await deleteFilesWithName(pendingMetadataDirFilePath, id)
  await deleteFilesWithName(pendingFilesDirFilePath, id)
}