KawAnime/server/openExternal.js

74 lines
1.4 KiB
JavaScript

/**
* Created by Kylart on 04/04/2017.
*/
const {join} = require('path')
const {dialog, BrowserWindow, shell} = require('electron')
const sendEmptyRes = (res) => {
res.status(200).send()
}
const sendRes = (res, data) => {
res.type('application/json')
res.status(200).send(JSON.stringify(data))
}
exports.openExternal = (query, res) => {
const type = query.type
console.log('[Open-External] Got a request for external open: type is ' + type)
switch (type) {
case 'video':
shell.openItem(join(query.dir, query.path))
sendEmptyRes(res)
break
case 'link':
shell.openExternal(query.link)
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)
break
case 'delete':
shell.moveItemToTrash(join(query.dir, query.path))
sendEmptyRes(res)
break
case 'dialog':
dialog.showOpenDialog({properties: ['openDirectory']}, (dirPath) => {
if (dirPath !== undefined) {
const result = {
path: dirPath[0]
}
sendRes(res, result)
}
})
break
default:
break
}
}
exports.openInBrowser = (res) => {
shell.openExternal(process.appURL)
if (process.win) process.win.close()
res.status(200).send()
}