From 7319404ef4b256d422980d90f765f9a80e6675af Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Wed, 6 Sep 2017 02:25:02 -0700 Subject: [PATCH] Enable very minimal update checking on Linux --- app/src/browser/linux-updater-adapter.es6 | 56 +++++++++++++++---- ...er.es6 => mailspring-protocol-handler.es6} | 13 +++-- 2 files changed, 53 insertions(+), 16 deletions(-) rename app/src/browser/{merani-protocol-handler.es6 => mailspring-protocol-handler.es6} (76%) diff --git a/app/src/browser/linux-updater-adapter.es6 b/app/src/browser/linux-updater-adapter.es6 index e8bc3a904..33450ed32 100644 --- a/app/src/browser/linux-updater-adapter.es6 +++ b/app/src/browser/linux-updater-adapter.es6 @@ -1,6 +1,7 @@ import {EventEmitter} from 'events'; -// import request from 'request'; -import _ from 'underscore'; +import https from 'https'; +import shell from 'electron'; +import url from 'url'; /* Currently, this class doesn't do much. We don't display update notices within @@ -8,26 +9,61 @@ the app because we can't provide a consistent upgrade path for linux users. However, we still want the app to report to our autoupdate service so we know how many Linux users exist. */ -class LinuxUpdaterAdapter { - +class LinuxUpdaterAdapter extends EventEmitter { setFeedURL(feedURL) { this.feedURL = feedURL; + this.downloadURL = null; + } + + onError = (err) => { + this.emit('error', err.toString()); } checkForUpdates() { if (!this.feedURL) { return; } - // TODO BG I removed request! - request(this.feedURL, () => { + + this.emit('checking-for-update'); + + const feedHost = url.parse(this.feedURL).hostname; + const feedPath = this.feedURL.split(feedHost).pop(); + + // Hit the feed URL ourselves and see if an update is available. + // On linux we can't autoupdate, but we can still show the "update available" bar. + https.get({ host: feedHost, path: feedPath }, (res) => { + console.log(`Manual update check returned ${res.statusCode}`); + + if (res.statusCode === 204) { + this.emit('update-not-available'); + return; + } + + let data = ''; + res.on('error', this.onError); + res.on('data', (chunk) => { data += chunk; }); + res.on('end', () => { + try { + const json = JSON.parse(data); + console.log(JSON.stringify(json, null, 2)); + if (!json.url) { + this.onError(new Error(`Autoupdater response did not include URL: ${data}`)); + return; + } + this.downloadURL = url; + this.emit('update-downloaded', 'Click to download.', null); + } catch (err) { + this.onError(err); + } + }); }); } quitAndInstall() { - + if (this.downloadURL) { + shell.openExternal(this.downloadURL); + } } } -Object.assign(LinuxUpdaterAdapter.prototype, EventEmitter.prototype); -const adapter = new LinuxUpdaterAdapter(); -export default adapter +export default new LinuxUpdaterAdapter() diff --git a/app/src/browser/merani-protocol-handler.es6 b/app/src/browser/mailspring-protocol-handler.es6 similarity index 76% rename from app/src/browser/merani-protocol-handler.es6 rename to app/src/browser/mailspring-protocol-handler.es6 index c2a23bf6c..8e0d38c01 100644 --- a/app/src/browser/merani-protocol-handler.es6 +++ b/app/src/browser/mailspring-protocol-handler.es6 @@ -13,7 +13,7 @@ import path from 'path'; // * /packages // * RESOURCE_PATH/node_modules // -export default class NylasProtocolHandler { +export default class MailspringProtocolHandler { constructor({configDirPath, resourcePath, safeMode}) { this.loadPaths = []; @@ -23,13 +23,14 @@ export default class NylasProtocolHandler { this.loadPaths.push(path.join(configDirPath, 'packages')); this.loadPaths.push(path.join(resourcePath, 'internal_packages')); - this.registerNylasProtocol(); + this.registerProtocol(); } - // Creates the 'Nylas' custom protocol handler. - registerNylasProtocol() { - protocol.registerFileProtocol('mailspring', (request, callback) => { - const relativePath = path.normalize(request.url.substr(7)); + // Creates the 'Mailspring' custom protocol handler. + registerProtocol() { + const scheme = 'mailspring' + protocol.registerFileProtocol(scheme, (request, callback) => { + const relativePath = path.normalize(request.url.substr(scheme.length + 1)); let filePath = null; for (const loadPath of this.loadPaths) {