功能建议:在添加种子时设置用户标签 #278

This commit is contained in:
doolb 2019-05-15 18:31:37 +08:00
parent a747a87bab
commit f1ad872b7d
6 changed files with 191 additions and 132 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,10 +1 @@
var transmission={SessionId:"",isInitialized:false,host:"",port:"9091",path:"/transmission/rpc",rpcpath:"../rpc",fullpath:"",on:{torrentCountChange:null,postError:null},username:"",password:"",_status:{stopped:0,checkwait:1,check:2,downloadwait:3,download:4,seedwait:5,seed:6,actively:101},_trackerStatus:{inactive:0,waiting:1,queued:2,active:3},options:{getFolders:true,getTarckers:true},headers:{},trackers:{},islocal:false,downloadDirs:new Array,getSessionId:function(me,callback){var settings={type:"POST",
url:this.fullpath,error:function(request,event,settings){var SessionId="";if(request.status===409&&(SessionId=request.getResponseHeader("X-Transmission-Session-Id"))){me.isInitialized=true;me.SessionId=SessionId;me.headers["X-Transmission-Session-Id"]=SessionId;if(callback)callback()}},headers:this.headers};jQuery.ajax(settings)},init:function(config,callback){jQuery.extend(this,config);if(this.username&&this.password)this.headers["Authorization"]="Basic "+(new Base64).encode(this.username+":"+this.password);
this.fullpath=this.rpcpath;this.getSessionId(this,callback)},exec:function(config,callback,tags){if(!this.isInitialized)return false;var data={method:"",arguments:{},tag:""};jQuery.extend(data,config);var settings={type:"POST",url:this.fullpath,dataType:"json",data:JSON.stringify(data),success:function(resultData,textStatus){if(callback)callback(resultData,tags)},error:function(request,event,page){var SessionId="";if(request.status===409&&(SessionId=request.getResponseHeader("X-Transmission-Session-Id"))){transmission.SessionId=
SessionId;transmission.headers["X-Transmission-Session-Id"]=SessionId;jQuery.ajax(settings)}else if(transmission.on.postError)transmission.on.postError(request)},headers:this.headers};jQuery.ajax(settings)},getStatus:function(callback){this.exec({method:"session-stats"},function(data){if(data.result=="success"){if(callback)callback(data.arguments);if(transmission.torrents.count!=data.arguments.torrentCount||transmission.torrents.activeTorrentCount!=data.arguments.activeTorrentCount||transmission.torrents.pausedTorrentCount!=
data.arguments.pausedTorrentCount){transmission.torrents.count=data.arguments.torrentCount;transmission.torrents.activeTorrentCount=data.arguments.activeTorrentCount;transmission.torrents.pausedTorrentCount=data.arguments.pausedTorrentCount;transmission._onTorrentCountChange()}}})},getSession:function(callback){this.exec({method:"session-get"},function(data){if(data.result=="success")if(callback)callback(data.arguments)})},addTorrentFromUrl:function(url,savepath,autostart,callback){if(url.match(/^[0-9a-f]{40}$/i))url=
"magnet:?xt\x3durn:btih:"+url;var options={method:"torrent-add",arguments:{filename:url,paused:!autostart}};if(savepath)options.arguments["download-dir"]=savepath;this.exec(options,function(data){switch(data.result){case "success":if(callback)if(data.arguments["torrent-added"])callback(data.arguments["torrent-added"]);else if(data.arguments["torrent-duplicate"])callback({status:"duplicate",torrent:data.arguments["torrent-duplicate"]});break;case "duplicate torrent":default:if(callback)callback(data.result);
break}})},addTorrentFromFile:function(file,savePath,paused,callback,filecount){var fileReader=new FileReader;fileReader.onload=function(e){var contents=e.target.result;var key="base64,";var index=contents.indexOf(key);if(index==-1)return;var metainfo=contents.substring(index+key.length);transmission.exec({method:"torrent-add",arguments:{metainfo:metainfo,"download-dir":savePath,paused:paused}},function(data){switch(data.result){case "success":if(callback)callback(data.arguments["torrent-added"],filecount);
break;case "duplicate torrent":if(callback)callback("duplicate");break}})};fileReader.readAsDataURL(file)},_onTorrentCountChange:function(){this.torrents.loadSimpleInfo=false;if(this.on.torrentCountChange)this.on.torrentCountChange()},removeTorrent:function(ids,removeData,callback){this.exec({method:"torrent-remove",arguments:{ids:ids,"delete-local-data":removeData}},function(data){if(callback)callback(data.result)})},getFreeSpace:function(path,callback){this.exec({method:"free-space",arguments:{"path":path}},
function(result){if(callback)callback(result)})},updateBlocklist:function(callback){this.exec({method:"blocklist-update"},function(data){if(callback)callback(data.result)})},renameTorrent:function(torrentId,oldpath,newname,callback){var torrent=this.torrents.all[torrentId];if(!torrent)return false;this.exec({method:"torrent-rename-path",arguments:{ids:[torrentId],path:oldpath||torrent.name,name:newname}},function(data){if(callback)callback(data)})},closeSession:function(callback){this.exec({method:"session-close"},
function(result){if(callback)callback(result)})}};
var transmission={SessionId:"",isInitialized:!1,host:"",port:"9091",path:"/transmission/rpc",rpcpath:"../rpc",fullpath:"",on:{torrentCountChange:null,postError:null},username:"",password:"",_status:{stopped:0,checkwait:1,check:2,downloadwait:3,download:4,seedwait:5,seed:6,actively:101},_trackerStatus:{inactive:0,waiting:1,queued:2,active:3},options:{getFolders:!0,getTarckers:!0},headers:{},trackers:{},islocal:!1,downloadDirs:new Array,getSessionId:function(t,e){var s={type:"POST",url:this.fullpath,error:function(s,n,r){var a="";409===s.status&&(a=s.getResponseHeader("X-Transmission-Session-Id"))&&(t.isInitialized=!0,t.SessionId=a,t.headers["X-Transmission-Session-Id"]=a,e&&e())},headers:this.headers};jQuery.ajax(s)},init:function(t,e){jQuery.extend(this,t),this.username&&this.password&&(this.headers.Authorization="Basic "+(new Base64).encode(this.username+":"+this.password)),this.fullpath=this.rpcpath,this.getSessionId(this,e)},exec:function(t,e,s){if(!this.isInitialized)return!1;var n={method:"",arguments:{},tag:""};jQuery.extend(n,t);var r={type:"POST",url:this.fullpath,dataType:"json",data:JSON.stringify(n),success:function(t,n){e&&e(t,s)},error:function(t,e,s){var n="";409===t.status&&(n=t.getResponseHeader("X-Transmission-Session-Id"))?(transmission.SessionId=n,transmission.headers["X-Transmission-Session-Id"]=n,jQuery.ajax(r)):transmission.on.postError&&transmission.on.postError(t)},headers:this.headers};jQuery.ajax(r)},getStatus:function(t){this.exec({method:"session-stats"},function(e){"success"==e.result&&(t&&t(e.arguments),transmission.torrents.count==e.arguments.torrentCount&&transmission.torrents.activeTorrentCount==e.arguments.activeTorrentCount&&transmission.torrents.pausedTorrentCount==e.arguments.pausedTorrentCount||(transmission.torrents.count=e.arguments.torrentCount,transmission.torrents.activeTorrentCount=e.arguments.activeTorrentCount,transmission.torrents.pausedTorrentCount=e.arguments.pausedTorrentCount,transmission._onTorrentCountChange()))})},getSession:function(t){this.exec({method:"session-get"},function(e){"success"==e.result&&t&&t(e.arguments)})},addTorrentFromUrl:function(t,e,s,n){t.match(/^[0-9a-f]{40}$/i)&&(t="magnet:?xt=urn:btih:"+t);var r={method:"torrent-add",arguments:{filename:t,paused:!s}};e&&(r.arguments["download-dir"]=e),this.exec(r,function(t){switch(t.result){case"success":n&&(t.arguments["torrent-added"]?n(t.arguments["torrent-added"]):t.arguments["torrent-duplicate"]&&n({status:"duplicate",torrent:t.arguments["torrent-duplicate"]}));break;case"duplicate torrent":default:n&&n(t.result)}})},addTorrentFromFile:function(t,e,s,n,r){var a=new FileReader;a.onload=function(t){var a=t.target.result,o=a.indexOf("base64,");if(-1!=o){var i=a.substring(o+"base64,".length);transmission.exec({method:"torrent-add",arguments:{metainfo:i,"download-dir":e,paused:s}},function(t){switch(t.result){case"success":n&&(null!=t.arguments["torrent-added"]?n(t.arguments["torrent-added"],r):null!=t.arguments["torrent-duplicate"]?n({status:"duplicate",torrent:t.arguments["torrent-duplicate"]},r):n("error"));break;case"duplicate torrent":n&&n("duplicate")}})}},a.readAsDataURL(t)},_onTorrentCountChange:function(){this.torrents.loadSimpleInfo=!1,this.on.torrentCountChange&&this.on.torrentCountChange()},removeTorrent:function(t,e,s){this.exec({method:"torrent-remove",arguments:{ids:t,"delete-local-data":e}},function(t){s&&s(t.result)})},getFreeSpace:function(t,e){this.exec({method:"free-space",arguments:{path:t}},function(t){e&&e(t)})},updateBlocklist:function(t){this.exec({method:"blocklist-update"},function(e){t&&t(e.result)})},renameTorrent:function(t,e,s,n){var r=this.torrents.all[t];if(!r)return!1;this.exec({method:"torrent-rename-path",arguments:{ids:[t],path:e||r.name,name:s}},function(t){n&&n(t)})},closeSession:function(t){this.exec({method:"session-close"},function(e){t&&t(e)})}};

View File

@ -487,7 +487,7 @@ var system = {
options: {
title: system.lang.toolbar["add-torrent"],
width: 620,
height: 300,
height: system.config.nav.labels ? 500 : 300,
resizable: true
},
datas: {
@ -1289,7 +1289,7 @@ var system = {
options: {
title: system.lang.toolbar["add-torrent"],
width: 620,
height: 400,
height: system.config.nav.labels ? 600 : 400,
resizable: true
}
});
@ -2433,7 +2433,7 @@ var system = {
return '<div class="torrent-progress" title="' + progress + '"><div class="torrent-progress-text">' + progress + '</div><div class="torrent-progress-bar ' + className + '" style="width:' + progress + ';"></div></div>';
},
// Add torrent
addTorrentsToServer: function (urls, count, autostart, savepath) {
addTorrentsToServer: function (urls, count, autostart, savepath, labels) {
//this.config.autoReload = false;
var index = count - urls.length;
var url = urls.shift();
@ -2441,11 +2441,15 @@ var system = {
this.showStatus(this.lang.system.status.queuefinish);
//this.config.autoReload = true;
this.getServerStatus();
if(labels != null)
system.saveConfig();
return;
}
this.showStatus(this.lang.system.status.queue + (index + 1) + "/" + (count) + "<br/>" + url, 0);
transmission.addTorrentFromUrl(url, savepath, autostart, function (data) {
system.addTorrentsToServer(urls, count, autostart, savepath);
system.addTorrentsToServer(urls, count, autostart, savepath, labels);
if(labels != null && data.hashString != null)
system.saveLabelsConfig(data.hashString, labels);
});
},
// Starts / pauses the selected torrent
@ -3003,6 +3007,16 @@ var system = {
}
this.saveUserConfig();
},
// Save labels config for torrent if need
saveLabelsConfig: function(hash, labels){
if(system.config.nav.labels){
if (labels.length==0) {
delete system.config.labelMaps[hash];
} else {
system.config.labelMaps[hash] = labels;
}
}
},
readUserConfig: function () {
var local = window.localStorage[this.configHead];
if (local) {

View File

@ -224,7 +224,15 @@ var transmission = {
// 添加成功
case "success":
if (callback) {
callback(data.arguments["torrent-added"], filecount);
if(data.arguments["torrent-added"] != null)
callback(data.arguments["torrent-added"], filecount);
else if (data.arguments["torrent-duplicate"] != null)
callback({
status: "duplicate",
torrent: data.arguments["torrent-duplicate"]
}, filecount);
else
callback("error");
}
break;
// 重复的种子

View File

@ -38,7 +38,21 @@
</tr>
</table>
</div>
</div>
<!-- label -->
<div data-options="region:'center'" style="padding:3px;border:0px;overflow: hidden;">
<div id="label_page" class="dialog" style="width:100%;padding:0px;height:50%;border-bottom: 1px dotted #ccc;">
<div style="position: absolute;width:50%;height: 100%;">
<div system-lang="dialog['torrent-setLabels']['available']"></div>
<div id="divAvailableList" style="padding-top: 5px;"></div>
</div>
<div style="position: absolute;left:50%;width:50%;height: 50%;border-left: 1px dotted #ccc;padding-left: 5px;">
<div system-lang="dialog['torrent-setLabels']['selected']"></div>
<div id="divSelectedList" style="padding-top: 5px;"></div>
</div>
</div>
</div>
</div>
<div data-options="region:'south',border:false" style="text-align:right;padding:6px;">
<span id="dialog-torrent-add-nosource" style="display:none;"></span>
<span id="dialog-torrent-add-queue" style="display:none;"></span>
@ -77,6 +91,64 @@
thisDialog.find("#chkautostart").prop("checked", system.serverConfig["start-added-torrents"]);
thisDialog.find("#download-dir").combobox();
// label
var labelPage = {
divSelectedList: $("#divSelectedList", thisDialog),
init: function() {
if(system.config.nav.labels){
system.resetLangText(thisDialog);
this.initLabels();
}
else{
$("#label_page").css("display", "none");
}
},
// 初始化标签列表
initLabels: function() {
var box = $("#divAvailableList", thisDialog);
for (var index = 0; index < system.config.labels.length; index++) {
var item = system.config.labels[index];
if (item) {
item.index = index;
var label = this.createLabel(item);
label.on("click", function(){
labelPage.setLabel("#divAvailableList", "#divSelectedList", $(this).data("label"));
$(this).remove();
}).appendTo(box);
}
}
},
createLabel: function(label) {
return $("<a href='javascript:void(0);' class='user-label-big label-index-"+label.index+"'/>").data("label", label).html(label.name).css({
"background-color": label.color,
"color": (getGrayLevel(label.color) > 0.5 ? "#000" : "#fff")
});
},
setLabel: function(from, to, item) {
var box = $(to, thisDialog);
var label = this.createLabel(item);
label.on("click", function(){
labelPage.setLabel(to, from, $(this).data("label"));
$(this).remove();
}).appendTo(box);
},
getLabels: function() {
if(!system.config.nav.labels)
return null;
var items = $(".user-label-big", this.divSelectedList);
var labels = [];
for (var index = 0; index < items.length; index++) {
var item = $(items[index]);
var label = item.data("label");
labels.push(label.index);
}
return labels;
}
};
labelPage.init();
// Confirm
thisDialog.find("#torrent-button-ok").click(function()
{
@ -133,7 +205,10 @@
thisDialog.find("#dialog-torrent-add-queue").html("").hide();
thisDialog.dialog("close");
}
if(system.config.nav.labels && data.hashString != null){
system.saveLabelsConfig(data.hashString, labelPage.getLabels());
system.saveConfig();
}
system.reloadData();
});
return;
@ -161,7 +236,7 @@
else if (url!="")
{
var urls = url.split("\n");
system.addTorrentsToServer(urls,urls.length,autostart,dir);
system.addTorrentsToServer(urls,urls.length,autostart,dir,labelPage.getLabels());
urls = null;
thisDialog.dialog("close");
}
@ -213,6 +288,10 @@
//alert(data);
if (callback)
callback();
if(system.config.nav.labels && data.hashString != null){
system.saveLabelsConfig(data.hashString, labelPage.getLabels());
system.saveConfig();
}
thisDialog.dialog("close");
system.reloadData();
}

View File

@ -28,6 +28,20 @@
</tr>
</table>
</div>
<!-- label -->
<div data-options="region:'center'" style="padding:3px;border:0px;overflow: hidden;">
<div id="label_page" class="dialog" style="width:100%;padding:0px;height:50%;border-bottom: 1px dotted #ccc;">
<div style="position: absolute;width:50%;height: 100%;">
<div system-lang="dialog['torrent-setLabels']['available']"></div>
<div id="divAvailableList" style="padding-top: 5px;"></div>
</div>
<div style="position: absolute;left:50%;width:50%;height: 50%;border-left: 1px dotted #ccc;padding-left: 5px;">
<div system-lang="dialog['torrent-setLabels']['selected']"></div>
<div id="divSelectedList" style="padding-top: 5px;"></div>
</div>
</div>
</div>
</div>
<div data-options="region:'south',border:false" style="text-align:right;padding:6px;">
<span id="dialog-torrent-add-queue" style="display:none;"></span>
@ -71,6 +85,61 @@
thisDialog.find("#chkautostart").prop("checked", system.serverConfig["start-added-torrents"]);
// label
var labelPage = {
divSelectedList: $("#divSelectedList", thisDialog),
init: function() {
if(system.config.nav.labels){
system.resetLangText(thisDialog);
this.initLabels();
}
else{
$("#label_page").css("display", "none");
}
},
// 初始化标签列表
initLabels: function() {
var box = $("#divAvailableList", thisDialog);
for (var index = 0; index < system.config.labels.length; index++) {
var item = system.config.labels[index];
if (item) {
item.index = index;
var label = this.createLabel(item);
label.on("click", function(){
labelPage.setLabel("#divAvailableList", "#divSelectedList", $(this).data("label"));
$(this).remove();
}).appendTo(box);
}
}
},
createLabel: function(label) {
return $("<a href='javascript:void(0);' class='user-label-big label-index-"+label.index+"'/>").data("label", label).html(label.name).css({
"background-color": label.color,
"color": (getGrayLevel(label.color) > 0.5 ? "#000" : "#fff")
});
},
setLabel: function(from, to, item) {
var box = $(to, thisDialog);
var label = this.createLabel(item);
label.on("click", function(){
labelPage.setLabel(to, from, $(this).data("label"));
$(this).remove();
}).appendTo(box);
},
getLabels: function() {
var items = $(".user-label-big", this.divSelectedList);
var labels = [];
for (var index = 0; index < items.length; index++) {
var item = $(items[index]);
var label = item.data("label");
labels.push(label.index);
}
return labels;
}
};
labelPage.init();
// Confirm
thisDialog.find("#torrent-button-ok").click(function()
{
@ -108,15 +177,25 @@
var files = thisDialog.data("files");
var uploaded = 0;
var islabelChange = false;
$.each(files,function(i,item){
transmission.addTorrentFromFile(item,dir,!autostart,function(){
transmission.addTorrentFromFile(item,dir,!autostart,function(data, count){
uploaded++;
thisDialog.find("#dialog-torrent-add-queue").html(uploaded+"/"+files.length).show();
// save label for torrent
if(system.config.nav.labels && data.hashString != null){
islabelChange = true;
system.saveLabelsConfig(data.hashString, labelPage.getLabels());
}
if (uploaded==files.length)
{
thisDialog.find("#dialog-torrent-add-queue").html("").hide();
button.linkbutton({disabled:false});
thisDialog.dialog("close");
if(islabelChange)
system.saveConfig();
}
system.reloadData();
});