Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

update #101

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ const (
const (
defaultTrackerListURL = "https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt"
defaultScraperURL = "https://raw.githubusercontent.com/boypt/simple-torrent/master/scraper-config.json"
fileSuffix = "avi|wmv|mpeg|mp4|m4v|mov|asf|flv|f4v|rmvb|rm|3gp|vob|jpe?g|png|gif|mp3|m4a"
)

type Config struct {
NoDefaultPortForwarding bool
DisableUTP bool
AutoStart bool
EngineDebug bool
MuteEngineLog bool
ObfsPreferred bool
ObfsRequirePreferred bool
DisableTrackers bool
DisableIPv6 bool
NoDefaultPortForwarding bool
DisableUTP bool
DownloadDirectory string
WatchDirectory string
EnableUpload bool
Expand All @@ -50,6 +51,7 @@ type Config struct {
ProxyURL string
RssURL string
ScraperURL string
FileSuffix string
}

func InitConf(specPath string) (*Config, error) {
Expand All @@ -74,6 +76,7 @@ func InitConf(specPath string) (*Config, error) {
viper.SetDefault("IncomingPort", 50007)
viper.SetDefault("TrackerListURL", defaultTrackerListURL)
viper.SetDefault("ScraperURL", defaultScraperURL)
viper.SetDefault("FileSuffix", fileSuffix)

// user specific config path
if stat, err := os.Stat(specPath); stat != nil && err == nil {
Expand Down
27 changes: 21 additions & 6 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package engine

import (
"bufio"
"cloud-torrent/engine/ffmpeg"
"fmt"
eglog "github.com/anacrolix/log"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"time"

eglog "github.com/anacrolix/log"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
)

const (
Expand Down Expand Up @@ -270,6 +271,11 @@ func genEnv(dir, path, hash, ttype, api string, size int64, ts int64) []string {
return env
}

func (e *Engine) Tomp4(input, output string) error {
ffmpeg.Tomp4(input, output)
return nil
}

func (e *Engine) upsertTorrent(tt *torrent.Torrent) *Torrent {
ih := tt.InfoHash().HexString()
e.RLock()
Expand Down Expand Up @@ -313,7 +319,7 @@ func (e *Engine) StartTorrent(infohash string) error {
t.Started = true
t.StartedAt = time.Now()
for _, f := range t.Files {
if f != nil {
if regexp.MustCompile(e.config.FileSuffix).MatchString(strings.ToLower(f.Path)) {
f.Started = true
}
}
Expand All @@ -323,8 +329,17 @@ func (e *Engine) StartTorrent(infohash string) error {

// start all files by setting the priority to normal
for _, f := range t.t.Files() {
f.SetPriority(torrent.PiecePriorityNormal)
log.Println(f.Path())
log.Println(regexp.MustCompile(e.config.FileSuffix).MatchString(strings.ToLower(f.Path())))
if regexp.MustCompile(e.config.FileSuffix).MatchString(strings.ToLower(f.Path())) {
f.SetPriority(torrent.PiecePriorityNormal)
} else {
f.SetPriority(torrent.PiecePriorityNone)
}
}

// call to DownloadAll cause StartFile/StopFile not working
// t.t.DownloadAll()
}
return nil
}
Expand Down
69 changes: 69 additions & 0 deletions engine/ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//go:build !windows
// +build !windows

package ffmpeg

import (
"fmt"
"github.com/xfrr/goffmpeg/transcoder"
"log"
"path"
"sync"
)

var mm = make(map[string]float64)

var mutex sync.RWMutex

func Tomp4(inputPath, outputPath string) {
log.Println("input-output:", inputPath, outputPath)
outputPath = outputPath + ".mp4"
mutex.RLock()
key := path.Base(outputPath)
defer mutex.RUnlock()
if t := mm[key]; t != 0 {
log.Println("is doing ", inputPath)
return
}

trans := new(transcoder.Transcoder)

//fpath := ffmpeg.Configuration{FfprobeBin: "E:/worktool/ffmpeg/bin/ffprobe.exe ", FfmpegBin: "E:/worktool/ffmpeg/bin/ffmpeg.exe "}
//trans.SetConfiguration(fpath)
err := trans.Initialize(inputPath, outputPath)
log.Println("err:", err)
//trans.MediaFile().SetResolution("320x240")
//trans.MediaFile().SetVideoCodec("xvid")
trans.MediaFile().SetResolution("320x240")
trans.MediaFile().SetVideoBitRate("400k")
trans.MediaFile().SetFrameRate(25)

go func() {
done := trans.Run(true)
fmt.Print(done)
progress := trans.Output()
for msg := range progress {
mm[key] = msg.Progress
fmt.Println(msg)
}
delete(mm, key)
}()

}

func ListProgress(key string) float64 {
//res := make(map[string]float64)
//for transs := range mm {
// out := mm[transs]
// num := <-out
// filenameWithSuffix := path.Base(transs) + ".mp4"
// fmt.Println("get file", filenameWithSuffix)
// res[filenameWithSuffix] = num.Progress
//}
//return res
if out := mm[key]; out != 0 {
return out
}
return 0

}
70 changes: 70 additions & 0 deletions engine/ffmpeg/ffmpeg_win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//go:build windows
// +build windows

package ffmpeg

import (
"fmt"
"github.com/xfrr/goffmpeg/ffmpeg"
"github.com/xfrr/goffmpeg/transcoder"
"log"
"path"
"sync"
)

var mm = make(map[string]float64)

var mutex sync.RWMutex

func Tomp4(inputPath, outputPath string) {
log.Println("input-output:", inputPath, outputPath)
outputPath = outputPath + ".mp4"
mutex.RLock()
key := path.Base(outputPath)
defer mutex.RUnlock()
if t := mm[key]; t != 0 {
log.Println("is doing ", inputPath)
return
}

trans := new(transcoder.Transcoder)

fpath := ffmpeg.Configuration{FfprobeBin: "E:/worktool/ffmpeg/bin/ffprobe.exe ", FfmpegBin: "E:/worktool/ffmpeg/bin/ffmpeg.exe "}
trans.SetConfiguration(fpath)
err := trans.Initialize(inputPath, outputPath)
log.Println("err:", err)
//trans.MediaFile().SetResolution("320x240")
//trans.MediaFile().SetVideoCodec("xvid")
trans.MediaFile().SetResolution("320x240")
trans.MediaFile().SetVideoBitRate("400k")
trans.MediaFile().SetFrameRate(25)

go func() {
done := trans.Run(true)
fmt.Print(done)
progress := trans.Output()
for msg := range progress {
mm[key] = msg.Progress
fmt.Println(msg)
}
delete(mm, key)
}()

}

func ListProgress(key string) float64 {
//res := make(map[string]float64)
//for transs := range mm {
// out := mm[transs]
// num := <-out
// filenameWithSuffix := path.Base(transs) + ".mp4"
// fmt.Println("get file", filenameWithSuffix)
// res[filenameWithSuffix] = num.Progress
//}
//return res
if out := mm[key]; out != 0 {
return out
}
return 0

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce // indirect
github.com/valyala/fasthttp v1.23.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xfrr/goffmpeg v0.0.0-20200825100927-5550d238df5c
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xfrr/goffmpeg v0.0.0-20200825100927-5550d238df5c h1:ydcAlElffxxmOn982VmNsx56UM2Op2OetAPura00Bec=
github.com/xfrr/goffmpeg v0.0.0-20200825100927-5550d238df5c/go.mod h1:fVs4qpwtgjOHD31cTmdHppcr/6vD8QHrAVAu2jTSVFI=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 h1:6fRhSjgLCkTD3JnJxvaJ4Sj+TYblw757bqYgZaOq5ZY=
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0/go.mod h1:/LWChgwKmvncFJFHJ7Gvn9wZArjbV5/FppcK2fKk/tI=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var VERSION = "0.0.0-src" //set with ldflags

func main() {
s := server.Server{
Title: "SimpleTorrent",
Title: "开始",
Port: 3000,
}

Expand Down
4 changes: 3 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ type Server struct {

//torrent engine
engine *engine.Engine
state struct {

state struct {
velox.State
sync.Mutex
ffm []*ffm
Config engine.Config
SearchProviders scraper.Config
Downloads *fsNode
Expand Down
4 changes: 4 additions & 0 deletions server/server_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func (s *Server) apiGET(w http.ResponseWriter, r *http.Request) error {
case "enginedebug":
w.Header().Set("Content-Type", "text/plain")
s.engine.WriteStauts(w)
case "tomp4":
path := s.engine.Config().DownloadDirectory + "/"
s.engine.Tomp4(path+r.URL.Query().Get("input"), path+r.URL.Query().Get("output"))
s.engine.WriteStauts(w)
default:
return errUnknowAct
}
Expand Down
11 changes: 10 additions & 1 deletion server/server_files.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"cloud-torrent/engine/ffmpeg"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -14,15 +15,21 @@ import (
"github.com/jpillora/archive"
)

const fileNumberLimit = 1000
const fileNumberLimit = 100000

type fsNode struct {
Name string
Size int64
Modified time.Time
Process float64
Children []*fsNode
}

type ffm struct {
File string
Size float64
}

func (s *Server) listFiles() *fsNode {
rootDir := s.state.Config.DownloadDirectory
root := &fsNode{}
Expand Down Expand Up @@ -84,6 +91,8 @@ func list(path string, info os.FileInfo, node *fsNode, n *int) error {
if (*n) > fileNumberLimit {
return errors.New("Over file limit") //limit number of files walked
}
m := ffmpeg.ListProgress(info.Name())
node.Process = m
node.Name = info.Name()
node.Size = info.Size()
node.Modified = info.ModTime()
Expand Down
20 changes: 15 additions & 5 deletions server/server_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (s *Server) webHandle(w http.ResponseWriter, r *http.Request) {
//handle realtime client library
// serve realtime client library
if r.URL.Path == "/js/velox.js" {
velox.JS.ServeHTTP(w, r)
return
Expand All @@ -18,32 +18,42 @@ func (s *Server) webHandle(w http.ResponseWriter, r *http.Request) {
s.rssh.ServeHTTP(w, r)
return
}
//handle realtime client connections
// serve realtime client connection
if r.URL.Path == "/sync" {
conn, err := velox.Sync(&s.state, w, r)
if err != nil {
log.Printf("sync failed: %s", err)
return
}

// use mutex to protect access to state.Users
s.state.Lock()
s.state.Users[conn.ID()] = r.RemoteAddr
s.state.Push()
s.state.Unlock()

conn.Wait()

// use mutex to protect access to state.Users
s.state.Lock()
delete(s.state.Users, conn.ID())
s.state.Push()
s.state.Unlock()

return
}
//search
// search
if strings.HasPrefix(r.URL.Path, "/search") {
s.scraperh.ServeHTTP(w, r)
return
}
//api call
// API calls
if strings.HasPrefix(r.URL.Path, "/api/") {
w.Header().Set("Access-Control-Allow-Headers", "authorization")
s.restAPIhandle(w, r)
return
}
//no match, assume static file
// no matching path, assume static file
s.files.ServeHTTP(w, r)
}

Expand Down
Loading