Adding a lot of js for the add torrent and add magnet link gui, started the torrent client backend.

This commit is contained in:
2017-09-16 12:52:05 -04:00
parent 5489b3d59b
commit 28e7dd9d5d
4 changed files with 200 additions and 4 deletions

89
client.go Normal file
View File

@@ -0,0 +1,89 @@
package main
import (
"fmt"
"github.com/anacrolix/torrent"
"log"
"os"
"path/filepath"
"time"
)
// ClientError formats errors coming from the client.
type ClientError struct {
Type string
Origin error
}
func (clientError ClientError) Error() string {
return fmt.Sprintf("Error %s: %s\n", clientError.Type, clientError.Origin)
}
type ClientConfig struct {
TorrentPath string
Port int
TorrentPort int
Seed bool
TCP bool
MaxConnections int
DownloadDir string
}
// NewClientConfig creates a new default configuration.
func NewClientConfig() ClientConfig {
return ClientConfig{
Port: 8080,
TorrentPort: 50007,
Seed: false,
TCP: true,
MaxConnections: 200,
DownloadDir: "Download",
}
}
type Client struct {
Client *torrent.Client
Torrent *torrent.Torrent
Name string
Progress int64
Status string
Seeds int
Peers int
DownloadSpeed int64
UploadSpeed int64
ETA time.Duration
Ratio int
Avail int
Config ClientConfig
}
func NewClient(cfg ClientConfig) (client Client, err error) {
var t *torrent.Torrent
var c *torrent.Client
client.Config = cfg
//create the download directory
_, err := os.Create(cfg.DownloadDir)
if err != nil {
log.Println(err)
return
}
c, err = torrent.NewClient(&torrent.Config{
DataDir: cfg.DownloadDir,
NoUpload: !cfg.Seed,
Seed: cfg.Seed,
DisableTCP: !cfg.TCP,
ListenAddr: fmt.Sprintf("%d", cfg.TorrentPort),
})
if err != nil {
return client, ClientError{Type: "creating torrent client", Origin: err}
}
client.Client = c
//adding torrents
}

View File

@@ -9,6 +9,7 @@ import (
"log"
"net/http"
"os"
"strings"
"time"
)
@@ -57,6 +58,8 @@ func main() {
fmt.Println(err)
return
}
} else if strings.HasPrefix(string(msg), "magnet:") {
fmt.Println(string(msg))
} else {
conn.Close()
fmt.Println(string(msg))

View File

@@ -117,4 +117,58 @@ tr:nth-child(even) {
.defaultTab {
display: initial;
}
.addTorrentModal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.addTorrentModalContent {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.addTorrentModalClose {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.addTorrentModalClose:hover,
.addTorrentModalClose:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.addTorrentFileModalClose {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.addTorrentFileModalClose:hover,
.addTorrentFileModalClose:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}

View File

@@ -18,11 +18,19 @@
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " activeButton";
}
</script>
<script type="text/javascript">
function myWebsocketStart()
{
var ws = new WebSocket("ws://192.168.1.141:8000/websocket");
var torrentLinkSubmit = document.getElementById('torrentLinkSubmit');
var magnetLink = document.getElementById('magnetLink');
var modal = document.getElementById('addTorrentModal');
var ws = new WebSocket("ws://192.168.1.141:8000/websocket");
ws.onopen = function()
{
@@ -46,11 +54,52 @@
var myTextArea = document.getElementById("loggerData");
myTextArea.innerHTML = myTextArea.innerHTML + "\n" + "Connection closed";
};
torrentLinkSubmit.onclick = function(e) {
e.preventDefault();
var magnetLinkjs = magnetLink.value;
ws.send(magnetLinkjs);
modal.style.display = "none";
}
}
function sendEvent(message)
{
ws.send(message);
}
</script>
</head>
<!-- The addlink Modal -->
<div id="addTorrentModal" class="addTorrentModal">
<!-- Modal content -->
<div class="addTorrentModalContent">
<span class="addTorrentModalClose">&times;</span>
<form id="torrentLinkForm" action="#" method="post">
Input Magnet Link: <input type="text" id="magnetLink">
<button id="torrentLinkSubmit">Submit</button>
</form>
</div>
</div>
<!-- The addTorrent Modal -->
<div id="addTorrentFileModal" class="addTorrentModal">
<!-- Modal content -->
<div class="addTorrentModalContent">
<span class="addTorrentFileModalClose">&times;</span>
Input Torrent File: <input type="file" name="torrentFile">
<button id="torrentFileSubmit">Submit</button>
</div>
</div>
<body onload="javascript:myWebsocketStart()">
<div class="wrapper">
<div class="box navcolumn">
@@ -66,7 +115,7 @@
</div>
<div class="box navsettings">
<ul class="navsettingsUl">
<li class="top" id="addTorrent"><img class="imagezoom" src="/static/images/iconAddTorrent.png"></li>
<li class="top" id="addTorrentFile"><img class="imagezoom" src="/static/images/iconAddTorrent.png"></li>
<li class="top" id="addTorrentLink"><img class="imagezoom" src="/static/images/iconAddTorrentLink.png"></li>
<li class="top verticalLine" id="deleteTorrent"><img class="imagezoom" src="/static/images/iconDelete.png"></li>
<li class="top verticalLine" id="startTorrent"><img class="imagezoom" src="/static/images/iconStart.png"></li>
@@ -82,7 +131,7 @@
<tr>
<th>Name</th>
<th>Size</th>
<th>Done</th>
<th>Progress</th>
<th>Status</th>
<th>Seeds</th>
<th>Peers</th>
@@ -151,11 +200,12 @@
<h2>Logger</h2>
<p id="loggerData">Logger lines here</p>
</div>
</div>
</div>
</body>
<script type="text/javascript" src="/static/js/addTorrents.js"></script>
<footer>Icons by <a href="https://icons8.com">icons8</a>
</html>
{{end}}