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" "http://localhost"
], ],
"database": { "database": {
"host": "database", "host": "localhost",
"port": 27017, "port": 27017,
"name": "webplay" "name": "webplay"
}, },
"redis": { "redis": {
"host": "redis", "host": "localhost",
"port": 6379 "port": 6379
}, },
"album_cover_files": [ "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 resize_image_for_album = require("../services/cover/resizer").resize_image_for_album
const router = new express.Router(); const router = new express.Router();
const { ObjectId } = require('mongodb');
router.route("/favourites") router.route("/favourites")
.get(passport.authenticate("jwt", { session: false }), (req, res) => { .get(passport.authenticate("jwt", { session: false }), (req, res) => {
database.albums.favourites(req.user._id, result => { 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 => { database.albums.collection(req.params.page, filter, result => {
process.stdout.write("router/album GET albums page " + req.params.page + " DB result\n"); 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 => { database.albums.newest(parseInt(req.params.count), filter, result => {
process.stdout.write("router/album GET newest " + req.params.count + " DB result\n"); 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"); process.stdout.write("router/album GET filter by term " + req.params.term + "\n");
database.albums.filter(req.params.term, result => { database.albums.filter(req.params.term, result => {
process.stdout.write("router/album GET filter by term " + req.params.term + " DB result\n"); 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 => { database.albums.byId(req.params.id, filter, result => {
process.stdout.write("router/album GET album by id " + req.params.id + " DB result\n"); 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) => { .put(passport.authenticate("jwt", { session: false }), (req, res) => {
@ -76,7 +77,7 @@ router.route("/:id/cover")
if (req.files.file) { if (req.files.file) {
resize_image_for_album(req.files.file.data, (result) => { resize_image_for_album(req.files.file.data, (result) => {
database.albums.updateCovers({ _id: req.params.id }, 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, () => { database.albums.delete(source, () => {
res.status(200).end(); res.status(200).end();
}); });
} else { } else {
res.status(403).end(); res.status(403).end();
} }
@ -118,18 +118,38 @@ router.route("/:id/move")
router.route("/:id/share") router.route("/:id/share")
.post(passport.authenticate("jwt", { session: false }), (req, res) => { .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) { 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) => { .delete(passport.authenticate("jwt", { session: false }), (req, res) => {
database.albums.byId(req.body.source, undefined, (result) => { let album_id = req.params.id
if (result != undefined && req.user.roles.indexOf("admin") > -1) { if (req.user.roles.indexOf("admin") > -1) {
process.write("Delete shared abum"); let item = {
object_id: new ObjectId(album_id),
type: "album"
} }
}); database.share.delete(item, () => {
res.status(200).end();
});
}
}); });
module.exports = router; 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 resize_image_for_box = require("../services/cover/resizer").resize_image_for_box
const router = new express.Router(); const router = new express.Router();
const { ObjectId } = require('mongodb');
router.route("/favourites") router.route("/favourites")
.get(passport.authenticate("jwt", { session: false }), (req, res) => { .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 router
.route("/:id") .route("/:id")
.get(checkGuest, (req, res) => { .get(checkGuest, (req, res) => {
@ -63,14 +73,6 @@ router
res.end(); 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") router.route("/:id/cover")
.put(passport.authenticate("jwt", { session: false }), (req, res) => { .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; module.exports = router;

View File

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

View File

@ -70,9 +70,10 @@ exports.collection = function (page, filter, callback) {
.toArray((err, result) => { .toArray((err, result) => {
if (err) throw err; if (err) throw err;
if (result) { if (result) {
result.forEach(item => { result.forEach(box => {
item.type = "box"; box.type = "box";
item.videos = []; box.videos = [];
box.share = {};
}); });
} }
process.stdout.write("services/db_manager BOXES Collection MONGO: " + page + "\n"); process.stdout.write("services/db_manager BOXES Collection MONGO: " + page + "\n");
@ -148,6 +149,14 @@ exports.byId = function (id, filter, callback) {
let aggregate = [ let aggregate = [
box_lookup_videos, box_lookup_videos,
box_project, box_project,
{
$lookup: {
from: "shares",
localField: "_id",
foreignField: "object_id",
as: "share"
}
},
{ $match: { _id: ObjectId(id) } } { $match: { _id: ObjectId(id) } }
]; ];
if (filter) { if (filter) {
@ -162,8 +171,13 @@ exports.byId = function (id, filter, callback) {
.toArray((err, result) => { .toArray((err, result) => {
if (err) throw err; if (err) throw err;
if (result) { if (result) {
result.forEach(item => { result.forEach(box => {
item.type = "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"); 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"); const system = require("./system");
exports.system = system; exports.system = system;
const share = require("./share");
exports.share = share;
exports.artist_count = function (callback) { exports.artist_count = function (callback) {
return dbo.collection("artists").countDocuments(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

@ -5,7 +5,7 @@ const config = server.config;
const redisUrl = "redis://" + config.redis.host + ":" + config.redis.port const redisUrl = "redis://" + config.redis.host + ":" + config.redis.port
const client = createClient({ const client = createClient({
url: redisUrl url: redisUrl
}); });
client.on('error', (err) => console.log('Redis Client Error', err)); client.on('error', (err) => console.log('Redis Client Error', err));
@ -16,21 +16,30 @@ client.flushAll();
const expire = 57600; // 24h const expire = 57600; // 24h
exports.set = function (key, value) { exports.set = function (key, value) {
if (value) { if (value) {
client.set(key, JSON.stringify(value)); client.set(key, JSON.stringify(value));
client.expire(key, expire); client.expire(key, expire);
} }
} }
exports.get = function (key, callback) { exports.get = function (key, callback) {
process.stdout.write("services/redis get '" + key + "'\n"); process.stdout.write("services/redis get '" + key + "'\n");
client.get(key).then(value => { client.get(key).then(value => {
callback(JSON.parse(value)); callback(JSON.parse(value));
}); });
client.expire(key, expire); 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 () { exports.flushAll = function () {
client.flushAll(); client.flushAll();
process.stdout.write("services/redis flushAll()\n"); process.stdout.write("services/redis flushAll()\n");
} }