From eb59c11f8566831e7efb9933c19e9cd412f0090b Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 27 Mar 2016 02:10:58 -0700 Subject: [PATCH] Resolve posterURL and torrentPath at runtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes bug where posters and torrent files can’t be found in the built app. --- renderer/index.js | 9 ++++++--- renderer/state.js | 12 ++++++------ renderer/util.js | 9 +++++++++ renderer/views/player.js | 4 +++- renderer/views/torrent-list.js | 5 ++++- 5 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 renderer/util.js diff --git a/renderer/index.js b/renderer/index.js index 6a8cfd3d..8e282173 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -22,6 +22,7 @@ var errors = require('./lib/errors') var config = require('../config') var TorrentPlayer = require('./lib/torrent-player') var torrentPoster = require('./lib/torrent-poster') +var util = require('./util') // These two dependencies are the slowest-loading, so we lazy load them // This cuts time from icon click to rendered window from ~550ms to ~150ms on my laptop @@ -478,7 +479,8 @@ function addTorrentToList (torrent) { function startTorrentingSummary (torrentSummary) { var s = torrentSummary if (s.torrentPath) { - var ret = startTorrentingID(s.torrentPath, s.path) + var torrentPath = util.getAbsoluteStaticPath(s.torrentPath) + var ret = startTorrentingID(torrentPath, s.path) if (s.infoHash) state.pendingTorrents[s.infoHash] = ret return ret } else if (s.magnetURI) { @@ -614,7 +616,7 @@ function generateTorrentPoster (torrent, torrentSummary) { fs.writeFile(posterFilePath, buf, function (err) { if (err) return onWarning(err) // show the poster - torrentSummary.posterURL = 'file:///' + posterFilePath + torrentSummary.posterURL = posterFilePath update() }) }) @@ -874,7 +876,8 @@ function saveTorrentFileAs (torrentSummary) { ] } dialog.showSaveDialog(remote.getCurrentWindow(), opts, (savePath) => { - fs.readFile(torrentSummary.torrentPath, function (err, torrentFile) { + var torrentPath = util.getAbsoluteStaticPath(torrentSummary.torrentPath) + fs.readFile(torrentPath, function (err, torrentFile) { if (err) return onError(err) fs.writeFile(savePath, torrentFile, function (err) { if (err) return onError(err) diff --git a/renderer/state.js b/renderer/state.js index 0e7e890e..1193973d 100644 --- a/renderer/state.js +++ b/renderer/state.js @@ -66,8 +66,8 @@ module.exports = { infoHash: '88594aaacbde40ef3e2510c47374ec0aa396c08e', magnetURI: 'magnet:?xt=urn:btih:88594aaacbde40ef3e2510c47374ec0aa396c08e&dn=bbb_sunflower_1080p_30fps_normal.mp4&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80%2Fannounce&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.webtorrent.io&ws=http%3A%2F%2Fdistribution.bbb3d.renderfarming.net%2Fvideo%2Fmp4%2Fbbb_sunflower_1080p_30fps_normal.mp4', displayName: 'Big Buck Bunny', - posterURL: path.join('..', 'static', 'bigBuckBunny.jpg'), - torrentPath: path.join('.', 'static', 'bigBuckBunny.torrent'), + posterURL: 'bigBuckBunny.jpg', + torrentPath: 'bigBuckBunny.torrent', files: [ { 'name': 'bbb_sunflower_1080p_30fps_normal.mp4', @@ -82,8 +82,8 @@ module.exports = { infoHash: '6a9759bffd5c0af65319979fb7832189f4f3c35d', magnetURI: 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&dn=sintel.mp4&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.webtorrent.io&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel-1024-surround.mp4', displayName: 'Sintel', - posterURL: path.join('..', 'static', 'sintel.jpg'), - torrentPath: path.join('.', 'static', 'sintel.torrent'), + posterURL: 'sintel.jpg', + torrentPath: 'sintel.torrent', files: [ { 'name': 'sintel.mp4', @@ -98,8 +98,8 @@ module.exports = { infoHash: '02767050e0be2fd4db9a2ad6c12416ac806ed6ed', magnetURI: 'magnet:?xt=urn:btih:02767050e0be2fd4db9a2ad6c12416ac806ed6ed&dn=tears_of_steel_1080p.webm&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.webtorrent.io', displayName: 'Tears of Steel', - posterURL: path.join('..', 'static', 'tearsOfSteel.jpg'), - torrentPath: path.join('.', 'static', 'tearsOfSteel.torrent'), + posterURL: 'tearsOfSteel.jpg', + torrentPath: 'tearsOfSteel.torrent', files: [ { 'name': 'tears_of_steel_1080p.webm', diff --git a/renderer/util.js b/renderer/util.js new file mode 100644 index 00000000..ea24cb06 --- /dev/null +++ b/renderer/util.js @@ -0,0 +1,9 @@ +var path = require('path') + +var config = require('../config') + +exports.getAbsoluteStaticPath = function (filePath) { + return path.isAbsolute(filePath) + ? filePath + : path.join(config.STATIC_PATH, filePath) +} diff --git a/renderer/views/player.js b/renderer/views/player.js index ea874b10..30ee49bd 100644 --- a/renderer/views/player.js +++ b/renderer/views/player.js @@ -4,6 +4,7 @@ var h = require('virtual-dom/h') var hyperx = require('hyperx') var hx = hyperx(h) +var util = require('../util') // Shows a streaming video player. Standard features + Chromecast + Airplay function Player (state, dispatch) { @@ -153,7 +154,8 @@ function renderCastScreen (state, dispatch) { function cssBackgroundImagePoster (state) { var torrentSummary = getPlayingTorrentSummary(state) if (!torrentSummary || !torrentSummary.posterURL) return '' - var cleanURL = torrentSummary.posterURL.replace(/\\/g, '/') + var posterURL = util.getAbsoluteStaticPath(torrentSummary.posterURL) + var cleanURL = posterURL.replace(/\\/g, '/') return 'radial-gradient(circle at center, ' + 'rgba(0,0,0,0.4) 0%, rgba(0,0,0,1) 100%)' + `, url(${cleanURL})` diff --git a/renderer/views/torrent-list.js b/renderer/views/torrent-list.js index 5f097c35..7d5e71f3 100644 --- a/renderer/views/torrent-list.js +++ b/renderer/views/torrent-list.js @@ -5,6 +5,8 @@ var hyperx = require('hyperx') var hx = hyperx(h) var prettyBytes = require('prettier-bytes') +var util = require('../util') + var TorrentPlayer = require('../lib/torrent-player') function TorrentList (state, dispatch) { @@ -35,9 +37,10 @@ function TorrentList (state, dispatch) { var gradient = isSelected ? 'linear-gradient(to bottom, rgba(0, 0, 0, 0.8) 0%, rgba(0, 0, 0, 0.4) 100%)' : 'linear-gradient(to bottom, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0) 100%)' + var posterURL = util.getAbsoluteStaticPath(torrentSummary.posterURL) // Work around a Chrome bug (reproduced in vanilla Chrome, not just Electron): // Backslashes in URLS in CSS cause bizarre string encoding issues - var cleanURL = torrentSummary.posterURL.replace(/\\/g, '/') + var cleanURL = posterURL.replace(/\\/g, '/') style.backgroundImage = gradient + `, url('${cleanURL}')` }