server: relative image request resolution (to session-path)

This commit is contained in:
bordun 2019-06-26 11:02:45 +02:00
parent bee8b7d814
commit e951b4bc0e
5 changed files with 35 additions and 8 deletions

View file

@ -35,6 +35,8 @@ class Ipc extends EventEmitter {
this.socket.send('["pong"]')
})
document.cookie = 'client_id=' + uuid
try {
this.open()
} catch(e) {
@ -42,6 +44,7 @@ class Ipc extends EventEmitter {
console.log(e)
}
}
init() {
@ -110,7 +113,7 @@ class Ipc extends EventEmitter {
id: 'ipc_state',
duration: Infinity
})
this.disconnected = true
clearTimeout(this.reconnect)

View file

@ -57,6 +57,7 @@ var SessionManager = class SessionManager {
loader.close()
new Popup({title: locales('session_parsingerror'), content: err.message, icon: 'exclamation-triangle', closable:true})
this.lock = false
ipc.send('sessionSetPath', {path: this.sessionPath})
throw err
}

View file

@ -88,6 +88,7 @@ module.exports = {
module.exports.fileRead(data, clientId, true, (result)=>{
ipc.clients[clientId].sessionPath = data.path // for resolving local file requests
ipc.send('sessionOpen', {path: data.path, session: result}, clientId)
})

View file

@ -1,4 +1,5 @@
var path = require('path'),
var urlparser = require('url'),
path = require('path'),
send = require('send'),
http = require('http'),
server = http.createServer(httpRoute),
@ -12,14 +13,21 @@ var path = require('path'),
clients = {}
function httpRoute(req, res) {
res.sendFile = (path)=>{
send(req, path.split('?')[0]).pipe(res)
}
if (req.url === '/' || req.url.indexOf('/?') === 0) {
var url = req.url
if (url === '/' || url.indexOf('/?') === 0) {
res.sendFile(path.resolve(__dirname + '/../client/index.html'))
} else {
if (req.url.indexOf('theme.css') != -1) {
if (url.indexOf('theme.css') != -1) {
res.setHeader('Content-Type', 'text/css')
if (settings.read('theme')) {
var str = theme.get(),
@ -29,23 +37,35 @@ function httpRoute(req, res) {
res.write('')
}
res.end()
} else if (/^\/(assets|client)\//.test(req.url)){
res.sendFile(path.resolve(__dirname + '/..' + req.url))
} else if (/^\/(assets|client)\//.test(url)){
res.sendFile(path.resolve(__dirname + '/..' + url))
} else {
var url = req.url
// windows absolute path fix
url = url.replace('_:_', ':') // escaped drive colon
url = url.replace(/^\/([^/]*:)/, '$1') // strip leading slash
if (url.match(/.(jpg|jpeg|png|apng|gif|webp|tiff|xbm|bmp|ico)(\?[0-9]*)?$/i)) {
res.sendFile(path.resolve(url))
try {
// relative resolution (session path)
var id = urlparser.parse('?' + req.headers.cookie, true).query.client_id,
sessionPath = path.dirname(ipc.clients[id].sessionPath)
res.sendFile(path.resolve(sessionPath + url))
} catch(e) {
// absolute resolution (session path)
res.sendFile(path.resolve(url))
}
} else {
res.writeHead(403)
res.end()
}
}
}
}