Fix exception caused by incorrect file ordering

See
https://github.com/feross/webtorrent-desktop/pull/604#issuecomment-22280
5214
This commit is contained in:
Feross Aboukhadijeh
2016-05-31 13:15:48 -07:00
parent 2c0de25423
commit 4ee36f459f

View File

@@ -13,7 +13,7 @@ var config = require('../../config')
// Change `state.saved` (which will be saved back to config.json on exit) as // Change `state.saved` (which will be saved back to config.json on exit) as
// needed, for example to deal with config.json format changes across versions // needed, for example to deal with config.json format changes across versions
function run (state) { function run (state) {
// Migration: replace "{ version: 1 }" with app version (semver) // Replace "{ version: 1 }" with app version (semver)
if (!semver.valid(state.saved.version)) { if (!semver.valid(state.saved.version)) {
state.saved.version = '0.0.0' // Pre-0.7.0 version, so run all migrations state.saved.version = '0.0.0' // Pre-0.7.0 version, so run all migrations
} }
@@ -39,7 +39,7 @@ function migrate_0_7_0 (state) {
state.saved.torrents.forEach(function (ts) { state.saved.torrents.forEach(function (ts) {
var infoHash = ts.infoHash var infoHash = ts.infoHash
// Migration: replace torrentPath with torrentFileName // Replace torrentPath with torrentFileName
var src, dst var src, dst
if (ts.torrentPath) { if (ts.torrentPath) {
// There are a number of cases to handle here: // There are a number of cases to handle here:
@@ -47,7 +47,7 @@ function migrate_0_7_0 (state) {
// * Then, relative paths for the default torrents, eg '../static/sintel.torrent' // * Then, relative paths for the default torrents, eg '../static/sintel.torrent'
// * Then, paths computed at runtime for default torrents, eg 'sintel.torrent' // * Then, paths computed at runtime for default torrents, eg 'sintel.torrent'
// * Finally, now we're getting rid of torrentPath altogether // * Finally, now we're getting rid of torrentPath altogether
console.log('migration: replacing torrentPath %s', ts.torrentPath) console.log('replacing torrentPath %s', ts.torrentPath)
if (path.isAbsolute(ts.torrentPath)) { if (path.isAbsolute(ts.torrentPath)) {
src = ts.torrentPath src = ts.torrentPath
} else if (ts.torrentPath.startsWith('..')) { } else if (ts.torrentPath.startsWith('..')) {
@@ -64,9 +64,9 @@ function migrate_0_7_0 (state) {
ts.torrentFileName = infoHash + '.torrent' ts.torrentFileName = infoHash + '.torrent'
} }
// Migration: replace posterURL with posterFileName // Replace posterURL with posterFileName
if (ts.posterURL) { if (ts.posterURL) {
console.log('migration: replacing posterURL %s', ts.posterURL) console.log('replacing posterURL %s', ts.posterURL)
var extension = path.extname(ts.posterURL) var extension = path.extname(ts.posterURL)
src = path.isAbsolute(ts.posterURL) src = path.isAbsolute(ts.posterURL)
? ts.posterURL ? ts.posterURL
@@ -80,9 +80,11 @@ function migrate_0_7_0 (state) {
ts.posterFileName = infoHash + extension ts.posterFileName = infoHash + extension
} }
// Migration: add per-file selections // Fix exception caused by incorrect file ordering.
if (!ts.selections && ts.files) { // https://github.com/feross/webtorrent-desktop/pull/604#issuecomment-222805214
ts.selections = ts.files.map((x) => true) delete ts.defaultPlayFileIndex
} delete ts.files
delete ts.selections
delete ts.fileModtimes
}) })
} }