fd5ef14999
Security & correctness hardening: - Gate /__api/token/auth behind auth and self-scope every handler to the caller (was fully unauthenticated — account-takeover hole). - Escape LDAP filter values (injection) and reject empty-password binds. - Enforce per-torrent ownership so private torrents aren't exposed via IDOR. - Assorted cleanup: fix 'use static' typos, drop dead Torrent.migrate + the getTorrentData noUpdate flag, Buffer.alloc, __dirname-relative reads, res.statusCode in the error handler, const-scope pubsub. Login-gated proxy + anti-indexing: - Block all proxying for logged-out users via an auth-token cookie the front end mirrors from its token; serve a local login page instead of hitting TPB. - robots.txt disallow-all + X-Robots-Tag noindex. Torrent category: - Store a normalized category (TV/Movie/Music/Adult/App/Game/Other) mapped from the TPB category id; captured at add time (migration). Smart Search (movies/TV): - New /__api/search: TMDB title confirm -> scrape piratebay.party HTML -> Ollama ranks releases against quality prefs (x265/1080p/~1.5GB/subs, prefer uncut) returning a recommended pick, optional warned 4K, and other editions. Front-end Smart Search box + dialog feeding the existing add flow. Post-download organization -> Emby (public Movie/TV only): - Completion watcher files finished torrents: Ollama parses the release name, TMDB canonicalizes title/year, files main video (+subs) into the library with edition/quality-aware names (movies + TV SxxExx), stops seeding, and triggers an Emby library scan. Low-confidence matches are flagged, not mis-filed; correctable via "Fix match". Adds organizedAt/metadata columns. Shared helpers: controller/tmdb.js, controller/ollama.js. Config blocks for tmdb/ollama/search/emby/library/organize (secrets stay in gitignored secrets.js). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
675 lines
21 KiB
HTML
675 lines
21 KiB
HTML
<link rel="stylesheet" href="/__static-modules/jquery-ui/dist/themes/smoothness/jquery-ui.css">
|
|
|
|
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
|
|
<script src='/__static-modules/mustache/mustache.min.js'></script>
|
|
<script src="/__static/lib/js/jq-repeat.js"></script>
|
|
<script src="/__static-modules/moment/min/moment-with-locales.min.js"></script>
|
|
<script src="/__static-modules/jquery-ui/dist/jquery-ui.min.js"></script>
|
|
<script src="/__static/js/app.js"></script>
|
|
|
|
<style type="text/css">
|
|
.ui-dialog{
|
|
padding: 0;
|
|
}
|
|
.ui-dialog .ui-dialog-title{
|
|
width: unset;
|
|
}
|
|
|
|
#tbp_proxy_header {
|
|
position: fixed;
|
|
top: 0;
|
|
width: 100%;
|
|
z-index: 95;
|
|
background: lightblue;
|
|
height: 3em;
|
|
text-align: initial;
|
|
padding-top: .5em;
|
|
padding-right: 1em;
|
|
}
|
|
|
|
#tbp_proxy_header_right{
|
|
margin-right: 2em;
|
|
float: right;
|
|
|
|
display: flex;
|
|
align-items:center;
|
|
}
|
|
|
|
#tbp_proxy_torrent_dialog_opener{
|
|
border-radius: 25px;
|
|
background: lightseagreen;
|
|
display: flex;
|
|
align-items:center;
|
|
|
|
padding: 1em;
|
|
padding-top: .3em;
|
|
padding-bottom: .3em;
|
|
|
|
margin-right: .5em;
|
|
}
|
|
|
|
#header {
|
|
padding-top: 3.5em;
|
|
}
|
|
</style>
|
|
|
|
<!--
|
|
Dialog boxes to be displayed
|
|
|
|
Login Dialog
|
|
-->
|
|
|
|
<div id="tbp_proxy_login_dialog" title="SSO Login">
|
|
<div class="shadow-lg card">
|
|
<div class="card-header shadow actionMessage" style="display:none"></div>
|
|
<div class="card-body">
|
|
<form action="auth/login" onsubmit="formAJAX(this)" evalAJAX="
|
|
app.auth.setToken(data.token);
|
|
app.auth.logInRedirect();
|
|
">
|
|
<input type="hidden" name="redirect" value="<%= redirect %>">
|
|
|
|
<div class="form-group">
|
|
<label class="control-label">User name</label>
|
|
<div class="input-group mb-3 shadow">
|
|
<div class="input-group-prepend">
|
|
<span class="input-group-text" ><i class="fa-solid fa-user-tie"></i></span>
|
|
</div>
|
|
<input type="text" name="uid" class="form-control" placeholder="jsmith" />
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="control-label">Password</label>
|
|
<div class="input-group mb-3 shadow">
|
|
<div class="input-group-prepend">
|
|
<span class="input-group-text" ><i class="fa-solid fa-key"></i></span>
|
|
</div>
|
|
<input type="password" name="password" class="form-control" placeholder="hunter123!"/>
|
|
</div>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-outline-dark"><i class="fa-solid fa-right-to-bracket"></i> Log in</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<!--
|
|
Torrent List Dialog
|
|
-->
|
|
|
|
<style type="text/css">
|
|
#tbp_proxy_torrent_dialog{
|
|
padding: 0;
|
|
}
|
|
|
|
#tbp_proxy_torrent_dialog progress{
|
|
width: 100%;
|
|
height: 2em;
|
|
}
|
|
|
|
#tbp_proxy_torrent_dialog ul{
|
|
height: 400px;
|
|
/*overflow-y: scroll;*/
|
|
list-style-type: none;
|
|
padding-left: 0;
|
|
margin-top: 0;
|
|
margin-bottom: 0;
|
|
}
|
|
|
|
#tbp_proxy_torrent_dialog li{
|
|
padding-left: 1em;
|
|
padding-right: 1em;
|
|
}
|
|
|
|
#tbp_proxy_torrent_dialog li p{
|
|
margin: .3em;
|
|
}
|
|
</style>
|
|
|
|
<div id="tbp_proxy_torrent_dialog" title="Torrents">
|
|
<ul>
|
|
<li jq-repeat="tbp_proxy_torrent_dialog_torrents" jq-repeat-index="hashString">
|
|
<p>
|
|
<b>{{name}}</b> - <i>{{statusText}}</i>
|
|
</p>
|
|
<p>
|
|
Is <b>{{sizeWhenDone}}</b>
|
|
saved to <b>{{downloadDir}}</b>
|
|
added by <b>{{added_by}}</b>
|
|
<b>{{createdAtString}}</b>
|
|
</p>
|
|
|
|
{{^isFinished}}
|
|
<p>
|
|
<progress id="file" max="100" value="{{percentDone}}">{{percentDone}}%</progress>
|
|
</p>
|
|
|
|
{{#isActive}}
|
|
<p>
|
|
<b>{{rateDownload}}</b>
|
|
Finishing <b>{{eta}}</b>
|
|
From <b>{{peersConnected}}</b> Peers
|
|
</p>
|
|
|
|
<button class="ui-button ui-widget ui-corner-all" onclick="app.torrent.stop({{hashString}})">
|
|
<span class="ui-icon ui-icon-pause"></span>Pause
|
|
</button>
|
|
{{/isActive}}
|
|
|
|
{{^isActive}}
|
|
<button class="ui-button ui-widget ui-corner-all" onclick="app.torrent.start({{hashString}})">
|
|
<span class="ui-icon ui-icon-play"></span> Start
|
|
</button>
|
|
{{/isActive}}
|
|
|
|
<button class="ui-button ui-widget ui-corner-all" onclick="
|
|
app.torrent.destroy({{hashString}}, function(error, data){
|
|
$.scope.tbp_proxy_torrent_dialog_torrents.splice({{__id}}, 1);
|
|
});
|
|
">
|
|
<span class="ui-icon ui-icon-closethick"></span>Delete
|
|
</button>
|
|
</p>
|
|
{{/isFinished}}
|
|
|
|
{{#errorString}}
|
|
<p>
|
|
<b>{{errorString}}</b>
|
|
</p>
|
|
{{/errorString}}
|
|
|
|
{{#isFinished}}
|
|
<p>
|
|
Done! <a href="https://stuff.718it.biz/torrents/{{name}}" target="_blank"> HTTP Link</a>
|
|
</p>
|
|
{{#organized}}
|
|
<p>📁 Filed to <b>{{libraryPath}}</b></p>
|
|
{{/organized}}
|
|
{{#organizeError}}
|
|
<p style="color:#b00">⚠ Not filed: {{organizeError}}</p>
|
|
{{/organizeError}}
|
|
<button class="ui-button ui-widget ui-corner-all" onclick="tbpFixMatch('{{hashString}}')">
|
|
<span class="ui-icon ui-icon-pencil"></span> Fix match
|
|
</button>
|
|
{{/isFinished}}
|
|
<hr />
|
|
</li>
|
|
<li jq-repeat-defualt="tbp_proxy_torrent_dialog_torrents">
|
|
<h3> No Torrents...</h3>
|
|
<hr width="300pt" />
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
|
|
|
|
<!--
|
|
Torrent Add Dialog
|
|
-->
|
|
<style type="text/css">
|
|
#tbp_proxy_torrent_add_dialog_container{
|
|
width: 32em;
|
|
}
|
|
|
|
#tbp_proxy_torrent_add_dialog input[type="text"]{
|
|
width: 90%;
|
|
}
|
|
#tbp_proxy_torrent_add_dialog label,legend{
|
|
font-weight: bold;
|
|
}
|
|
|
|
</style>
|
|
|
|
<div id="tbp_proxy_torrent_add_dialog" title="Add torrent">
|
|
<div id="tbp_proxy_torrent_add_dialog_container" class='card'>
|
|
<div class="card-header shadow actionMessage" style="display:none"></div>
|
|
<div jq-repeat="torrentAdd">
|
|
<form action="torrent" method="post" onsubmit="formAJAX(this)" evalAJAX="
|
|
app.publish('torrent:add', {...data, __noSocket: true});
|
|
$('#tbp_proxy_torrent_add_dialog').dialog('close');
|
|
openDialog($('#tbp_proxy_torrent_dialog'));
|
|
savePrivateState(data, this);
|
|
">
|
|
<p>
|
|
<label for="_name">Name:</label>
|
|
<br />
|
|
<input type="text" name="_name" value="{{{name}}}" readonly/>
|
|
</p>
|
|
|
|
<p>
|
|
<label for="magnetLink">Magnet Link:</label>
|
|
<br />
|
|
<input type="text" name="magnetLink" value="{{{magnetLink}}}" readonly/>
|
|
</p>
|
|
|
|
<p>
|
|
<label for="hashString">Hash:</label>
|
|
<br />
|
|
<input type="text" name="hashString" value="{{{hashString}}}" readonly/>
|
|
</p>
|
|
|
|
<input type="hidden" name="category" value="{{{category}}}"/>
|
|
|
|
<p>
|
|
<label for="isPrivate-false" title="The download will appare in the communal download folder">Public:</label>
|
|
<input type="radio" name="isPrivate" id="isPrivate-false" value="false" />
|
|
|
|
<label for="isPrivate-true" title="Only you(and the admins) will be able to see this download">Private:</label>
|
|
<input type="radio" name="isPrivate" id="isPrivate-true" value="true" />
|
|
</p>
|
|
|
|
<p style="display:none">
|
|
<legend>Start on add:</legend>
|
|
|
|
<label for="isStart-1" title="The download will appare in the communal download folder">Yes</label>
|
|
<input type="radio" name="isStart" id="isStart-1" value="true" checked readonly/>
|
|
|
|
<label for="isStart-2" title="Only you(and the admins) will be able to see this download">No</label>
|
|
<input type="radio" name="isStart" id="isStart-2" value="false" readonly/>
|
|
</p>
|
|
|
|
<hr />
|
|
<button type="submit">Start Download</button>
|
|
<button type="reset" onclick="$('#tbp_proxy_torrent_add_dialog').dialog('close')">Cancel</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div id="tbp_proxy_search_dialog" title="Smart Search">
|
|
<div id="tbp_proxy_search_dialog_body"></div>
|
|
</div>
|
|
|
|
|
|
<!--
|
|
Injected Header bar
|
|
-->
|
|
|
|
<div id="tbp_proxy_header_right">
|
|
<form id="tbp_proxy_search_form" class="tbp_proxy_is_authed" style="display:inline-block; margin-right:.5em;" onsubmit="return false;">
|
|
<input type="text" id="tbp_proxy_search_input" placeholder="Smart search movies & TV…" />
|
|
<button type="submit" class="ui-button ui-corner-all ui-widget">Search</button>
|
|
</form>
|
|
<span id="tbp_proxy_torrent_dialog_opener" class="tbp_proxy_is_authed">
|
|
<img src="/__static/img/Transmission_Icon.svg" height="22" width="22" style="margin-right: .3em;" />
|
|
<span jq-repeat="tbp_proxy_torrent_dialog_opener_status">
|
|
<b> <span class="ui-icon ui-icon-arrowthick-1-s"></span>{{downloadSpeed}} <span class="ui-icon ui-icon-arrowthick-1-n"></span>{{uploadSpeed}}</b>
|
|
</span>
|
|
</span>
|
|
<button id="tbp_proxy_login_dialog_opener" class="tbp_proxy_not_authed ui-button ui-corner-all ui-widget">Login</button>
|
|
<button class="tbp_proxy_is_authed ui-button ui-corner-all ui-widget"
|
|
onclick="app.auth.logOut(e => window.location.href='/')">Logout</button>
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
$(document).ready(function() {
|
|
var commonDialogOptions = {
|
|
position: { my: 'left top', at: 'left bottom', of: '#tbp_proxy_header_right' },
|
|
autoOpen: false,
|
|
resizable: false,
|
|
closeOnEscape: true,
|
|
draggable: false,
|
|
// maxWidth: document.body.clientWidth,
|
|
// width: 'auto',
|
|
};
|
|
|
|
/* Login Button and dialog*/
|
|
$('#tbp_proxy_login_dialog').dialog(commonDialogOptions);
|
|
|
|
$('#tbp_proxy_login_dialog_opener').on('click', function() {
|
|
openDialog($('#tbp_proxy_login_dialog'));
|
|
});
|
|
|
|
|
|
/* Torrent list button and dialog */
|
|
$('#tbp_proxy_torrent_dialog').dialog(commonDialogOptions);
|
|
|
|
$('#tbp_proxy_torrent_dialog_opener').on('click', function() {
|
|
if($('#tbp_proxy_torrent_dialog').dialog('isOpen')){
|
|
$('#tbp_proxy_torrent_dialog').dialog('close')
|
|
}else{
|
|
openDialog($('#tbp_proxy_torrent_dialog'));
|
|
}
|
|
});
|
|
|
|
|
|
/* Torrent add button and dialog */
|
|
$('#tbp_proxy_torrent_add_dialog').dialog(commonDialogOptions);
|
|
|
|
|
|
/* Smart Search button and dialog */
|
|
$('#tbp_proxy_search_dialog').dialog(commonDialogOptions);
|
|
|
|
// Escape untrusted text before dropping it into HTML.
|
|
function tbpEsc(text){ return $('<i>').text(text == null ? '' : text).html(); }
|
|
function tbpSearchBody(html){ $('#tbp_proxy_search_dialog_body').html(html); }
|
|
|
|
// Step 1: render the TMDB title candidates to confirm which one they meant.
|
|
// onPick defaults to the search flow; the "Fix match" flow passes its own.
|
|
function tbpRenderTitles(results, onPick){
|
|
onPick = onPick || tbpFindReleases;
|
|
if(!results.length){ tbpSearchBody('<p>No matches found.</p>'); return; }
|
|
tbpSearchBody('<p>Which one did you mean?</p>');
|
|
results.forEach(function(r){
|
|
var poster = r.posterUrl ? '<img src="'+ r.posterUrl +'" width="60" style="vertical-align:middle;margin-right:.5em;"/>' : '';
|
|
$('<div class="tbp_search_card" style="cursor:pointer;padding:.4em;border-bottom:1px solid #ccc;"></div>')
|
|
.html(poster +'<b>'+ tbpEsc(r.title) +'</b> ('+ tbpEsc(r.year || '?') +') <span style="color:#888">'+ tbpEsc(r.mediaType) +'</span>')
|
|
.on('click', function(){ onPick(r); })
|
|
.appendTo('#tbp_proxy_search_dialog_body');
|
|
});
|
|
}
|
|
|
|
// "Fix match": re-run the title picker for a finished torrent and re-file it under
|
|
// the chosen TMDB match via POST /torrent/:hash/organize/match.
|
|
window.tbpFixMatch = function(hash){
|
|
var item = ($.scope.tbp_proxy_torrent_dialog_torrents || []).filter(function(t){ return t.hashString === hash; })[0];
|
|
var query = (item ? item.name : '').replace(/[._]/g, ' ').replace(/\b(19|20)\d\d\b.*/, function(m){ return m.slice(0, 4); }).trim();
|
|
|
|
openDialog($('#tbp_proxy_search_dialog'));
|
|
tbpSearchBody('<p>Searching for a better match…</p>');
|
|
app.api.get('search/title?q='+ encodeURIComponent(query || (item ? item.name : '')), function(error, data){
|
|
if(error || !data || !data.results){ tbpSearchBody('<p>Search failed. Please try again.</p>'); return; }
|
|
tbpRenderTitles(data.results, function(r){
|
|
tbpSearchBody('<p>Re-filing as <b>'+ tbpEsc(r.title) +' ('+ tbpEsc(r.year) +')</b>…</p>');
|
|
app.api.post('torrent/'+ hash +'/organize/match', { tmdbId: r.tmdbId, mediaType: r.mediaType }, function(err, res){
|
|
if(err || !res || res.organized !== true){
|
|
tbpSearchBody('<p>Re-file failed: '+ tbpEsc((res && res.message) || err) +'</p>'); return;
|
|
}
|
|
tbpSearchBody('<p>✓ Re-filed to <b>'+ tbpEsc(res.libraryPath) +'</b>. Emby will re-index.</p>');
|
|
});
|
|
});
|
|
});
|
|
};
|
|
|
|
// Step 2: ask the server( TPB + LLM) for the curated release options.
|
|
function tbpFindReleases(title){
|
|
tbpSearchBody('<p>Finding the best copies for <b>'+ tbpEsc(title.title) +'</b>…<br/>this can take a few seconds.</p>');
|
|
app.api.post('search/releases', { tmdbId: title.tmdbId, mediaType: title.mediaType }, function(error, data){
|
|
if(error || !data || !data.options){ tbpSearchBody('<p>Search failed. Please try again.</p>'); return; }
|
|
tbpRenderReleases(data);
|
|
});
|
|
}
|
|
|
|
// Step 3: render the release cards; a tap feeds the existing add dialog.
|
|
function tbpRenderReleases(data){
|
|
if(!data.options.length){ tbpSearchBody('<p>No good torrents found for '+ tbpEsc(data.title) +'.</p>'); return; }
|
|
tbpSearchBody('<h3>'+ tbpEsc(data.title +' ('+ (data.year || '?') +')') +'</h3>');
|
|
data.options.forEach(function(o){
|
|
var warn = o.warning ? '<div style="color:#b00;font-weight:bold;">⚠ '+ tbpEsc(o.warning) +'</div>' : '';
|
|
var why = o.why ? '<div style="color:#555;font-style:italic;">'+ tbpEsc(o.why) +'</div>' : '';
|
|
$('<div class="tbp_search_card" style="cursor:pointer;padding:.5em;border-bottom:1px solid #ccc;"></div>')
|
|
.html('<b>'+ tbpEsc(o.label || o.role) +'</b> <span style="color:#888">'+ tbpEsc(o.sizeHuman || '') +' · '+ tbpEsc(o.seeders || 0) +' seeders</span>'
|
|
+ '<div style="font-size:.9em">'+ tbpEsc(o.name) +'</div>' + why + warn)
|
|
.on('click', function(){ tbpAddRelease(o); })
|
|
.appendTo('#tbp_proxy_search_dialog_body');
|
|
});
|
|
}
|
|
|
|
// Reuse the existing add flow: fill the torrentAdd scope and open the add dialog.
|
|
function tbpAddRelease(o){
|
|
$.scope.torrentAdd.update({
|
|
magnetLink: o.magnetLink,
|
|
name: o.name,
|
|
hashString: String(o.info_hash).toLowerCase(),
|
|
category: o.category,
|
|
});
|
|
if(localStorage.getItem('isPrivate') === 'true'){
|
|
$('#isPrivate-true').prop('checked', true);
|
|
}else{
|
|
$('#isPrivate-false').prop('checked', true);
|
|
}
|
|
$('#tbp_proxy_search_dialog').dialog('close');
|
|
openDialog($('#tbp_proxy_torrent_add_dialog'));
|
|
}
|
|
|
|
$('#tbp_proxy_search_form').on('submit', function(){
|
|
var q = $('#tbp_proxy_search_input').val();
|
|
if(!q) return false;
|
|
openDialog($('#tbp_proxy_search_dialog'));
|
|
tbpSearchBody('<p>Searching…</p>');
|
|
app.api.get('search/title?q='+ encodeURIComponent(q), function(error, data){
|
|
if(error || !data || !data.results){ tbpSearchBody('<p>Search failed. Please try again.</p>'); return; }
|
|
tbpRenderTitles(data.results);
|
|
});
|
|
return false;
|
|
});
|
|
|
|
|
|
/* Enable tooltips*/
|
|
$('#tbp_proxy_header').tooltip({
|
|
track: true
|
|
});
|
|
|
|
app.auth.isLoggedIn(function(error, data){
|
|
if(data){
|
|
|
|
$('body').on('click', 'img.718link', function(event){
|
|
// magnetLink
|
|
let magnetLinkParams = new URLSearchParams($(this).data('link'));
|
|
|
|
// Grab the TPB category id from the /browse/<id> link in this
|
|
// torrent's row( listing) or the Type: field( detail page).
|
|
let $cat = $(this).closest('tr').find('a[href*="/browse/"]').first();
|
|
if(!$cat.length) $cat = $('dd a[href*="/browse/"]').first();
|
|
let category = $cat.length ? $cat.attr('href').replace(/.*\/browse\//, '').replace(/\D.*$/, '') : '';
|
|
|
|
$.scope.torrentAdd.update({
|
|
magnetLink: $(this).data('link'),
|
|
name: magnetLinkParams.get('dn'),
|
|
hashString: magnetLinkParams.get('magnet:?xt').split(':').pop().toLowerCase(),
|
|
category: category,
|
|
});
|
|
|
|
if(localStorage.getItem('isPrivate') === 'true'){
|
|
$('#isPrivate-true').prop('checked', true);
|
|
}else{
|
|
$('#isPrivate-false').prop('checked', true);
|
|
}
|
|
|
|
openDialog($('#tbp_proxy_torrent_add_dialog'));
|
|
});
|
|
|
|
// Look for
|
|
$('a').each(function(idx, el){
|
|
var $el = $(el);
|
|
if($el.attr('href') && $el.attr('href').match('magnet:?')){
|
|
$el.before(`<img class="tbp_proxy_is_authed 718link" src="/__static/img/Transmission_Icon.svg" height=24 width=24 data-link="${$el.attr('href')}"/>`)
|
|
}
|
|
});
|
|
|
|
app.subscribe('torrent:add', function(data, topic){
|
|
$.scope.tbp_proxy_torrent_dialog_torrents.unshift(app.torrent.parseTorrnetItem(data))
|
|
});
|
|
|
|
app.subscribe('torrent:server:status', function(data, topic){
|
|
app.torrent.isDown = false
|
|
$('#tbp_proxy_torrent_dialog_opener').css('background', 'lightseagreen')
|
|
$.scope.tbp_proxy_torrent_dialog_opener_status.update(app.torrent.parseServerStatus(data));
|
|
});
|
|
|
|
app.subscribe('app:api:error:555', function(data, topics){
|
|
app.torrent.isDown = true
|
|
$('#tbp_proxy_torrent_dialog_opener').css('background', 'red')
|
|
});
|
|
|
|
app.subscribe('torrent:server:status:down', function(data, topic){
|
|
app.torrent.isDown = true
|
|
$('#tbp_proxy_torrent_dialog_opener').css('background', 'red')
|
|
});
|
|
|
|
listTorrents();
|
|
setInterval(refreshTorrents, 1000);
|
|
app.torrent.migrate();
|
|
}
|
|
});
|
|
});
|
|
|
|
function savePrivateState(data){
|
|
localStorage.setItem('isPrivate', data.isPrivate);
|
|
if(data.isPrivate){
|
|
$('#isPrivate-true').prop('checked', true);
|
|
} else {
|
|
$('#isPrivate-false').prop('checked', true);
|
|
}
|
|
}
|
|
|
|
function humanFileSize(size) {
|
|
var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
|
|
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
|
|
}
|
|
|
|
function openDialog($el){
|
|
// https://stackoverflow.com/a/6500385
|
|
$el.parent().css({
|
|
position: 'fixed',
|
|
'margin-right': '2em',
|
|
'margin-top': '3em'
|
|
}).end().dialog('open');
|
|
}
|
|
|
|
function listTorrents(){
|
|
app.torrent.list(function(err, data){
|
|
for(let torrent of data.results){
|
|
$.scope.tbp_proxy_torrent_dialog_torrents.push(app.torrent.parseTorrnetItem(torrent))
|
|
if(torrent.percentDone !== 1) app.torrent.get(function(error, torrent){
|
|
$.scope.tbp_proxy_torrent_dialog_torrents.update('hashString', torrent.result.hashString, app.torrent.parseTorrnetItem(torrent.result))
|
|
} , torrent.hashString, true)
|
|
}
|
|
});
|
|
};
|
|
|
|
function refreshTorrents(){
|
|
for(let torrent of $.scope.tbp_proxy_torrent_dialog_torrents){
|
|
if(!torrent.isFinished){
|
|
app.torrent.get(function(error, torrent){
|
|
$.scope.tbp_proxy_torrent_dialog_torrents.update('hashString', torrent.result.hashString, app.torrent.parseTorrnetItem(torrent.result))
|
|
} , torrent.hashString, true)
|
|
}
|
|
}
|
|
}
|
|
|
|
app.torrent = (function(app){
|
|
let isDown = false;
|
|
|
|
// Dont spam the server if its not online
|
|
$(document).on('ajaxSend', function(event, ajax, res, ...args) {
|
|
if(res.url.startsWith('/__api/torrent') && isDown){
|
|
ajax.abort();
|
|
}
|
|
});
|
|
|
|
statusMap = [
|
|
'Inactive', // 0
|
|
'CHECK_WAIT', // 1
|
|
'Verifying', // 2
|
|
'DOWNLOAD_WAIT', // 3
|
|
'Downloading', // 4
|
|
'SEED_WAIT', // 5
|
|
'Seeding', // 6
|
|
'ISOLATED', // 7
|
|
'Unknown', // 8
|
|
];
|
|
|
|
function list(callback, username) {
|
|
app.api.get('torrent', function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function get(callback, hashString, forceUpdate){
|
|
app.api.get(`torrent/${hashString}?${forceUpdate ? 'latest': '' }`, function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function start(hashString, callback){
|
|
app.api.post(`torrent/${hashString}/start`, function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function stop(hashString, callback){
|
|
app.api.post(`torrent/${hashString}/stop`, function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function destroy(hashString, callback){
|
|
app.api.delete(`torrent/${hashString}`, function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function parseServerStatus(data){
|
|
return {
|
|
...data,
|
|
"downloadSpeed": humanFileSize(data.downloadSpeed)+'/s',
|
|
"uploadSpeed": humanFileSize(data.uploadSpeed)+'/s',
|
|
/* 'activeTorrentCount": 11,
|
|
"cumulative-stats": {
|
|
"downloadedBytes": 2925927098021,
|
|
"filesAdded": 80609,
|
|
"secondsActive": 12136579,
|
|
"sessionCount": 21,
|
|
"uploadedBytes": 107123787853
|
|
},
|
|
"current-stats": {
|
|
"downloadedBytes": 48440590262,
|
|
"filesAdded": 544,
|
|
"secondsActive": 111907,
|
|
"sessionCount": 1,
|
|
"uploadedBytes": 461874022
|
|
},
|
|
"pausedTorrentCount": 462,
|
|
"torrentCount": 473,
|
|
"__noSocket": true*/
|
|
}
|
|
}
|
|
|
|
function parseTorrnetItem(torrent){
|
|
let percentDone = (torrent.percentDone || 0)*100;
|
|
return {
|
|
...torrent,
|
|
"eta": torrent.eta > 0 ? moment().seconds(torrent.eta).fromNow() : 'Unknown',
|
|
"rateDownload": `${humanFileSize(torrent.rateDownload)}/s`,
|
|
"sizeWhenDone": humanFileSize(torrent.sizeWhenDone),
|
|
"percentDone": percentDone,
|
|
"statusText": statusMap[torrent.status],
|
|
"isActive": [3, 4, 5, 6].includes(torrent.status), // DOWNLOAD_WAIT ,DOWNLOAD, SEED_WAIT, SEED
|
|
"isFinished": torrent.isFinished || percentDone === 100,
|
|
"createdAtString": moment(torrent.createdAt).fromNow(),
|
|
"organized": !!torrent.organizedAt,
|
|
"libraryPath": torrent.metadata && torrent.metadata.libraryPath,
|
|
"organizeError": torrent.metadata && torrent.metadata.organizeError,
|
|
}
|
|
}
|
|
|
|
function migrate(){
|
|
let torrents = JSON.parse(window.localStorage.getItem('torrents') || '{}').list || [];
|
|
|
|
if(torrents.length){
|
|
console.log(`Migrating ${torrents.length}`)
|
|
for(let torrent of torrents){
|
|
app.api.post(`torrent/${torrent.hashString}`, {}, function(error, torrent) {
|
|
if(Object.keys(torrent).length){
|
|
$.scope.tbp_proxy_torrent_dialog_torrents.unshift(app.torrent.parseTorrnetItem(torrent))
|
|
}
|
|
});
|
|
}
|
|
localStorage.removeItem('torrents',);
|
|
}
|
|
}
|
|
|
|
return {list, get, start, stop, destroy, migrate, parseTorrnetItem, parseServerStatus, isDown};
|
|
})(app);
|
|
|
|
</script>
|