finish share api
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Artem Anufrij 2023-02-15 23:36:19 +01:00
parent dc358e21dc
commit 408b6c3dc5
8 changed files with 205 additions and 48 deletions

View File

@ -10,12 +10,12 @@
"http://localhost"
],
"database": {
"host": "database",
"host": "localhost",
"port": 27017,
"name": "webplay"
},
"redis": {
"host": "redis",
"host": "localhost",
"port": 6379
},
"album_cover_files": [

View File

@ -8,11 +8,12 @@ const checkGuest = server.checkGuest;
const resize_image_for_album = require("../services/cover/resizer").resize_image_for_album
const router = new express.Router();
const { ObjectId } = require('mongodb');
router.route("/favourites")
.get(passport.authenticate("jwt", { session: false }), (req, res) => {
database.albums.favourites(req.user._id, result => {
res.json(result).end();
res.json(result).status(200).end();
});
})
@ -25,7 +26,7 @@ router.route("/page/:page")
}
database.albums.collection(req.params.page, filter, result => {
process.stdout.write("router/album GET albums page " + req.params.page + " DB result\n");
res.json(result);
res.json(result).status(200).end();
});
});
@ -38,7 +39,7 @@ router.route("/newest/:count")
}
database.albums.newest(parseInt(req.params.count), filter, result => {
process.stdout.write("router/album GET newest " + req.params.count + " DB result\n");
res.json(result);
res.json(result).status(200).end();
});
});
@ -47,7 +48,7 @@ router.route("/filter/:term")
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");
res.json(result);
res.json(result).status(200).end();
});
})
@ -60,7 +61,7 @@ router.route("/:id")
}
database.albums.byId(req.params.id, filter, result => {
process.stdout.write("router/album GET album by id " + req.params.id + " DB result\n");
res.json(result);
res.json(result).status(200).end();
});
})
.put(passport.authenticate("jwt", { session: false }), (req, res) => {
@ -76,7 +77,7 @@ router.route("/:id/cover")
if (req.files.file) {
resize_image_for_album(req.files.file.data, (result) => {
database.albums.updateCovers({ _id: req.params.id }, result);
res.json(result);
res.json(result).status(200).end();
});
}
}
@ -105,7 +106,6 @@ router.route("/:id/move")
database.albums.delete(source, () => {
res.status(200).end();
});
} else {
res.status(403).end();
}
@ -118,18 +118,38 @@ router.route("/:id/move")
router.route("/:id/share")
.post(passport.authenticate("jwt", { session: false }), (req, res) => {
database.albums.byId(req.body.source, undefined, (result) => {
let album_id = req.params.id
process.stdout.write("router/album POST albums share " + album_id + "\n");
database.albums.byId(album_id, undefined, (result) => {
if (result != undefined && req.user.roles.indexOf("admin") > -1) {
process.write("Add shared abum");
database.share.exists(album_id, result => {
if (result == null) {
let item = {
user_id: req.user._id,
object_id: new ObjectId(album_id),
type: "album"
}
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) => {
database.albums.byId(req.body.source, undefined, (result) => {
if (result != undefined && req.user.roles.indexOf("admin") > -1) {
process.write("Delete shared abum");
let album_id = req.params.id
if (req.user.roles.indexOf("admin") > -1) {
let item = {
object_id: new ObjectId(album_id),
type: "album"
}
database.share.delete(item, () => {
res.status(200).end();
});
}
});
module.exports = router;

View File

@ -8,6 +8,7 @@ const checkGuest = server.checkGuest;
const resize_image_for_box = require("../services/cover/resizer").resize_image_for_box
const router = new express.Router();
const { ObjectId } = require('mongodb');
router.route("/favourites")
.get(passport.authenticate("jwt", { session: false }), (req, res) => {
@ -44,6 +45,15 @@ router
});
});
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);
});
})
router
.route("/:id")
.get(checkGuest, (req, res) => {
@ -63,14 +73,6 @@ router
res.end();
});
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);
});
})
router.route("/:id/cover")
.put(passport.authenticate("jwt", { session: false }), (req, res) => {
@ -115,4 +117,43 @@ router.route("/:id/move")
}
});
});
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");
database.boxes.byId(box_id, undefined, (result) => {
if (result != undefined && req.user.roles.indexOf("admin") > -1) {
database.share.exists(box_id, result => {
if (result == null) {
let item = {
user_id: req.user._id,
object_id: new ObjectId(box_id),
type: "box"
}
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 = {
object_id: new ObjectId(box_id),
type: "box"
}
database.share.delete(item, () => {
res.status(200).end();
});
}
});
module.exports = router;

View File

@ -31,8 +31,6 @@ function updateArtistName() {
});
}
exports.collection = function (page, filter, callback) {
process.stdout.write("services/db_manager ALBUMS Collection: " + page + "\n");
let redis_key = "albumsCollection_" + (filter || '') + '_' + page;
@ -70,6 +68,7 @@ exports.collection = function (page, filter, callback) {
result.forEach(album => {
album.type = "album";
album.tracks = [];
album.share = {};
});
}
process.stdout.write("services/db_manager ALBUMS Collection MONGO: " + page + "\n");
@ -149,6 +148,13 @@ exports.byId = function (id, filter, callback) {
foreignField: "album_id",
as: "tracks"
}
}, {
$lookup: {
from: "shares",
localField: "_id",
foreignField: "object_id",
as: "share"
}
},
{ $match: { _id: ObjectId(id) } }
]
@ -167,6 +173,11 @@ exports.byId = function (id, filter, callback) {
if (result) {
result.forEach(album => {
album.type = "album";
if (album.share.length > 0) {
album.share = album.share[0];
} else {
album.share = {};
}
});
}
process.stdout.write("services/db_manager ALBUM by id MONGO: " + id + "\n");

View File

@ -70,9 +70,10 @@ exports.collection = function (page, filter, callback) {
.toArray((err, result) => {
if (err) throw err;
if (result) {
result.forEach(item => {
item.type = "box";
item.videos = [];
result.forEach(box => {
box.type = "box";
box.videos = [];
box.share = {};
});
}
process.stdout.write("services/db_manager BOXES Collection MONGO: " + page + "\n");
@ -148,6 +149,14 @@ exports.byId = function (id, filter, callback) {
let aggregate = [
box_lookup_videos,
box_project,
{
$lookup: {
from: "shares",
localField: "_id",
foreignField: "object_id",
as: "share"
}
},
{ $match: { _id: ObjectId(id) } }
];
if (filter) {
@ -162,8 +171,13 @@ exports.byId = function (id, filter, callback) {
.toArray((err, result) => {
if (err) throw err;
if (result) {
result.forEach(item => {
item.type = "box";
result.forEach(box => {
box.type = "box";
if (box.share.length > 0) {
box.share = box.share[0];
} else {
box.share = {};
}
});
}
process.stdout.write("services/db_manager BOX by id MONGO: " + id + "\n");

View File

@ -71,6 +71,8 @@ exports.users = users;
const system = require("./system");
exports.system = system;
const share = require("./share");
exports.share = share;
exports.artist_count = function (callback) {
return dbo.collection("artists").countDocuments(callback);

View File

@ -0,0 +1,60 @@
const redis = require("../redis")
const { ObjectId } = require('mongodb');
const connector = require("./CONNECTOR");
var dbo;
connector.connect().then((ret) => {
dbo = ret;
});
exports.byId = function (id, callback) {
dbo.collection("shares")
.findOne({ _id: ObjectId(id) })
.then((result) => {
callback(result);
});
}
exports.exists = function (object_id, callback) {
dbo.collection("shares")
.findOne({ object_id: ObjectId(object_id) })
.then(result => {
callback(result);
});
}
exports.add = function (item, callback) {
let redis_key = item.type + "Id__" + item.object_id;
redis.del(redis_key);
dbo.collection("shares")
.insertOne(item, err => {
if (err) throw err;
if (callback) {
dbo.collection("shares")
.aggregate([
{ $sort: { _id: -1 } },
{ $limit: 1 }
])
.toArray((err, result) => {
if (err) throw err;
callback(result[0]);
})
}
});
}
exports.delete = function (item, callback) {
let redis_key = item.type + "Id__" + item.object_id;
redis.del(redis_key);
dbo.collection("shares")
.deleteMany({ object_id: ObjectId(item.object_id) }, (err) => {
if (err) throw err;
if (callback) {
callback();
}
});
}

View File

@ -30,6 +30,15 @@ exports.get = function (key, callback) {
client.expire(key, expire);
}
exports.del = function (key, callback) {
process.stdout.write("services/redis del '" + key + "'\n");
client.del(key).then(() => {
if (callback) {
callback();
}
});
}
exports.flushAll = function () {
client.flushAll();
process.stdout.write("services/redis flushAll()\n");