server/router/album.js

157 lines
5.3 KiB
JavaScript
Raw Normal View History

2023-02-08 12:30:56 +01:00
console.log("router/album 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_album = require("../services/cover/resizer").resize_image_for_album
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.albums.favourites(req.user._id, result => {
2023-02-15 23:36:19 +01:00
res.json(result).status(200).end();
2023-02-08 12:30:56 +01:00
});
})
router.route("/page/:page")
.get(checkGuest, (req, res) => {
process.stdout.write("router/album GET albums page " + req.params.page + "\n");
let filter = undefined;
if (req.user._id == -1) {
filter = ['global'];
}
database.albums.collection(req.params.page, filter, result => {
process.stdout.write("router/album GET albums page " + req.params.page + " DB result\n");
2023-02-15 23:36:19 +01:00
res.json(result).status(200).end();
2023-02-08 12:30:56 +01:00
});
});
router.route("/newest/:count")
.get(checkGuest, (req, res) => {
process.stdout.write("router/album GET newest " + req.params.count + "\n");
let filter = undefined;
if (req.user._id == -1) {
filter = ['global'];
}
database.albums.newest(parseInt(req.params.count), filter, result => {
process.stdout.write("router/album GET newest " + req.params.count + " DB result\n");
2023-02-15 23:36:19 +01:00
res.json(result).status(200).end();
2023-02-08 12:30:56 +01:00
});
});
2023-02-15 12:57:00 +01:00
router.route("/filter/:term")
.get(passport.authenticate("jwt", { session: false }), (req, res) => {
process.stdout.write("router/album GET filter by term " + req.params.term + "\n");
database.albums.filter(req.params.term, result => {
process.stdout.write("router/album GET filter by term " + req.params.term + " DB result\n");
2023-02-15 23:36:19 +01:00
res.json(result).status(200).end();
2023-02-15 12:57:00 +01:00
});
})
2023-02-08 12:30:56 +01:00
router.route("/:id")
.get(checkGuest, (req, res) => {
process.stdout.write("router/album GET album by id " + req.params.id + "\n");
let filter = undefined;
if (req.user._id == -1) {
filter = ['global'];
}
database.albums.byId(req.params.id, filter, result => {
process.stdout.write("router/album GET album by id " + req.params.id + " DB result\n");
2023-02-15 23:36:19 +01:00
res.json(result).status(200).end();
2023-02-08 12:30:56 +01:00
});
})
.put(passport.authenticate("jwt", { session: false }), (req, res) => {
if (req.user.roles.indexOf("admin") >= 0 || req.user.roles.indexOf("moderator") >= 0) {
database.albums.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_album(req.files.file.data, (result) => {
database.albums.updateCovers({ _id: req.params.id }, result);
2023-02-15 23:36:19 +01:00
res.json(result).status(200).end();
2023-02-08 12:30:56 +01:00
});
}
}
})
.delete(passport.authenticate("jwt", { session: false }), (req, res) => {
database.albums.updateCovers({ _id: req.params.id }, {});
res.end();
});
router.route("/:id/move")
.put(passport.authenticate("jwt", { session: false }), (req, res) => {
let source = {};
let target = {};
database.albums.byId(req.body.source, undefined, (result) => {
if (result != undefined && (req.user.roles.indexOf("admin") > -1) || result.owner_id == req.user._id) {
source = result;
database.albums.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.tracks.length; i++) {
let track = source.tracks[i]
track.album_id = target._id
database.tracks.moveTo(track);
}
database.albums.delete(source, () => {
res.status(200).end();
});
} else {
res.status(403).end();
}
});
} else {
res.status(403).end();
}
});
});
2023-02-15 12:57:00 +01:00
router.route("/:id/share")
.post(passport.authenticate("jwt", { session: false }), (req, res) => {
2023-02-15 23:36:19 +01:00
let album_id = req.params.id
process.stdout.write("router/album POST albums share " + album_id + "\n");
2023-02-17 22:06:11 +01:00
database.albums.byId(album_id, undefined, (album) => {
if (album != undefined && req.user.roles.indexOf("admin") > -1) {
2023-02-15 23:36:19 +01:00
database.share.exists(album_id, result => {
if (result == null) {
let item = {
user_id: req.user._id,
2023-02-17 22:52:54 +01:00
object_id: new ObjectId(album_id),
2023-02-17 22:47:15 +01:00
title: album.title,
2023-02-17 22:06:11 +01:00
type: "album",
cover: album.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();
}
});
2023-02-15 12:57:00 +01:00
}
});
})
.delete(passport.authenticate("jwt", { session: false }), (req, res) => {
2023-02-15 23:36:19 +01:00
let album_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(album_id),
type: "album"
2023-02-15 12:57:00 +01:00
}
2023-02-15 23:36:19 +01:00
database.share.delete(item, () => {
res.status(200).end();
});
}
2023-02-15 12:57:00 +01:00
});
2023-02-08 12:30:56 +01:00
module.exports = router;