KawAnime/server/openExternal.js

79 lines
1.6 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 {dialog, BrowserWindow, shell} = require('electron')
2017-04-15 14:16:14 +00:00
const fs = require('fs')
2017-04-27 19:05:55 +00:00
const sendEmptyRes = (res) => {
res.status(200).send()
2017-04-27 19:05:55 +00:00
}
const sendRes = (res, data) => {
res.type('application/json')
res.status(200).send(JSON.stringify(data))
2017-04-27 19:05:55 +00:00
}
exports.openExternal = (query, res) => {
2017-04-15 14:16:14 +00:00
const type = query.type
console.log('[Open-External] Got a request for external open: type is ' + type)
2017-05-08 13:38:26 +00:00
switch (type) {
2017-04-15 14:16:14 +00:00
case 'video':
2017-05-08 13:18:10 +00:00
shell.openItem(join(query.dir, query.path))
2017-04-27 19:05:55 +00:00
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) => {
2017-05-08 13:38:26 +00:00
if (dirPath !== undefined) {
2017-04-27 19:05:55 +00:00
const result = {
path: dirPath[0]
}
sendRes(res, result)
}
})
2017-04-15 14:16:14 +00:00
break
default:
break
}
}
exports.openInBrowser = (res) => {
shell.openExternal(process.appURL)
2017-05-13 14:38:17 +00:00
if (process.win) process.win.close()
res.status(200).send()
2017-05-08 13:18:10 +00:00
}