/
file_selector.js
210 lines (182 loc) · 8.45 KB
/
file_selector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
(function (App) {
'use strict';
var that,
magnetName,
importedTorrent,
backdrop,
formatMagnet;
var FileSelector = Marionette.View.extend({
template: '#file-selector-tpl',
className: 'file-selector',
events: {
'click .close-icon': 'closeSelector',
'click .file-item *': 'startStreaming',
'contextmenu .file-item a': 'copytoclip',
'click .store-torrent': 'storeTorrent',
'click .playerchoicemenu li a': 'selectPlayer',
'click .playerchoicehelp': 'showPlayerList',
'click .playerchoicerefresh': 'refreshPlayerList'
},
initialize: function () {
that = this;
magnetName = Settings.droppedMagnetName;
delete(Settings.droppedMagnetName);
importedTorrent = Settings.importedTorrent;
delete(Settings.importedTorrent);
!that.model.get('localFile') ? that.model.set('localFile', false) : null;
formatMagnet = function (link) {
// format magnet with Display Name
var index = Settings.droppedMagnet.indexOf('\&dn=') !== -1 ? link.indexOf('\&dn=') + 4 : link.indexOf('btih:') + 5,
_link = link.substring(index); // remove everything before dn
_link = _link.split('\&'); // array of strings starting with &
_link = _link[0]; // keep only the first (i.e: display name)
_link = _link.replace(/\+/g, '.'); // replace + by .
_link = _link.replace(/%5B/g, '[').replace(/%5D/g, ']');
_link = _link.replace(/%28/g, '(').replace(/%29/g, ')');
link = _link.replace(/\W$/, ''); // remove trailing non-word char
return link;
};
},
onAttach: function () {
this.isTorrentStored();
Mousetrap.bind(['esc', 'backspace'], function (e) {
that.closeSelector(e);
});
App.Device.Collection.setDevice(Settings.chosenPlayer);
App.Device.ChooserView('#player-chooser2').render();
this.$('#watch-now').text('');
if (!$.trim($('.file-selector-container .file-list').html()).length) {
$('.file-selector-container .file-list').html('<li style="margin-top: 30px">' + i18n.__('No results found') + '</li>');
}
backdrop = !importedTorrent && $('.shb-img')[0] && $('.shb-img')[0].style ? $('.shb-img')[0].style.backgroundImage : null;
$('.file-selector-backdrop').css('background-image', backdrop);
this.$('.tooltipped').tooltip({
html: true,
delay: {
'show': 800,
'hide': 100
}
});
},
startStreaming: function (e) {
$('.tooltipped').tooltip('hide');
if (that.model.get('localFile')) {
App.vent.trigger('stream:start', that.model, 'local');
return App.vent.trigger('system:closeFileSelector');
}
var torrent = that.model.get('torrent');
var file = $(e.currentTarget).parent().attr('data-file');
var torrentStart = new Backbone.Model({
torrent: torrent.magnetURI,
backdrop: backdrop ? backdrop.replace('url("', '').replace('")', '') : null,
torrent_read: true,
file_name: file,
device: App.Device.Collection.selected
});
App.vent.trigger('stream:start', torrentStart, !$(e.currentTarget).hasClass('item-download') ? '' : 'downloadOnly' );
App.vent.trigger('system:closeFileSelector');
if ($(e.currentTarget).hasClass('item-download')) {
if (Settings.showSeedboxOnDlInit) {
if (App.currentview !== 'Torrent-collection') {
App.previousview = App.currentview;
App.currentview = 'Seedbox';
}
App.vent.trigger('seedbox:show');
$('.filter-bar').find('.active').removeClass('active');
$('#filterbar-seedbox').addClass('active');
$('#nav-filters, .right .search').hide();
} else {
$('.notification_alert').stop().text(i18n.__('Download added')).fadeIn('fast').delay(1500).fadeOut('fast');
}
}
},
copytoclip: (e) => Common.openOrClipboardLink(e, $(e.target)[0].textContent, i18n.__('filename'), true),
isTorrentStored: function () {
var target = App.settings['databaseLocation'] + '/TorrentCollection/';
// bypass errors
if (!Settings.droppedTorrent && !Settings.droppedMagnet) {
$('.store-torrent').hide();
return false;
}
var file, _file;
if (Settings.droppedTorrent) {
file = Settings.droppedTorrent;
} else if (Settings.droppedMagnet && !Settings.droppedStoredMagnet) {
_file = Settings.droppedMagnet,
file = magnetName || formatMagnet(_file);
} else if (Settings.droppedMagnet && Settings.droppedStoredMagnet) {
file = Settings.droppedStoredMagnet;
}
// check if torrent stored
if (!fs.existsSync(target + file)) {
$('.store-torrent').text(i18n.__('Store this torrent'));
return false;
} else {
$('.store-torrent').text(i18n.__('Remove this torrent'));
return true;
}
},
storeTorrent: function () {
var source = App.settings.tmpLocation + '/',
target = App.settings['databaseLocation'] + '/TorrentCollection/',
file,
_file;
if (Settings.droppedTorrent) {
file = Settings.droppedTorrent;
if (this.isTorrentStored()) {
fs.unlinkSync(target + file); // remove the torrent
win.info('Torrent Collection: deleted', file);
} else {
if (!fs.existsSync(target)) {
fs.mkdir(target); // create directory if needed
}
fs.writeFileSync(target + file, fs.readFileSync(source + file)); // save torrent
win.info('Torrent Collection: added', file);
}
} else if (Settings.droppedMagnet) {
_file = Settings.droppedMagnet,
file = magnetName || formatMagnet(_file);
if (this.isTorrentStored()) {
if (Settings.droppedStoredMagnet) {
file = Settings.droppedStoredMagnet;
}
fs.unlinkSync(target + file); // remove the magnet
win.info('Torrent Collection: deleted', file);
} else {
if (!fs.existsSync(target)) {
fs.mkdir(target); // create directory if needed
}
fs.writeFileSync(target + file, _file); // save magnet link inside readable file
win.info('Torrent Collection: added', file);
}
}
this.isTorrentStored(); // trigger button change
if (App.currentview === 'Torrent Collection' || App.currentview === 'Torrent-collection') {
App.vent.trigger('torrentCollection:show'); // refresh collection
}
},
selectPlayer: function (e) {
Common.selectPlayer(e, that.model);
},
showPlayerList: function () {
Common.showPlayerList();
},
refreshPlayerList: function (e) {
Common.refreshPlayerList(e);
},
closeSelector: function (e) {
$('.filter-bar').show();
$('#header').removeClass('header-shadow');
App.vent.trigger('system:closeFileSelector');
},
onBeforeDestroy: function () {
Settings.droppedTorrent = false;
Settings.droppedMagnet = false;
Settings.droppedStoredMagnet = false;
//Clean TorrentCache
App.Providers.TorrentCache().clearTmpDir();
App.Providers.TorrentCache()._checkTmpDir();
},
});
App.View.FileSelector = FileSelector;
})(window.App);