diff --git a/CHANGELOG.md b/CHANGELOG.md index 515d7117..2165eadc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - launcher: spaces not properly escaped in quoted strings - widgets - sliders: `default` and `spring` properties are now dynamic +- server + - images urls are now resolved relatively to the session path directory by default (fallback to absolute path resolution if not found) ## 0.46.2 diff --git a/src/client/app/ipc/index.js b/src/client/app/ipc/index.js index 15415bd2..4ae293a5 100644 --- a/src/client/app/ipc/index.js +++ b/src/client/app/ipc/index.js @@ -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) diff --git a/src/client/app/managers/session.js b/src/client/app/managers/session.js index 11928db1..084c2255 100644 --- a/src/client/app/managers/session.js +++ b/src/client/app/managers/session.js @@ -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 } diff --git a/src/server/callbacks.js b/src/server/callbacks.js index 805b47c7..3ae5a688 100644 --- a/src/server/callbacks.js +++ b/src/server/callbacks.js @@ -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) }) diff --git a/src/server/server.js b/src/server/server.js index 9bbfc9e7..bd4bfabc 100644 --- a/src/server/server.js +++ b/src/server/server.js @@ -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() } } + } }