Version 0.3.3 - Add of links
All checks were successful
Neutral/pipeline/head This commit looks good
All checks were successful
Neutral/pipeline/head This commit looks good
This commit is contained in:
106
bin/links.js
Normal file
106
bin/links.js
Normal file
@ -0,0 +1,106 @@
|
||||
const { LogType } = require("loguix")
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
const { __glob } = require("./global-variables")
|
||||
const ulog = new LogType("Links")
|
||||
const uuid = require("uuid")
|
||||
const config = require("./config")
|
||||
const {ApplyLinks} = require("../routes/link")
|
||||
|
||||
|
||||
|
||||
if(!fs.existsSync(__glob.DATA + path.sep + "links.json")) {
|
||||
fs.writeFileSync(__glob.DATA + path.sep + "links.json", JSON.stringify([], null, 2))
|
||||
}
|
||||
|
||||
module.exports.getLinks = function() {
|
||||
return JSON.parse(fs.readFileSync(__glob.DATA + path.sep + "links.json"))
|
||||
}
|
||||
|
||||
const FirstLinkManager = new ApplyLinks(this.getLinks())
|
||||
|
||||
|
||||
module.exports.addLink = function(settings) {
|
||||
|
||||
var canDo = true
|
||||
const links = this.getLinks()
|
||||
var id = makeid(8)
|
||||
|
||||
if(settings.abstractLink) {
|
||||
settings.dest = id.toString()
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Check if a destination already exists between links and if it's the case, we return an error "ALREADY_EXiST"
|
||||
links.forEach((link) => {
|
||||
if(link.dest == settings.dest) {
|
||||
|
||||
canDo = false
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
const link = {
|
||||
id: id,
|
||||
title: settings.title,
|
||||
url: settings.url,
|
||||
dest: settings.dest,
|
||||
}
|
||||
|
||||
if(canDo) {
|
||||
|
||||
links.push(link)
|
||||
fs.writeFileSync(__glob.DATA + path.sep + "links.json", JSON.stringify(links, null, 2))
|
||||
const LinkManager = new ApplyLinks(this.getLinks())
|
||||
return "OK"
|
||||
|
||||
} else {
|
||||
|
||||
return "ALREADY_EXIST"
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports.removeLink = function(id) {
|
||||
const links = this.getLinks()
|
||||
const newLinks = []
|
||||
links.forEach((link) => {
|
||||
if(link.id != id) {
|
||||
newLinks.push(link)
|
||||
}
|
||||
})
|
||||
fs.writeFileSync(__glob.DATA + path.sep + "links.json", JSON.stringify(newLinks, null, 2))
|
||||
const LinkManager = new ApplyLinks(this.getLinks())
|
||||
return "OK"
|
||||
}
|
||||
|
||||
module.exports.updateLink = function(id, settings) {
|
||||
const links = this.getLinks()
|
||||
const newLinks = []
|
||||
links.forEach((link) => {
|
||||
if(link.id == id) {
|
||||
link.title = settings.title
|
||||
link.url = settings.url
|
||||
}
|
||||
newLinks.push(link)
|
||||
})
|
||||
fs.writeFileSync(__glob.DATA + path.sep + "links.json", JSON.stringify(newLinks, null, 2))
|
||||
const LinkManager = new ApplyLinks(this.getLinks())
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
|
||||
function makeid(length) {
|
||||
let result = '';
|
||||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const charactersLength = characters.length;
|
||||
let counter = 0;
|
||||
while (counter < length) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
counter += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
@ -4,6 +4,7 @@ const path = require("path")
|
||||
const { __glob } = require("./global-variables")
|
||||
const auth = require("./auth")
|
||||
const files = require("./files")
|
||||
const links = require("./links.js")
|
||||
const service = require("./services")
|
||||
const plog = new LogType("Web")
|
||||
const cook = require("cookie")
|
||||
@ -113,6 +114,24 @@ module.exports.serverIO = function(server) {
|
||||
PostAnswer("SV_RESTART_SERVICE", {answer: await service.restartService(sv), name: sv})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
if(user.checkPermission("LINKS")) {
|
||||
PostRequest("LINKS_GET_ALL", () => {
|
||||
PostAnswer("LINKS_GET_ALL", {answer: "OK", links: links.getLinks()})
|
||||
})
|
||||
|
||||
PostRequest("LINKS_ADD", (settings) => {
|
||||
PostAnswer("LINKS_ADD", {answer: links.addLink(settings)})
|
||||
})
|
||||
|
||||
PostRequest("LINKS_DELETE", (id) => {
|
||||
PostAnswer("LINKS_DELETE", links.removeLink(id))
|
||||
})
|
||||
|
||||
PostRequest("LINKS_EDIT", (settings) => {
|
||||
PostAnswer("LINKS_EDIT", links.updateLink(settings.id, settings))
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user