@@ -3,6 +3,7 @@ package main
33import (
44 "crypto/md5"
55 "crypto/rand"
6+ "crypto/sha1"
67 "encoding/base64"
78 "github.com/syndtr/goleveldb/leveldb/filter"
89 "github.com/syndtr/goleveldb/leveldb/opt"
@@ -119,7 +120,9 @@ const (
119120 "下载token过期时间":"",
120121 "download_token_expire":600,
121122 "是否自动修复":"在超过1亿文件时出现性能问题,取消此选项,请手动按天同步,请查看FAQ",
122- "auto_repair":true
123+ "auto_repair":true,
124+ "文件去重算法md5可能存在冲突,默认md5":"sha1|md5",
125+ "file_sum_arithmetic":"md5"
123126
124127
125128}
@@ -233,6 +236,7 @@ type GloablConfig struct {
233236 QueueSize int `json:"queue_size"`
234237 AutoRepair bool `json:"auto_repair"`
235238 Host string `json:"host"`
239+ FileSumArithmetic string `json:"file_sum_arithmetic"`
236240}
237241
238242func NewServer () * Server {
@@ -562,6 +566,14 @@ func (this *Common) GetFileMd5(file *os.File) string {
562566 return sum
563567}
564568
569+ func (this * Common ) GetFileSha1Sum (file * os.File ) string {
570+ file .Seek (0 , 0 )
571+ md5h := sha1 .New ()
572+ io .Copy (md5h , file )
573+ sum := fmt .Sprintf ("%x" , md5h .Sum (nil ))
574+ return sum
575+ }
576+
565577func (this * Common ) Contains (obj interface {}, arrayobj interface {}) bool {
566578 targetValue := reflect .ValueOf (arrayobj )
567579 switch reflect .TypeOf (arrayobj ).Kind () {
@@ -961,7 +973,11 @@ func (this *Server) Download(w http.ResponseWriter, r *http.Request) {
961973 if fp != nil {
962974 defer fp .Close ()
963975 }
964- md5sum = this .util .GetFileMd5 (fp )
976+ if strings .ToLower (Config ().FileSumArithmetic )== "sha1" {
977+ md5sum = this .util .GetFileSha1Sum (fp )
978+ } else {
979+ md5sum = this .util .GetFileMd5 (fp )
980+ }
965981 if ! CheckToken (token , md5sum , timestamp ) {
966982 w .Write ([]byte ("unvalid request,error token" ))
967983 return
@@ -1762,13 +1778,21 @@ func (this *Server) SyncFile(w http.ResponseWriter, r *http.Request) {
17621778
17631779 outPath = fileInfo .Path + "/" + fileInfo .Name
17641780
1781+ sum := ""
1782+
17651783 if this .util .FileExists (outPath ) {
17661784 if tmpFile , err = os .Open (outPath ); err != nil {
17671785 log .Error (err )
17681786 w .Write ([]byte (err .Error ()))
17691787 return
17701788 }
1771- if this .util .GetFileMd5 (tmpFile ) != fileInfo .Md5 {
1789+
1790+ if strings .ToLower (Config ().FileSumArithmetic )== "sha1" {
1791+ sum = this .util .GetFileSha1Sum (tmpFile )
1792+ } else {
1793+ sum = this .util .GetFileMd5 (tmpFile )
1794+ }
1795+ if sum != fileInfo .Md5 {
17721796 tmpFile .Close ()
17731797 log .Error ("md5 !=fileInfo.Md5 " )
17741798 w .Write ([]byte ("md5 !=fileInfo.Md5 " ))
@@ -1782,6 +1806,8 @@ func (this *Server) SyncFile(w http.ResponseWriter, r *http.Request) {
17821806 return
17831807 }
17841808
1809+
1810+
17851811 defer tmpFile .Close ()
17861812
17871813 if _ , err = io .Copy (tmpFile , uploadFile ); err != nil {
@@ -1790,7 +1816,12 @@ func (this *Server) SyncFile(w http.ResponseWriter, r *http.Request) {
17901816
17911817 return
17921818 }
1793- if this .util .GetFileMd5 (tmpFile ) != fileInfo .Md5 {
1819+ if strings .ToLower (Config ().FileSumArithmetic )== "sha1" {
1820+ sum = this .util .GetFileSha1Sum (tmpFile )
1821+ } else {
1822+ sum = this .util .GetFileMd5 (tmpFile )
1823+ }
1824+ if sum != fileInfo .Md5 {
17941825 log .Error ("md5 error" )
17951826 w .Write ([]byte ("md5 error" ))
17961827 tmpFile .Close ()
@@ -2026,8 +2057,14 @@ func (this *Server) Upload(w http.ResponseWriter, r *http.Request) {
20262057 } else {
20272058 fileInfo .Size = fi .Size ()
20282059 }
2060+ v := ""
2061+ if strings .ToLower (Config ().FileSumArithmetic )== "sha1" {
2062+ this .util .GetFileSha1Sum (outFile )
2063+ } else {
2064+ v = this .util .GetFileMd5 (outFile )
2065+ }
2066+
20292067
2030- v := this .util .GetFileMd5 (outFile )
20312068 fileInfo .Md5 = v
20322069 fileInfo .Path = folder
20332070
@@ -2924,6 +2961,5 @@ func (this *Server) Main() {
29242961
29252962func main () {
29262963
2927-
29282964 server .Main ()
29292965}
0 commit comments