Skip to content

Commit

Permalink
improved ui, fixed js crash, rebuild go1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jpillora committed Sep 14, 2015
1 parent de30585 commit d7ece81
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 129 deletions.
8 changes: 4 additions & 4 deletions engine/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ func (torrent *Torrent) Update(t torrent.Torrent) {
}
}
file.Completed = completed
file.Percent = percent(file.Completed, file.Chunks)
file.Percent = percent(int64(file.Completed), int64(file.Chunks))
file.f = f

totalChunks += file.Chunks
totalCompleted += file.Completed
}

torrent.Percent = percent(totalChunks, totalCompleted)
//cacluate rate
now := time.Now()
bytes := t.BytesCompleted()
torrent.Percent = percent(bytes, torrent.Size)
if !torrent.updatedAt.IsZero() {
dt := float32(now.Sub(torrent.updatedAt))
db := float32(bytes - torrent.Downloaded)
Expand All @@ -87,9 +87,9 @@ func (torrent *Torrent) Update(t torrent.Torrent) {
torrent.t = t
}

func percent(n, total int) float32 {
func percent(n, total int64) float32 {
if total == 0 {
return float32(0)
}
return float32(int(float32(10000)*(float32(n)/float32(total)))) / 100
return float32(int(float64(10000)*(float64(n)/float64(total)))) / 100
}
14 changes: 10 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
"github.com/jpillora/cloud-torrent/engine"
"github.com/jpillora/cloud-torrent/static"
"github.com/jpillora/go-realtime"
"github.com/jpillora/scraper/lib"
"github.com/jpillora/requestlog"
"github.com/jpillora/scraper/scraper"
)

//Server is the "State" portion of the diagram
Expand All @@ -26,6 +27,7 @@ type Server struct {
Host string `help:"Listening interface (default all)"`
Auth string `help:"Optional basic auth (in form user:password)"`
ConfigPath string `help:"Configuration file path"`
Log bool `help:"Enable request logging"`
//http handlers
files, static http.Handler
scraper *scraper.Handler
Expand All @@ -51,7 +53,7 @@ func (s *Server) init() error {
//will use a the local embed/ dir if it exists, otherwise will use the hardcoded embedded binaries
s.files = http.HandlerFunc(s.serveFiles)
s.static = ctstatic.FileSystemHandler()
s.scraper = &scraper.Handler{Log: true}
s.scraper = &scraper.Handler{Log: false}
if err := s.scraper.LoadConfig(defaultSearchConfig); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -104,7 +106,7 @@ func (s *Server) init() error {
// log.Printf("torrents #%d files #%d", len(s.state.Torrents), len(s.state.Downloads.Children))
s.state.Unlock()
s.state.Update()
time.Sleep(1 * time.Second)
time.Sleep(2 * time.Second)
}
}()

Expand Down Expand Up @@ -135,8 +137,12 @@ func (s *Server) Run() error {
// TODO if Open {
// cross platform open - https://github.com/skratchdot/open-golang
// }
h := http.Handler(http.HandlerFunc(s.handle))
if s.Log {
h = requestlog.Wrap(h)
}
log.Printf("Listening on %d...", s.Port)
return http.ListenAndServe(s.Host+":"+strconv.Itoa(s.Port), http.HandlerFunc(s.handle))
return http.ListenAndServe(s.Host+":"+strconv.Itoa(s.Port), h)
}

func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
Expand Down
15 changes: 11 additions & 4 deletions server/server_files.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"errors"
"fmt"
"io/ioutil"
"log"
Expand All @@ -11,6 +12,8 @@ import (
"time"
)

const fileNumberLimit = 1000

type fsNode struct {
Name string
Size int64
Expand All @@ -22,7 +25,7 @@ func (s *Server) listFiles() *fsNode {
rootDir := s.state.Config.DownloadDirectory
root := &fsNode{}
if info, err := os.Stat(rootDir); err == nil {
if err := list(rootDir, info, root); err != nil {
if err := list(rootDir, info, root, new(int)); err != nil {
log.Printf("File listing failed: %s", err)
}
}
Expand Down Expand Up @@ -56,9 +59,13 @@ func (s *Server) serveFiles(w http.ResponseWriter, r *http.Request) {

//custom directory walk

func list(path string, info os.FileInfo, node *fsNode) error {
func list(path string, info os.FileInfo, node *fsNode, n *int) error {
if (!info.IsDir() && !info.Mode().IsRegular()) || strings.HasPrefix(info.Name(), ".") {
return fmt.Errorf("Non-regular file")
return errors.New("Non-regular file")
}
(*n)++
if (*n) > fileNumberLimit {
return errors.New("Over file limit") //limit number of files walked
}
node.Name = info.Name()
node.Size = info.Size()
Expand All @@ -74,7 +81,7 @@ func list(path string, info os.FileInfo, node *fsNode) error {
for _, i := range children {
c := &fsNode{}
p := filepath.Join(path, i.Name())
if err := list(p, i, c); err != nil {
if err := list(p, i, c, n); err != nil {
continue
}
node.Size += c.Size
Expand Down
2 changes: 1 addition & 1 deletion server/server_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var defaultSearchConfig = []byte(`{
"url": "http://audiobookbay.co{{path}}",
"result": {
"infohash": "/td>([a-f0-9]+)</",
"tracker": "table tr:nth-child(1) > td:nth-child(2)"
"tracker": "table tr td:nth-child(2)"
}
}
}`)
31 changes: 27 additions & 4 deletions static/files/css/sections/torrents.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ section.torrents {
font-size: 0.75rem;
}

.torrent .info .progress {
margin: 0;
}
.torrent .info .progress .bar {
height: 10px;
}

.torrent .status {
padding-top: 10px;
display: inline;
padding: 5px;
display: inline-block;
}

.torrent .status .muted {
Expand All @@ -46,18 +53,31 @@ section.torrents {
font-size: 0.75rem;
}



.torrent .download.file .name span {
position: relative;
z-index: 1;
}

.torrent .download.file .name .percent {
display: inline-block;
width: 60px;
}


.torrent .download.file .name,
.torrent .download.file .percent {
position: relative;
overflow: hidden;
}

.torrent .download.file .progress {
.torrent .downloads .progress {
position: absolute;
top: 0;
left: 0;
width: 100%;
opacity: 0.75;
opacity: 0.55;
z-index: 0;
padding: 4px;
margin: 0;
Expand All @@ -68,6 +88,9 @@ section.torrents {
height: 100%;
}

.torrent .downloads thead tr th.size {
width: 110px;
}
.torrent .downloads tfoot tr th {
font-weight: bold;
font-size: 0.85rem;
Expand Down
12 changes: 7 additions & 5 deletions static/files/js/downloads-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ app.controller("NodeController", function($scope, $rootScope, $http, $timeout) {
if($scope.isfile() && torrents) {
for(var ih in torrents) {
var files = torrents[ih].Files;
for (var i = 0; i < files.length; i++) {
var f = files[i];
if(f.Path === path) {
n.$file = f;
break;
if(files) {
for (var i = 0; i < files.length; i++) {
var f = files[i];
if(f.Path === path) {
n.$file = f;
break;
}
}
}
if(n.$file)
Expand Down
12 changes: 8 additions & 4 deletions static/files/js/omni-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ app.controller("OmniController", function($scope, $rootScope, storage, api, sear
}
for (var i = 0; i < results.length; i++) {
var r = results[i];
//add origin to absolute urls
if(r.url && /^\//.test(r.url)) {
r.url = origin + r.url;
//add origin to path to create urls
if(r.path && /^\//.test(r.path)) {
r.url = origin + r.path;
}
$scope.results.push(r);
}
Expand All @@ -184,7 +184,11 @@ app.controller("OmniController", function($scope, $rootScope, storage, api, sear
if (data.magnet) {
magnet = data.magnet;
} else if (data.infohash) {
magnet = magnetURI(result.name, data.infohash, [{v: data.tracker }]);
//get urls from the comma separated list
var trackers = (data.tracker || "").split(",").filter(function(s){
return /^(http|udp):\/\//.test(s);
}).map(function(v) { return {v:v}; });
magnet = magnetURI(result.name, data.infohash, trackers);
} else {
$scope.omnierr = "No magnet or infohash found";
return;
Expand Down
60 changes: 0 additions & 60 deletions static/files/template/downloads.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,3 @@ <h3 class="ui header">Downloads</h3>
ng-include src="'template/download-tree.html'"></div>
</div>
</div>

<!--
<table ng-if="data.uploads && (data.uploads | keys).length > 0" class="ui unstackable compact striped uploads tcld table">
<thead>
<tr>
<th class="name">File</th>
<th class="size">Size</th>
<th></th>
</tr>
</thead>
<tbody>
<tr class="upload file" ng-repeat="(path, f) in data.uploads">
<td class="name">
<a ng-href="{{ f.url }}" target="_blank" class="path">
{{ path | filename }}
<i class="ui external link icon"></i>
</a>
<i ng-show="ext(f.url) == ext(path) &&
(ext(path) == 'mp4' ||
ext(path) == 'mp3' ||
ext(path) == 'pdf')"
ng-click="previews[path] = !previews[path]"
class="video play icon"
ng-class="{outline: previews[path]}"></i>
<div ng-if="previews[path]" ng-switch="ext(path)">
<div ng-switch-when="mp4">
<video width="100%" controls>
<source jp-src="f.url" type="video/mp4">
</video>
</div>
<div ng-switch-when="mp3">
<audio controls>
<source jp-src="f.url" type="audio/mpeg; codecs='mp3'">
</audio>
</div>
<div ng-switch-when="pdf">
<object data="{{ f.url }}" type="application/pdf"
width="100%" height="300px">
<embed jp-src="f.url" type="application/pdf" />
</object>
</div>
</div>
</td>
<td class="size">
{{ f.Size | bytes }}
</td>
<td class="controls">
<i ng-show="!f.$confirm" ng-click="f.$confirm = true"
class="red trash icon"></i>
<i ng-show="!f.$deleting && f.$confirm"
ng-click="f.$deleting = true; api('trash', {path: path})"
ng-class="{red: !f.$deleting, grey: f.$deleting}"
class="check icon"></i>
<i ng-show="f.$deleting" class="grey notched circle loading icon"></i>
</td>
</tr>
</tbody>
</table> -->
Loading

0 comments on commit d7ece81

Please sign in to comment.