server/router/box.js

161 lines
5.0 KiB
JavaScript
Raw Normal View History

2023-02-08 12:30:56 +01:00
console.log("router/box INIT");
const express = require("express");
const database = require("../services/database");
const server = require("../server");
const passport = server.passport;
const checkGuest = server.checkGuest;
const resize_image_for_box = require("../services/cover/resizer").resize_image_for_box
const router = new express.Router();
2023-02-15 23:36:19 +01:00
const { ObjectId } = require('mongodb');
2023-02-08 12:30:56 +01:00
router.route("/favourites")
.get(passport.authenticate("jwt", { session: false }), (req, res) => {
database.boxes.favourites(req.user._id, result => {
res.json(result).end();
});
})
router
.route("/page/:page")
.get(checkGuest, (req, res) => {
process.stdout.write("router/box GET boxes page " + req.params.page + "\n");
let filter = undefined;
if (req.user._id == -1) {
filter = ['global'];
}
database.boxes.collection(req.params.page, filter, result => {
process.stdout.write("router/box GET boxes page " + req.params.page + " DB result\n");
res.json(result);
});
});
router
.route("/newest/:count")
.get(checkGuest, (req, res) => {
process.stdout.write("router/box GET newest " + req.params.count + "\n");
let filter = undefined;
if (req.user._id == -1) {
filter = ['global'];
}
database.boxes.newest(parseInt(req.params.count), filter, result => {
process.stdout.write("router/box GET newest " + req.params.count + " DB result\n");
res.json(result);
});
});
2023-02-15 23:36:19 +01:00
router.route("/filter/:term")
.get(passport.authenticate("jwt", { session: false }), (req, res) => {
process.stdout.write("router/boxes GET filter by term " + req.params.term + "\n");
database.boxes.filter(req.params.term, result => {
process.stdout.write("router/boxes GET filter by term " + req.params.term + " DB result\n");
res.json(result);
});
})
2023-02-08 12:30:56 +01:00
router
.route("/:id")
.get(checkGuest, (req, res) => {
process.stdout.write("router/box GET by id " + req.params.id + "\n");
let filter = undefined;
if (req.user._id == -1) {
filter = ['global'];
}
database.boxes.byId(req.params.id, filter, result => {
process.stdout.write("router/box GET by id " + req.params.id + " DB result\n");
res.json(result).end();
});
}).put(passport.authenticate("jwt", { session: false }), (req, res) => {
if (req.user.roles.indexOf("admin") >= 0 || req.user.roles.indexOf("moderator") >= 0) {
database.boxes.update(req.body);
}
res.end();
});
router.route("/:id/cover")
.put(passport.authenticate("jwt", { session: false }), (req, res) => {
if (req.files) {
if (req.files.file) {
resize_image_for_box(req.files.file.data, (result) => {
database.boxes.updateCovers({ _id: req.params.id }, result);
res.json(result);
});
}
}
})
.delete(passport.authenticate("jwt", { session: false }), (req, res) => {
database.boxes.updateCovers({ _id: req.params.id }, {});
res.end();
});
router.route("/:id/move")
.put(passport.authenticate("jwt", { session: false }), (req, res) => {
let source = {};
let target = {};
database.boxes.byId(req.body.source, undefined, (result) => {
if (result && (req.user.roles.indexOf("admin") > -1 || result.owner_id == req.user._id)) {
source = result;
database.boxes.byId(req.body.target, undefined, (result) => {
if (result && (req.user.roles.indexOf("admin") > -1 || result.owner_id == req.user._id)) {
target = result;
for (let i = 0; i < source.videos.length; i++) {
let video = source.videos[i]
video.box_id = target._id
database.videos.moveTo(video);
}
database.boxes.delete(source, () => {
res.status(200).end();
});
} else {
res.status(403).end();
}
});
} else {
res.status(403).end();
}
});
});
2023-02-15 23:36:19 +01:00
router.route("/:id/share")
.post(passport.authenticate("jwt", { session: false }), (req, res) => {
let box_id = req.params.id
process.stdout.write("router/box POST box share " + box_id + "\n");
2023-02-17 22:06:11 +01:00
database.boxes.byId(box_id, undefined, (box) => {
if (box != undefined && req.user.roles.indexOf("admin") > -1) {
2023-02-15 23:36:19 +01:00
database.share.exists(box_id, result => {
if (result == null) {
let item = {
user_id: req.user._id,
2023-02-17 22:36:34 +01:00
object_id: box._id,
2023-02-17 22:06:11 +01:00
type: "box",
cover: box.covers.cover32
2023-02-15 23:36:19 +01:00
}
database.share.add(item, (result) => {
res.json(result).status(200).end();
});
} else {
res.json(result).status(200).end();
}
});
}
});
})
.delete(passport.authenticate("jwt", { session: false }), (req, res) => {
let box_id = req.params.id
if (req.user.roles.indexOf("admin") > -1) {
let item = {
2023-02-16 23:55:40 +01:00
object_id: new ObjectId(box_id),
type: "box"
2023-02-15 23:36:19 +01:00
}
database.share.delete(item, () => {
res.status(200).end();
});
}
});
2023-02-08 12:30:56 +01:00
module.exports = router;