Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 7,602 Bytes
5dfc565 bda5f6b 9571f2e a65e95e bda5f6b c5b0dde 5fb05e6 5dfc565 652f343 5dfc565 c8231d6 278d122 9571f2e 652f343 9571f2e 652f343 bda5f6b 9658ad9 652f343 c5b0dde 278d122 652f343 9658ad9 c5b0dde 652f343 7bea24d 652f343 5fb05e6 652f343 7bea24d 652f343 bda5f6b 652f343 bda5f6b 9571f2e 278d122 9571f2e c8231d6 9571f2e c8231d6 9571f2e c8231d6 9571f2e 278d122 9571f2e 278d122 9571f2e 278d122 9571f2e 652f343 9571f2e 652f343 9571f2e 652f343 9571f2e 652f343 9658ad9 9571f2e 278d122 9571f2e 5dfc565 9571f2e 5dfc565 9571f2e 5dfc565 9571f2e 5dfc565 9571f2e 5dfc565 9571f2e 5dfc565 9571f2e 5dfc565 9571f2e 278d122 9571f2e c5b0dde 9571f2e 652f343 9571f2e 652f343 bda5f6b 652f343 9571f2e 652f343 9571f2e 652f343 9571f2e 652f343 bda5f6b 9571f2e 5fb05e6 9571f2e 5fb05e6 9571f2e 5fb05e6 9571f2e 652f343 9571f2e 652f343 9571f2e 652f343 9571f2e 652f343 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
import { createReadStream, existsSync } from "node:fs"
import path from "node:path"
import { validate as uuidValidate } from "uuid"
import express from "express"
import { VideoTask, VideoTaskRequest } from "./types.mts"
import { parseVideoRequest } from "./utils/parseVideoRequest.mts"
import { savePendingTask } from "./scheduler/savePendingTask.mts"
import { getTask } from "./scheduler/getTask.mts"
import { main } from "./main.mts"
import { completedFilesDirFilePath } from "./config.mts"
import { deleteTask } from "./scheduler/deleteTask.mts"
import { getPendingTasks } from "./scheduler/getPendingTasks.mts"
import { hasValidAuthorization } from "./utils/hasValidAuthorization.mts"
import { getAllTasksForOwner } from "./scheduler/getAllTasksForOwner.mts"
import { initFolders } from "./initFolders.mts"
initFolders()
// to disable all processing (eg. to debug)
// then comment the following line:
main()
const app = express()
const port = 7860
app.use(express.json())
app.post("/", async (req, res) => {
const request = req.body as VideoTaskRequest
if (!hasValidAuthorization(req.headers)) {
console.log("Invalid authorization")
res.status(401)
res.write(JSON.stringify({ error: "invalid token" }))
res.end()
return
}
let task: VideoTask = null
console.log(`creating task from request..`)
console.log(`request: `, JSON.stringify(request))
try {
task = await parseVideoRequest(request)
} catch (err) {
console.error(`failed to create task: ${task} (${err})`)
res.status(400)
res.write(JSON.stringify({ error: "query seems to be malformed" }))
res.end()
return
}
console.log(`saving task ${task.id}`)
try {
await savePendingTask(task)
res.status(200)
res.write(JSON.stringify(task))
res.end()
} catch (err) {
console.error(err)
res.status(500)
res.write(JSON.stringify({ error: "couldn't save the task" }))
res.end()
}
})
// only get the tasks for a specific owner
app.get("/owner/:ownerId", async (req, res) => {
if (!hasValidAuthorization(req.headers)) {
console.log("Invalid authorization")
res.status(401)
res.write(JSON.stringify({ error: "invalid token" }))
res.end()
return
}
const ownerId = req.params.ownerId
if (!uuidValidate(ownerId)) {
console.error("invalid owner id")
res.status(400)
res.write(JSON.stringify({ error: `invalid owner id` }))
res.end()
return
}
try {
const tasks = await getAllTasksForOwner(ownerId)
res.status(200)
res.write(JSON.stringify(tasks, null, 2))
res.end()
} catch (err) {
console.error(err)
res.status(500)
res.write(JSON.stringify({ error: `couldn't get the tasks for owner ${ownerId}` }))
res.end()
}
})
app.get("/download/:id\.mp4", async (req, res) => {
/*
for simplicity, let's skip auth when fetching videos
the UUIDs cannot easily be guessed anyway
if (!hasValidAuthorization(req.headers)) {
console.log("Invalid authorization")
res.status(401)
res.write(JSON.stringify({ error: "invalid token" }))
res.end()
return
}
*/
const [ownerId, videoId] = `${req.params.id}`.split("_")
if (!uuidValidate(ownerId)) {
console.error("invalid owner id")
res.status(400)
res.write(JSON.stringify({ error: `invalid owner id` }))
res.end()
return
}
if (!uuidValidate(videoId)) {
console.error("invalid video id")
res.status(400)
res.write(JSON.stringify({ error: `invalid video id` }))
res.end()
return
}
let task: VideoTask = null
try {
task = await getTask(ownerId, videoId)
console.log(`returning video ${videoId} to owner ${ownerId}`)
} catch (err) {
res.status(404)
res.write(JSON.stringify({ error: "this video doesn't exist" }))
res.end()
return
}
const completedFilePath = path.join(completedFilesDirFilePath, task.fileName)
// note: we DON'T want to use the pending file path, as there may be operations on it
// (ie. a process might be busy writing stuff to it)
const filePath = existsSync(completedFilePath) ? completedFilePath : ""
if (!filePath) {
res.status(400)
res.write(JSON.stringify({ error: "video exists, but cannot be previewed yet" }))
res.end()
return
}
// file path exists, let's try to read it
try {
// do we need this?
// res.status(200)
// res.setHeader("Content-Type", "media/mp4")
console.log(`creating a video read stream from ${filePath}`)
const stream = createReadStream(filePath)
stream.on('close', () => {
console.log(`finished streaming the video`)
res.end()
})
stream.pipe(res)
} catch (err) {
console.error(`failed to read the video file at ${filePath}: ${err}`)
res.status(500)
res.write(JSON.stringify({ error: "failed to read the video file" }))
res.end()
}
})
// get all pending tasks
app.get("/", async (req, res) => {
if (!hasValidAuthorization(req.headers)) {
console.log("Invalid authorization")
res.status(401)
res.write(JSON.stringify({ error: "invalid token" }))
res.end()
return
}
try {
const tasks = await getPendingTasks()
res.status(200)
res.write(JSON.stringify(tasks, null, 2))
res.end()
} catch (err) {
console.error(err)
res.status(500)
res.write(JSON.stringify({ error: "couldn't get the tasks" }))
res.end()
}
})
app.get("/:id", async (req, res) => {
if (!hasValidAuthorization(req.headers)) {
console.log("Invalid authorization")
res.status(401)
res.write(JSON.stringify({ error: "invalid token" }))
res.end()
return
}
const [ownerId, videoId] = `${req.params.id}`.split("_")
if (!uuidValidate(ownerId)) {
console.error("invalid owner id")
res.status(400)
res.write(JSON.stringify({ error: `invalid owner id` }))
res.end()
return
}
if (!uuidValidate(videoId)) {
console.error("invalid video id")
res.status(400)
res.write(JSON.stringify({ error: `invalid video id` }))
res.end()
return
}
try {
const task = await getTask(ownerId, videoId)
res.status(200)
res.write(JSON.stringify(task))
res.end()
} catch (err) {
console.error(err)
res.status(404)
res.write(JSON.stringify({ error: "couldn't find this task" }))
res.end()
}
})
app.delete("/:id", async (req, res) => {
if (!hasValidAuthorization(req.headers)) {
console.log("Invalid authorization")
res.status(401)
res.write(JSON.stringify({ error: "invalid token" }))
res.end()
return
}
const [ownerId, videoId] = `${req.params.id}`.split("_")
if (!uuidValidate(ownerId)) {
console.error("invalid owner id")
res.status(400)
res.write(JSON.stringify({ error: `invalid owner id` }))
res.end()
return
}
if (!uuidValidate(videoId)) {
console.error("invalid video id")
res.status(400)
res.write(JSON.stringify({ error: `invalid video id` }))
res.end()
return
}
let task: VideoTask = null
try {
task = await getTask(ownerId, videoId)
} catch (err) {
console.error(err)
res.status(404)
res.write(JSON.stringify({ error: "couldn't find this task" }))
res.end()
}
try {
await deleteTask(task)
res.status(200)
res.write(JSON.stringify({ success: true }))
res.end()
} catch (err) {
console.error(err)
res.status(500)
res.write(JSON.stringify({ success: false, error: "failed to delete the task" }))
res.end()
}
})
app.listen(port, () => { console.log(`Open http://localhost:${port}`) }) |