2021-05-17 13:12:07 +08:00
|
|
|
package doc
|
2020-03-13 10:24:49 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2021-05-17 13:12:07 +08:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/httplib"
|
|
|
|
"github.com/eventials/go-tus"
|
|
|
|
"github.com/sjqzhang/goutil"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2020-03-13 10:24:49 +08:00
|
|
|
)
|
|
|
|
|
2021-05-17 13:12:07 +08:00
|
|
|
var Url *string
|
|
|
|
var Dir *string
|
|
|
|
var Worker *int
|
|
|
|
var Scene *string
|
2020-03-13 10:24:49 +08:00
|
|
|
|
2021-05-17 13:12:07 +08:00
|
|
|
var queue chan string
|
2020-03-13 10:24:49 +08:00
|
|
|
var store tus.Store
|
|
|
|
var done chan bool = make(chan bool, 1)
|
2021-05-17 13:12:07 +08:00
|
|
|
|
2020-03-13 10:24:49 +08:00
|
|
|
var wg sync.WaitGroup = sync.WaitGroup{}
|
|
|
|
var util goutil.Common
|
|
|
|
|
|
|
|
type LeveldbStore struct {
|
|
|
|
db *leveldb.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLeveldbStore(path string) (tus.Store, error) {
|
|
|
|
db, err := leveldb.OpenFile(path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
store := &LeveldbStore{db: db}
|
|
|
|
return store, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LeveldbStore) Get(fingerprint string) (string, bool) {
|
|
|
|
url, err := s.db.Get([]byte(fingerprint), nil)
|
|
|
|
ok := true
|
|
|
|
if err != nil {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
return string(url), ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LeveldbStore) Set(fingerprint, url string) {
|
|
|
|
s.db.Put([]byte(fingerprint), []byte(url), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LeveldbStore) Delete(fingerprint string) {
|
|
|
|
s.db.Delete([]byte(fingerprint), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LeveldbStore) Close() {
|
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
|
2021-05-17 13:12:07 +08:00
|
|
|
func GetDir(dir string) []string {
|
|
|
|
return getDir(dir)
|
2020-03-13 10:24:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDir(dir string) []string {
|
|
|
|
var (
|
|
|
|
paths []string
|
|
|
|
)
|
|
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if !info.IsDir() {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendFile() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
if len(queue) <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
filePath := <-queue
|
2021-05-17 13:12:07 +08:00
|
|
|
if strings.Index(*Url, "/big/upload") > 0 {
|
2020-03-13 10:24:49 +08:00
|
|
|
bigUpload(filePath)
|
|
|
|
} else {
|
|
|
|
normalUpload(filePath)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalUpload(filePath string) {
|
|
|
|
defer func() {
|
|
|
|
if re := recover(); re != nil {
|
|
|
|
}
|
|
|
|
}()
|
2021-05-17 13:12:07 +08:00
|
|
|
req := httplib.Post(*Url)
|
2020-03-13 10:24:49 +08:00
|
|
|
req.PostFile("file", filePath) //注意不是全路径
|
|
|
|
req.Param("output", "text")
|
|
|
|
req.Param("scene", "")
|
2021-05-17 13:12:07 +08:00
|
|
|
path := strings.Replace(filePath, *Dir, "", 1)
|
2020-03-13 10:24:49 +08:00
|
|
|
filename := filepath.Base(path)
|
|
|
|
path = strings.Replace(filepath.Dir(path), "\\", "/", -1)
|
2021-05-17 13:12:07 +08:00
|
|
|
req.Param("path", *Scene+"/"+strings.TrimLeft(path, "/"))
|
2020-03-13 10:24:49 +08:00
|
|
|
req.Param("filename", filename)
|
|
|
|
req.Retries(-1)
|
|
|
|
if s, err := req.String(); err != nil {
|
|
|
|
fmt.Println(err, filePath)
|
|
|
|
} else {
|
|
|
|
fmt.Println(s, filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func bigUpload(filePath string) {
|
|
|
|
defer func() {
|
|
|
|
if re := recover(); re != nil {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
f, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
cfg := tus.DefaultConfig()
|
|
|
|
//cfg.Store=store
|
|
|
|
//cfg.Resume=true
|
2021-05-17 13:12:07 +08:00
|
|
|
client, err := tus.NewClient(*Url, cfg)
|
2020-03-13 10:24:49 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
upload, err := tus.NewUploadFromFile(f)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
uploader, err := client.CreateOrResumeUpload(upload)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
url := uploader.Url()
|
|
|
|
err = uploader.Upload()
|
|
|
|
fmt.Println(url, filePath)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-17 13:12:07 +08:00
|
|
|
func StartWorker() {
|
2020-03-13 10:24:49 +08:00
|
|
|
defer func() {
|
|
|
|
if re := recover(); re != nil {
|
|
|
|
}
|
|
|
|
}()
|
2021-05-17 13:12:07 +08:00
|
|
|
for i := 0; i < *Worker; i++ {
|
2020-03-13 10:24:49 +08:00
|
|
|
go sendFile()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 13:12:07 +08:00
|
|
|
func StartServer() {
|
2020-03-13 10:24:49 +08:00
|
|
|
st := time.Now()
|
2021-05-17 13:12:07 +08:00
|
|
|
files := getDir(*Dir)
|
2020-03-13 10:24:49 +08:00
|
|
|
wg.Add(len(files))
|
|
|
|
queue = make(chan string, len(files))
|
|
|
|
for i := 0; i < len(files); i++ {
|
|
|
|
queue <- files[i]
|
|
|
|
}
|
2021-05-17 13:12:07 +08:00
|
|
|
StartWorker()
|
2020-03-13 10:24:49 +08:00
|
|
|
wg.Wait()
|
|
|
|
fmt.Println(time.Since(st))
|
|
|
|
}
|
2021-05-17 13:12:07 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
util = goutil.Common{}
|
|
|
|
defaultTransport := &http.Transport{
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
Dial: httplib.TimeoutDialer(time.Second*15, time.Second*300),
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
MaxIdleConnsPerHost: 100,
|
|
|
|
}
|
|
|
|
settings := httplib.BeegoHTTPSettings{
|
|
|
|
UserAgent: "Go-FastDFS",
|
|
|
|
ConnectTimeout: 15 * time.Second,
|
|
|
|
ReadWriteTimeout: 15 * time.Second,
|
|
|
|
Gzip: true,
|
|
|
|
DumpBody: true,
|
|
|
|
Transport: defaultTransport,
|
|
|
|
}
|
|
|
|
httplib.SetDefaultSetting(settings)
|
|
|
|
//store,_=NewLeveldbStore("upload.db")
|
|
|
|
}
|