Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/451 keep seeding after closing player #474

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<script src="httpapi.js"></script>
<script src="language.js"></script>
<script src="lib/streamer.js"></script>
<script src="lib/seeder.js"></script>

<script src="lib/device/generic.js"></script>
<script src="lib/device/airplay.js"></script>
Expand Down Expand Up @@ -127,19 +128,20 @@
<script src="lib/views/player/loading.js"></script>
<script src="lib/views/player/player.js"></script>

<script src="lib/views/browser/generic_browser.js"></script>
<script src="lib/views/browser/generic_browser.js"></script>
<script src="lib/views/browser/movie_browser.js"></script>
<script src="lib/views/browser/filter_bar.js"></script>
<script src="lib/views/browser/item.js"></script>
<script src="lib/views/browser/list.js"></script>

<script src="lib/views/browser/show_browser.js"></script>
<script src="lib/views/browser/anime_browser.js"></script>
<script src="lib/views/browser/anime_browser.js"></script>
<script src="lib/views/browser/indie_browser.js"></script>

<script src="lib/views/browser/favorite_browser.js"></script>
<script src="lib/views/browser/watchlist_browser.js"></script>
<script src="lib/views/browser/watchlist_browser.js"></script>

<script src="bootstrap.js"></script>

</body>
</html>
60 changes: 60 additions & 0 deletions src/app/lib/seeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
(function (App) {

var Seeder = function () {
this.worker = null;
};

Seeder.prototype = {
start: function() {
this.getWorkerInstance().send({action: 'start'});
},

stop: function() {
if (this.getWorkerInstance()) {
this.getWorkerInstance().send({action: 'stop'});
this.worker = null;
}
},

append: function(torrent) {
this.getWorkerInstance().send({action: 'append', payload: torrent.name});
},

save: function(torrent) {
var targetFile = path.join(App.settings.tmpLocation, 'TorrentCache', Common.md5(torrent.name) + '.torrent');
var wstream = fs.createWriteStream(targetFile);

wstream.write(torrent.torrentFile);
wstream.end();
},

getWorkerInstance: function () {
if (this.worker === null) {
var taskFile = 'src/app/lib/workers/seederTask.js';
// TODO: AdvSettings here should be trigger creation of worker instance
var args = JSON.stringify({
connectionLimit: Settings.connectionLimit,
trackerAnnouncement: Settings.trackers.forced,
tmpLocation: App.settings.tmpLocation,
seedLimit: Settings.seedLimit
});

this.worker = child.fork(taskFile, [args], {silent: true, execPath:'node'});

this.worker.on('message', function(msg) {
win.info(msg);
});
}

return this.worker;
}
};

var seeder = new Seeder();

App.vent.on('seed:start', seeder.start.bind(seeder));
App.vent.on('seed:stop', seeder.stop.bind(seeder));
App.vent.on('seed:save', seeder.save.bind(seeder));
App.vent.on('seed:append', seeder.append.bind(seeder));

})(window.App);
7 changes: 7 additions & 0 deletions src/app/lib/streamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
this.setModels(model);

this.fetchTorrent(this.torrentModel.get('torrent')).then(function (torrent) {
if (AdvSettings.get('autoSeed')) {
App.vent.trigger('seed:save', torrent);
}
this.handleTorrent(torrent);
this.watchState();
this.handleStreamInfo();
Expand All @@ -54,6 +57,10 @@

// kill the streamer
stop: function() {
if (AdvSettings.get('autoSeed')) {
App.vent.trigger('seed:append', this.torrentModel.get('torrent'));
}

if (this.webtorrent) {
// update ratio
AdvSettings.set('totalDownloaded', Settings.totalDownloaded + this.torrentModel.get('torrent').downloaded);
Expand Down
9 changes: 4 additions & 5 deletions src/app/lib/views/main_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,17 @@
$('.events').css('display', 'block');
}

if (AdvSettings.get('autoSeed')) {
App.vent.trigger('seed:start');
}

// set player from settings
var players = App.Device.Collection.models;
for (var i in players) {
if (players[i].id === AdvSettings.get('chosenPlayer')) {
App.Device.Collection.setDevice(AdvSettings.get('chosenPlayer'));
}
}

// Focus the window when the app opens
win.focus();

});

// Cancel all new windows (Middle clicks / New Tab)
Expand All @@ -258,7 +258,6 @@

App.vent.trigger('updatePostersSizeStylesheet');
App.vent.trigger('main:ready');

},

movieTabShow: function (e) {
Expand Down
8 changes: 8 additions & 0 deletions src/app/lib/views/settings_container.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
case 'opensubtitlesAutoUpload':
case 'subtitles_bold':
case 'rememberFilters':
case 'autoSeed':
value = field.is(':checked');
break;
case 'httpApiUsername':
Expand Down Expand Up @@ -292,6 +293,13 @@

syncSetting: function (setting, value) {
switch (setting) {
case 'autoSeed':
if (value) {
App.vent.trigger('seed:start');
} else {
App.vent.trigger('seed:stop');
}
break;
case 'coversShowRating':
if (value) {
$('.rating').show();
Expand Down
115 changes: 115 additions & 0 deletions src/app/lib/workers/seederTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
'use strict';

var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var WebTorrent = require('webtorrent');
var child = require('child_process');
var crypt = require('crypto');

var SeederTask = function (opt) {
this.webtorrent = null;
this.torrentFiles = null;
this.tmpLocation = opt.tmpLocation;
this.seedLimit = opt.seedLimit;
this.connectionLimit = opt.connectionLimit;
this.trackerAnnouncement = opt.trackerAnnouncement;
this.torrentDir = path.join(this.tmpLocation, 'TorrentCache');
};

SeederTask.prototype = {
start: function() {
if (this.webtorrent) {
this.stop();
}

process.send('Seeding started');

var seedTorrentFiles = this.seedLimit ?
_.sample(this.getTorrentFiles(), this.seedLimit) : this.getTorrentFiles();

seedTorrentFiles.forEach(this.joinSwarm.bind(this));
},

stop: function() {
if (this.webtorrent) {
this.webtorrent.destroy();
}

this.webtorrent = null;
this.torrentFiles = [];

process.send('Seeding stopped');
process.kill();
},

getTorrentFiles: function() {
if (this.torrentFiles === null) {
var regexp = /\.torrent$/i;
var files = fs.readdirSync(this.torrentDir);

this.torrentFiles = files.filter(function(val) {
return regexp.test(val);
}).map(function(filename) {
return path.join(this.torrentDir, filename);
}, this);
}

return this.torrentFiles;
},

joinSwarm: function (torrentId) {
var client = this.getWebTorrentInstance();

if (!client.get(torrentId)) {
var torrent = client.add(torrentId, {
path: this.tmpLocation
});

torrent.on('ready', function () {
process.send(`Seeding ${torrent.name} --- ${torrentId}`);
});
}
},

append: function(name) {
var targetFile = path.join(this.torrentDir, this._md5(name) + '.torrent');

this.torrentFiles.push(targetFile);
this.seedLimit += 1;
this.joinSwarm(targetFile);
},

_md5: function (arg) {
return crypt.createHash('md5').update(arg).digest('hex');
},

getWebTorrentInstance: function() {
if (this.webtorrent === null) {
this.webtorrent = new WebTorrent({
maxConns: parseInt(this.connectionLimit, 10) || 55,
tracker: {
wrtc: false,
announce: this.trackerAnnouncement
}
});

this.webtorrent.on('error', function (error) {
process.send('WebTorrent fatal error', error);
});
}

return this.webtorrent;
}
};

var args = JSON.parse(process.argv[2]);
var seederTask = new SeederTask(args);

process.on('message', function (params) {
if (params.action && seederTask[params.action]) {
seederTask[params.action].call(seederTask, params.payload);
} else {
process.send(`Invalid seederTask function`);
}
});
2 changes: 2 additions & 0 deletions src/app/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ Settings.automaticUpdating = true;
Settings.events = true;
Settings.minimizeToTray = false;
Settings.bigPicture = false;
Settings.autoSeed = false;
Settings.seedLimit = 2;

// Features
Settings.activateTorrentCollection = true;
Expand Down
4 changes: 4 additions & 0 deletions src/app/templates/settings-container.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@
<input class="settings-checkbox" name="deleteTmpOnClose" id="cb2" type="checkbox" <%=(Settings.deleteTmpOnClose? "checked='checked'":"")%>>
<label class="settings-label" for="cb2"><%= i18n.__("Clear Tmp Folder after closing app?") %></label>
</span>
<span>
<input class="settings-checkbox" name="autoSeed" id="cb8" type="checkbox" <%=(Settings.autoSeed? "checked='checked'":"")%>>
<label class="settings-label" for="cb8"><%= i18n.__("Continue seeding from Tmp Folder") %></label>
</span>
</div>
</section>

Expand Down