- Remove trackers from torrenst/magnet URI --------- Co-authored-by: Mukhtar Akere <akeremukhtar10@gmail.com>
1 line
5.6 KiB
JavaScript
1 line
5.6 KiB
JavaScript
class DownloadManager{constructor(e){this.downloadFolder=e,this.refs={downloadForm:document.getElementById("downloadForm"),magnetURI:document.getElementById("magnetURI"),torrentFiles:document.getElementById("torrentFiles"),arr:document.getElementById("arr"),downloadAction:document.getElementById("downloadAction"),downloadUncached:document.getElementById("downloadUncached"),rmTrackerUrls:document.getElementById("rmTrackerUrls"),downloadFolder:document.getElementById("downloadFolder"),debrid:document.getElementById("debrid"),submitBtn:document.getElementById("submitDownload"),activeCount:document.getElementById("activeCount"),completedCount:document.getElementById("completedCount"),totalSize:document.getElementById("totalSize")},this.init()}init(){this.loadSavedOptions(),this.bindEvents(),this.handleMagnetFromURL()}bindEvents(){this.refs.downloadForm.addEventListener("submit",e=>this.handleSubmit(e)),this.refs.arr.addEventListener("change",()=>this.saveOptions()),this.refs.downloadAction.addEventListener("change",()=>this.saveOptions()),this.refs.downloadUncached.addEventListener("change",()=>this.saveOptions()),this.refs.rmTrackerUrls.addEventListener("change",()=>this.saveOptions()),this.refs.downloadFolder.addEventListener("change",()=>this.saveOptions()),this.refs.torrentFiles.addEventListener("change",e=>this.handleFileSelection(e)),this.setupDragAndDrop()}loadSavedOptions(){const e={category:localStorage.getItem("downloadCategory")||"",action:localStorage.getItem("downloadAction")||"symlink",uncached:"true"===localStorage.getItem("downloadUncached"),rmTrackerUrls:"true"===localStorage.getItem("rmTrackerUrls"),folder:localStorage.getItem("downloadFolder")||this.downloadFolder};this.refs.arr.value=e.category,this.refs.downloadAction.value=e.action,this.refs.downloadUncached.checked=e.uncached,this.refs.rmTrackerUrls.checked=e.rmTrackerUrls,this.refs.downloadFolder.value=e.folder}saveOptions(){localStorage.setItem("downloadCategory",this.refs.arr.value),localStorage.setItem("downloadAction",this.refs.downloadAction.value),localStorage.setItem("downloadUncached",this.refs.downloadUncached.checked.toString()),this.refs.rmTrackerUrls.disabled||localStorage.setItem("rmTrackerUrls",this.refs.rmTrackerUrls.checked.toString()),localStorage.setItem("downloadFolder",this.refs.downloadFolder.value)}handleMagnetFromURL(){const e=new URLSearchParams(window.location.search).get("magnet");e&&(this.refs.magnetURI.value=e,history.replaceState({},document.title,window.location.pathname),window.decypharrUtils.createToast("Magnet link loaded from URL","info"))}async handleSubmit(e){e.preventDefault();const t=new FormData,r=this.refs.magnetURI.value.split("\n").map(e=>e.trim()).filter(e=>e.length>0);r.length>0&&t.append("urls",r.join("\n"));for(let e=0;e<this.refs.torrentFiles.files.length;e++)t.append("files",this.refs.torrentFiles.files[e]);const o=r.length+this.refs.torrentFiles.files.length;if(0!==o)if(o>100)window.decypharrUtils.createToast("Please submit up to 100 torrents at a time","warning");else{t.append("arr",this.refs.arr.value),t.append("downloadFolder",this.refs.downloadFolder.value),t.append("action",this.refs.downloadAction.value),t.append("downloadUncached",this.refs.downloadUncached.checked),t.append("rmTrackerUrls",this.refs.rmTrackerUrls.checked),this.refs.debrid&&t.append("debrid",this.refs.debrid.value);try{window.decypharrUtils.setButtonLoading(this.refs.submitBtn,!0);const e=await window.decypharrUtils.fetcher("/api/add",{method:"POST",body:t,headers:{}}),r=await e.json();if(!e.ok)throw new Error(r.error||"Unknown error");r.errors&&r.errors.length>0?r.results.length>0?(window.decypharrUtils.createToast(`Added ${r.results.length} torrents with ${r.errors.length} errors`,"warning"),this.showErrorDetails(r.errors)):(window.decypharrUtils.createToast("Failed to add torrents","error"),this.showErrorDetails(r.errors)):(window.decypharrUtils.createToast(`Successfully added ${r.results.length} torrent${r.results.length>1?"s":""}!`),this.clearForm())}catch(e){console.error("Error adding downloads:",e),window.decypharrUtils.createToast(`Error adding downloads: ${e.message}`,"error")}finally{window.decypharrUtils.setButtonLoading(this.refs.submitBtn,!1)}}else window.decypharrUtils.createToast("Please provide at least one torrent","warning")}showErrorDetails(e){const t=e.map(e=>`• ${e}`).join("\n");console.error("Download errors:",t),window.decypharrUtils.createToast(`Errors occurred while adding torrents:\n${t}`,"error")}clearForm(){this.refs.magnetURI.value="",this.refs.torrentFiles.value=""}handleFileSelection(e){const t=e.target.files;if(t.length>0){const e=Array.from(t).map(e=>e.name).join(", ");window.decypharrUtils.createToast(`Selected ${t.length} file${t.length>1?"s":""}: ${e}`,"info")}}setupDragAndDrop(){const e=this.refs.downloadForm;["dragenter","dragover","dragleave","drop"].forEach(t=>{e.addEventListener(t,this.preventDefaults,!1)}),["dragenter","dragover"].forEach(t=>{e.addEventListener(t,()=>this.highlight(e),!1)}),["dragleave","drop"].forEach(t=>{e.addEventListener(t,()=>this.unhighlight(e),!1)}),e.addEventListener("drop",e=>this.handleDrop(e),!1)}preventDefaults(e){e.preventDefault(),e.stopPropagation()}highlight(e){e.classList.add("border-primary","border-2","border-dashed","bg-primary/5")}unhighlight(e){e.classList.remove("border-primary","border-2","border-dashed","bg-primary/5")}handleDrop(e){const t=e.dataTransfer.files,r=Array.from(t).filter(e=>e.name.toLowerCase().endsWith(".torrent"));if(r.length>0){const e=new DataTransfer;r.forEach(t=>e.items.add(t)),this.refs.torrentFiles.files=e.files,this.handleFileSelection({target:{files:r}})}else window.decypharrUtils.createToast("Please drop .torrent files only","warning")}} |