mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-14 21:14:15 +00:00
141 lines
4.6 KiB
JavaScript
141 lines
4.6 KiB
JavaScript
import { Controller } from "linebridge/dist/server"
|
|
import { User, StreamingKey } from "../../models"
|
|
import { nanoid } from "nanoid"
|
|
|
|
import axios from "axios"
|
|
|
|
const streamingMediaServer = process.env.streamingMediaServer ?? "media.ragestudio.net"
|
|
const streamingServerAPIAddress = process.env.streamingServerAPIAddress ?? "media.ragestudio.net"
|
|
const streamingServerAPIPort = process.env.streamingServerAPIPort ?? 3002
|
|
const streamingServerAPIProtocol = process.env.streamingServerAPIProtocol ?? "http"
|
|
const streamingServerAPIUri = `${streamingServerAPIProtocol}://${streamingServerAPIAddress}:${streamingServerAPIPort}`
|
|
|
|
export default class StreamingController extends Controller {
|
|
static useMiddlewares = ["withAuthentication"]
|
|
|
|
methods = {
|
|
genereteKey: async (user_id) => {
|
|
// this will generate a new key for the user
|
|
// if the user already has a key, it will be regenerated
|
|
|
|
// get username from user_id
|
|
const userData = await User.findById(user_id)
|
|
|
|
const streamingKey = new StreamingKey({
|
|
user_id,
|
|
username: userData.username,
|
|
key: nanoid()
|
|
})
|
|
|
|
await streamingKey.save()
|
|
|
|
return streamingKey
|
|
}
|
|
}
|
|
|
|
get = {
|
|
"/stream_info_from_username": async (req, res) => {
|
|
const { username } = req.query
|
|
|
|
const userspace = await StreamingKey.findOne({ username })
|
|
|
|
if (!userspace) {
|
|
return res.status(403).json({
|
|
error: "This username has not a streaming key"
|
|
})
|
|
}
|
|
|
|
// TODO: meanwhile linebridge remote linkers are in development we gonna use this methods to fetch
|
|
const { data } = await axios.get(`${streamingServerAPIUri}/streams`, {
|
|
params: {
|
|
username: userspace.username
|
|
}
|
|
}).catch((error) => {
|
|
res.status(500).json({
|
|
error: `Failed to fetch streams from [${streamingServerAPIAddress}]: ${error.message}`
|
|
})
|
|
return false
|
|
})
|
|
|
|
if (data) {
|
|
return res.json(data)
|
|
}
|
|
},
|
|
"/streams": async (req, res) => {
|
|
// TODO: meanwhile linebridge remote linkers are in development we gonna use this methods to fetch
|
|
const { data } = await axios.get(`${streamingServerAPIUri}/streams`).catch((error) => {
|
|
res.status(500).json({
|
|
error: `Failed to fetch streams from [${streamingServerAPIAddress}]: ${error.message}`
|
|
})
|
|
return false
|
|
})
|
|
|
|
if (data) {
|
|
return res.json(data)
|
|
}
|
|
},
|
|
"/target_streaming_server": async (req, res) => {
|
|
// TODO: resolve an available server
|
|
// for now we just return the only one should be online
|
|
return res.json({
|
|
protocol: "rtmp",
|
|
port: "1935",
|
|
space: "live",
|
|
address: streamingMediaServer,
|
|
})
|
|
},
|
|
"/streaming_key": async (req, res) => {
|
|
let streamingKey = await StreamingKey.findOne({
|
|
user_id: req.user._id.toString()
|
|
})
|
|
|
|
if (!streamingKey) {
|
|
const newKey = await this.methods.genereteKey(req.user._id.toString()).catch(err => {
|
|
res.status(500).json({
|
|
error: `Cannot generate a new key: ${err.message}`,
|
|
})
|
|
|
|
return false
|
|
})
|
|
|
|
if (!newKey) {
|
|
return false
|
|
}
|
|
|
|
return res.json(newKey)
|
|
} else {
|
|
return res.json(streamingKey)
|
|
}
|
|
}
|
|
}
|
|
|
|
post = {
|
|
"/regenerate_streaming_key": async (req, res) => {
|
|
// check if the user already has a key
|
|
let streamingKey = await StreamingKey.findOne({
|
|
user_id: req.user._id.toString()
|
|
})
|
|
|
|
// if exists, delete it
|
|
|
|
if (streamingKey) {
|
|
await streamingKey.remove()
|
|
}
|
|
|
|
// generate a new key
|
|
const newKey = await this.methods.genereteKey(req.user._id.toString()).catch(err => {
|
|
res.status(500).json({
|
|
error: `Cannot generate a new key: ${err.message}`,
|
|
})
|
|
|
|
return false
|
|
})
|
|
|
|
if (!newKey) {
|
|
return false
|
|
}
|
|
|
|
return res.json(newKey)
|
|
}
|
|
}
|
|
} |