|
import { Video } from "../types.mts" |
|
import { deleteVideo } from "./deleteVideo.mts" |
|
import { getVideoStatus } from "./getVideoStatus.mts" |
|
import { saveCompletedVideo } from "./saveCompletedVideo.mts" |
|
import { updatePendingVideo } from "./updatePendingVideo.mts" |
|
|
|
export const saveAndCheckIfNeedToStop = async (video: Video): Promise<boolean> => { |
|
|
|
const status = await getVideoStatus(video) |
|
const isToDelete = status === "delete" |
|
const isToAbort = status === "abort" |
|
const isToPause = status === "pause" |
|
|
|
|
|
|
|
|
|
|
|
|
|
const mustStop = isToAbort || isToPause || isToDelete |
|
|
|
|
|
if (isToDelete) { |
|
await deleteVideo(video.ownerId, video.id) |
|
return mustStop |
|
} |
|
|
|
|
|
if (video.completed) { |
|
console.log(`video ${video.id} is completed!`) |
|
video.progressPercent = 100 |
|
video.completedAt = new Date().toISOString() |
|
video.status = "completed" |
|
await updatePendingVideo(video) |
|
await saveCompletedVideo(video) |
|
return mustStop |
|
} |
|
|
|
|
|
if (isToPause) { |
|
console.log(`we've been requested to pause the video`) |
|
video.status = "pause" |
|
await updatePendingVideo(video) |
|
return mustStop |
|
} |
|
|
|
if (isToAbort) { |
|
console.log(`we've been requested to cancel the video`) |
|
|
|
|
|
|
|
video.completed = true |
|
|
|
|
|
|
|
video.status = "completed" |
|
|
|
video.completedAt = new Date().toISOString() |
|
await updatePendingVideo(video) |
|
await saveCompletedVideo(video) |
|
|
|
return mustStop |
|
} |
|
|
|
await updatePendingVideo(video) |
|
|
|
|
|
|
|
return true |
|
} |
|
|