69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
|
const path = require("path"),
|
||
|
chokidar = require("chokidar"),
|
||
|
fs = require("fs")
|
||
|
|
||
|
var paths = {
|
||
|
static: path.join(__dirname, "static"),
|
||
|
postUrl: path.join(__dirname, "resources/html/postUrl.html"),
|
||
|
redirWarn: path.join(__dirname, "resources/html/redirWarn.html"),
|
||
|
data: path.join(__dirname, "data"),
|
||
|
config: path.join(__dirname, "data/config.json"),
|
||
|
defaultConfig: path.join(__dirname, "resources/json/config.json"),
|
||
|
urls: path.join(__dirname, "data/urls.json")
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
paths,
|
||
|
config: undefined,
|
||
|
urls: [],
|
||
|
writeUrls: function (props) {
|
||
|
var urls = module.exports.urls
|
||
|
props.shortCode = 0
|
||
|
|
||
|
for (var i in urls) {
|
||
|
props.shortCode = urls[i].shortCode + 1
|
||
|
}
|
||
|
|
||
|
if (props.shortCode > 100000) {
|
||
|
props.shortCode = 0
|
||
|
}
|
||
|
|
||
|
urls.push(props)
|
||
|
|
||
|
var expirePoint = 0
|
||
|
|
||
|
for (var i in urls) {
|
||
|
var url = urls[i]
|
||
|
if (url.expire < Date.now()) {
|
||
|
expirePoint = i
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
|
||
|
urls.splice(0, expirePoint)
|
||
|
|
||
|
fs.writeFileSync(paths.urls, JSON.stringify(module.exports.urls))
|
||
|
return props
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var currentConfig = JSON.parse(fs.readFileSync(paths.config))
|
||
|
var defaultConfig = JSON.parse(fs.readFileSync(paths.defaultConfig))
|
||
|
currentConfig = Object.assign({}, defaultConfig, currentConfig)
|
||
|
fs.writeFileSync(paths.config, JSON.stringify(currentConfig))
|
||
|
|
||
|
var watcher = chokidar.watch(paths.data)
|
||
|
|
||
|
function dataUpdate() {
|
||
|
module.exports.config = JSON.parse(fs.readFileSync(paths.config))
|
||
|
}
|
||
|
|
||
|
watcher
|
||
|
.on("change", dataUpdate)
|
||
|
.on("add", dataUpdate)
|
||
|
|
||
|
if (!fs.existsSync(paths.urls)) {
|
||
|
fs.writeFileSync(paths.urls, "[]")
|
||
|
}
|
||
|
|
||
|
module.exports.urls = JSON.parse(fs.readFileSync(paths.urls))
|