@@ -5,14 +5,20 @@ import (
55 "crypto/rand"
66 "crypto/sha1"
77 "encoding/base64"
8- random "math/rand"
9- "runtime"
108 "errors"
119 "flag"
1210 "fmt"
13-
11+ "github.com/astaxie/beego/httplib"
12+ "github.com/deckarep/golang-set"
13+ "github.com/json-iterator/go"
14+ log "github.com/sjqzhang/seelog"
15+ "github.com/syndtr/goleveldb/leveldb"
16+ "github.com/tus/tusd"
17+ "github.com/tus/tusd/filestore"
1418 "io"
1519 "io/ioutil"
20+ slog "log"
21+ random "math/rand"
1622 "mime/multipart"
1723 "net"
1824 "net/http"
@@ -25,6 +31,7 @@ import (
2531 "path/filepath"
2632 "reflect"
2733 "regexp"
34+ "runtime"
2835 "runtime/debug"
2936 "strconv"
3037 "strings"
@@ -33,14 +40,6 @@ import (
3340 "syscall"
3441 "time"
3542 "unsafe"
36-
37- "github.com/astaxie/beego/httplib"
38- "github.com/deckarep/golang-set"
39- "github.com/json-iterator/go"
40- log "github.com/sjqzhang/seelog"
41- "github.com/syndtr/goleveldb/leveldb"
42- "github.com/tus/tusd"
43- "github.com/tus/tusd/filestore"
4443)
4544
4645var staticHandler http.Handler
@@ -62,12 +61,13 @@ var (
6261
6362 DOCKER_DIR = ""
6463
65- STORE_DIR = "files"
66- STORE_DIR_NAME = "files"
64+ STORE_DIR = STORE_DIR_NAME
6765
68- CONF_DIR = "conf"
66+ CONF_DIR = CONF_DIR_NAME
6967
70- DATA_DIR = "data"
68+ LOG_DIR = LOG_DIR_NAME
69+
70+ DATA_DIR = DATA_DIR_NAME
7171
7272 LARGE_DIR_NAME = "haystack"
7373
@@ -107,6 +107,10 @@ var (
107107)
108108
109109const (
110+ STORE_DIR_NAME = "files"
111+ LOG_DIR_NAME = "log"
112+ DATA_DIR_NAME = "data"
113+ CONF_DIR_NAME = "conf"
110114 CONST_STAT_FILE_COUNT_KEY = "fileCount"
111115
112116 CONST_STAT_FILE_TOTAL_SIZE_KEY = "totalSize"
@@ -525,6 +529,31 @@ func (this *Common) GetUUID() string {
525529
526530}
527531
532+ func (this * Common ) CopyFile (src , dst string ) (int64 , error ) {
533+ sourceFileStat , err := os .Stat (src )
534+ if err != nil {
535+ return 0 , err
536+ }
537+
538+ if ! sourceFileStat .Mode ().IsRegular () {
539+ return 0 , fmt .Errorf ("%s is not a regular file" , src )
540+ }
541+
542+ source , err := os .Open (src )
543+ if err != nil {
544+ return 0 , err
545+ }
546+ defer source .Close ()
547+
548+ destination , err := os .Create (dst )
549+ if err != nil {
550+ return 0 , err
551+ }
552+ defer destination .Close ()
553+ nBytes , err := io .Copy (destination , source )
554+ return nBytes , err
555+ }
556+
528557func (this * Common ) RandInt (min , max int ) int {
529558
530559 return func (min , max int ) int {
@@ -3370,9 +3399,10 @@ func init() {
33703399 DOCKER_DIR = DOCKER_DIR + "/"
33713400 }
33723401 }
3373- STORE_DIR = DOCKER_DIR + "files"
3374- CONF_DIR = DOCKER_DIR + "conf"
3375- DATA_DIR = DOCKER_DIR + "data"
3402+ STORE_DIR = DOCKER_DIR + STORE_DIR_NAME
3403+ CONF_DIR = DOCKER_DIR + CONF_DIR_NAME
3404+ DATA_DIR = DOCKER_DIR + DATA_DIR_NAME
3405+ LOG_DIR = DOCKER_DIR + LOG_DIR_NAME
33763406 LARGE_DIR_NAME = "haystack"
33773407 LARGE_DIR = STORE_DIR + "/haystack"
33783408 CONST_LEVELDB_FILE_NAME = DATA_DIR + "/fileserver.db"
@@ -3488,27 +3518,116 @@ func (this *Server) test() {
34883518}
34893519
34903520func (this * Server ) initTus () {
3521+
3522+ var (
3523+ err error
3524+ fileLog * os.File
3525+ )
3526+
3527+ BIG_DIR := STORE_DIR + "/_big/" + Config ().PeerId
3528+ os .MkdirAll (BIG_DIR , 0775 )
34913529 store := filestore.FileStore {
3492- Path : STORE_DIR ,
3530+ Path : BIG_DIR ,
3531+ }
3532+
3533+ if fileLog , err = os .OpenFile (LOG_DIR + "/tusd.log" , os .O_CREATE | os .O_RDWR , 0666 ); err != nil {
3534+ log .Error (err )
3535+ fmt .Println (err )
3536+ panic ("initTus" )
3537+ }
3538+
3539+ go func () {
3540+ for {
3541+ if fi , err := fileLog .Stat (); err != nil {
3542+ log .Error (err )
3543+
3544+ } else {
3545+ if fi .Size () > 1024 * 1024 * 500 { //500M
3546+ this .util .CopyFile (LOG_DIR + "/tusd.log" , LOG_DIR + "/tusd.log.2" )
3547+ fileLog .Truncate (0 )
3548+ fileLog .Seek (0 , 0 )
3549+ }
3550+ }
3551+ time .Sleep (time .Second * 30 )
3552+ }
3553+ }()
3554+
3555+ l := slog .New (fileLog , "[tusd] " , slog .LstdFlags )
3556+
3557+ bigDir := "/big/upload/"
3558+ if Config ().SupportGroupManage {
3559+ bigDir = fmt .Sprintf ("/%s/big/upload/" , Config ().Group )
34933560 }
3494- bigDir := "/big/"
34953561 composer := tusd .NewStoreComposer ()
34963562 store .UseIn (composer )
34973563 handler , err := tusd .NewHandler (tusd.Config {
3498- BasePath : bigDir ,
3499- StoreComposer : composer ,
3564+ Logger : l ,
3565+ BasePath : bigDir ,
3566+ StoreComposer : composer ,
3567+ NotifyCompleteUploads : true ,
35003568 })
3569+
3570+ notify := func (handler * tusd.Handler ) {
3571+
3572+ for {
3573+ select {
3574+ case info := <- handler .CompleteUploads :
3575+ log .Info ("CompleteUploads" , info )
3576+ name := ""
3577+ if v , ok := info .MetaData ["filename" ]; ok {
3578+ name = v
3579+ }
3580+ var err error
3581+ md5sum := ""
3582+ oldFullPath := BIG_DIR + "/" + info .ID + ".bin"
3583+ if md5sum , err = this .util .GetFileSumByName (oldFullPath , Config ().FileSumArithmetic ); err != nil {
3584+ log .Error (err )
3585+ continue
3586+ }
3587+ if fi , err := this .GetFileInfoFromLevelDB (md5sum ); err != nil {
3588+ log .Error (err )
3589+ } else {
3590+ if fi .Md5 != "" {
3591+ log .Info (fmt .Sprintf ("file is found md5:%s" , fi .Md5 ))
3592+ continue
3593+ }
3594+ }
3595+ timeStamp := time .Now ().Unix ()
3596+ path := time .Now ().Format ("/20060102/15/04/" )
3597+ newFullPath := STORE_DIR + "/" + Config ().DefaultScene + path + Config ().PeerId + "/" + md5sum + ".bin"
3598+ infoFullPath := STORE_DIR + "/" + Config ().DefaultScene + path + Config ().PeerId + "/" + info .ID + ".info"
3599+ path = STORE_DIR_NAME + "/" + Config ().DefaultScene + path + Config ().PeerId
3600+ os .MkdirAll (path , 0775 )
3601+ fileInfo := & FileInfo {
3602+ Name : name ,
3603+ Path : path ,
3604+ ReName : md5sum + ".bin" ,
3605+ Size : info .Size ,
3606+ TimeStamp : timeStamp ,
3607+ Md5 : md5sum ,
3608+ Peers : []string {this .host },
3609+ OffSet : - 1 ,
3610+ }
3611+ if err = os .Rename (oldFullPath , newFullPath ); err != nil {
3612+ log .Error (err )
3613+ continue
3614+ }
3615+ os .Remove (infoFullPath )
3616+ this .postFileToPeer (fileInfo )
3617+ this .SaveFileMd5Log (fileInfo , CONST_FILE_Md5_FILE_NAME )
3618+ }
3619+ }
3620+ }
3621+
3622+ go notify (handler )
3623+
35013624 if err != nil {
35023625 log .Error (err )
35033626
35043627 }
35053628
3506- if Config ().SupportGroupManage {
3507- http .Handle ("/" + Config ().Group + bigDir , http .StripPrefix ("/" + Config ().Group + bigDir , handler ))
3508- } else {
3509- http .Handle (bigDir , http .StripPrefix (bigDir , handler ))
3629+ http .Handle (bigDir , http .StripPrefix (bigDir , handler ))
35103630
3511- }
35123631}
35133632
35143633func (this * Server ) FormatStatInfo () {
0 commit comments