KawAnime/assets/scripts/init/openExternal.js

83 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-04-15 14:16:14 +00:00
/**
* Created by Kylart on 04/04/2017.
*/
const {join} = require('path')
const shell = require('electron').shell
2017-04-27 19:05:55 +00:00
const {dialog, BrowserWindow} = require('electron')
2017-04-15 14:16:14 +00:00
const fs = require('fs')
const qs = require('querystring')
2017-04-27 19:05:55 +00:00
const sendEmptyRes = (res) => {
res.writeHead(200, {})
res.end()
}
const sendRes = (res, data) => {
res.writeHead(200, {"Content-Type": "application/json"})
res.write(JSON.stringify(data))
res.end()
}
2017-04-27 20:48:03 +00:00
exports.openExternal = (url, res) => {
2017-04-15 14:16:14 +00:00
const query = qs.parse(url.query.replace('?', ''))
const type = query.type
console.log('[Open-External] Got a request for external open: type is ' + type)
switch (type)
{
case 'dummy':
shell.openExternal('http://hestia.dance')
break
case 'video':
2017-04-27 19:05:55 +00:00
shell.openItem(join(query.dir, query.path));
sendEmptyRes(res)
2017-04-15 14:16:14 +00:00
break
case 'link':
shell.openExternal(query.link)
2017-04-27 19:05:55 +00:00
sendEmptyRes(res)
break
case 'insideLink':
const win = new BrowserWindow({
parent: process.win,
x: 50,
y: 50,
minimizable: false,
maximizable: false,
resizable: false
})
win.loadURL(query.link)
2017-04-15 14:16:14 +00:00
break
case 'delete':
fs.unlink(join(query.dir, query.path), (err) => {
if (err) throw err
console.log('[Open-External] Deleted file successfully.')
})
2017-04-27 19:05:55 +00:00
sendEmptyRes(res)
break
case 'dialog':
dialog.showOpenDialog({properties: ['openDirectory']}, (dirPath) => {
if (dirPath !== undefined)
{
const result = {
path: dirPath[0]
}
sendRes(res, result)
}
})
2017-04-15 14:16:14 +00:00
break
default:
break
}
}