🎇 use tempFile to cal md5

pull/548/head
微凉 2022-01-27 23:48:29 +08:00
parent bf9aa5c3d3
commit b797f4302c
5 changed files with 78 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io/ioutil" "io/ioutil"
"os"
) )
// InitConf init config // InitConf init config
@ -41,4 +42,8 @@ func InitConf() {
if err != nil { if err != nil {
log.Fatalf("update config struct error: %s", err.Error()) log.Fatalf("update config struct error: %s", err.Error())
} }
err = os.MkdirAll("data/temp", 0700)
if err != nil {
log.Fatalf("create temp dir error: %s", err.Error())
}
} }

View File

@ -1,6 +1,8 @@
package _23 package _23
import ( import (
"crypto/md5"
"encoding/hex"
"fmt" "fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/drivers/base"
@ -12,7 +14,10 @@ import (
"github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
) )
@ -294,10 +299,37 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
return base.ErrNotFolder return base.ErrNotFolder
} }
parentFileId, _ := strconv.Atoi(parentFile.Id) parentFileId, _ := strconv.Atoi(parentFile.Id)
tempFile, err := ioutil.TempFile("data/temp", "file-*")
if err != nil {
return err
}
defer func() {
_ = tempFile.Close()
_ = os.Remove(tempFile.Name())
}()
_, err = io.Copy(tempFile, file)
if err != nil {
return err
}
_, err = tempFile.Seek(0, io.SeekStart)
if err != nil {
return err
}
h := md5.New()
_, err = io.Copy(h, tempFile)
if err != nil {
return err
}
etag := hex.EncodeToString(h.Sum(nil))
log.Debugln("md5:", etag)
_, err = tempFile.Seek(0, io.SeekStart)
if err != nil {
return err
}
data := base.Json{ data := base.Json{
"driveId": 0, "driveId": 0,
"duplicate": true, "duplicate": true,
"etag": "836aae6cac845e17fce51919594737d0", //maybe file's md5 "etag": etag,
"fileName": file.GetFileName(), "fileName": file.GetFileName(),
"parentFileId": parentFileId, "parentFileId": parentFileId,
"size": file.GetSize(), "size": file.GetSize(),
@ -310,6 +342,9 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
if err != nil { if err != nil {
return err return err
} }
if resp.Data.Key == "" {
return nil
}
cfg := &aws.Config{ cfg := &aws.Config{
Credentials: credentials.NewStaticCredentials(resp.Data.AccessKeyId, resp.Data.SecretAccessKey, resp.Data.SessionToken), Credentials: credentials.NewStaticCredentials(resp.Data.AccessKeyId, resp.Data.SecretAccessKey, resp.Data.SessionToken),
Region: aws.String("123pan"), Region: aws.String("123pan"),
@ -324,7 +359,7 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
input := &s3manager.UploadInput{ input := &s3manager.UploadInput{
Bucket: &resp.Data.Bucket, Bucket: &resp.Data.Bucket,
Key: &resp.Data.Key, Key: &resp.Data.Key,
Body: file, Body: tempFile,
} }
_, err = uploader.Upload(input) _, err = uploader.Upload(input)
if err != nil { if err != nil {

View File

@ -1,7 +1,6 @@
package mediatrack package mediatrack
import ( import (
"bytes"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
@ -18,6 +17,8 @@ import (
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io" "io"
"io/ioutil"
"os"
"path/filepath" "path/filepath"
) )
@ -265,21 +266,39 @@ func (driver MediaTrack) Upload(file *model.FileStream, account *model.Account)
if err != nil { if err != nil {
return err return err
} }
var buf bytes.Buffer tempFile, err := ioutil.TempFile("data/temp", "file-*")
read := io.TeeReader(file, &buf) if err != nil {
return err
}
defer func() {
_ = tempFile.Close()
_ = os.Remove(tempFile.Name())
}()
_, err = io.Copy(tempFile, file)
if err != nil {
return err
}
_, err = tempFile.Seek(0, io.SeekStart)
if err != nil {
return err
}
uploader := s3manager.NewUploader(s) uploader := s3manager.NewUploader(s)
input := &s3manager.UploadInput{ input := &s3manager.UploadInput{
Bucket: &resp.Data.Bucket, Bucket: &resp.Data.Bucket,
Key: &resp.Data.Object, Key: &resp.Data.Object,
Body: read, Body: tempFile,
} }
_, err = uploader.Upload(input) _, err = uploader.Upload(input)
if err != nil { if err != nil {
return err return err
} }
url := fmt.Sprintf("https://jayce.api.mediatrack.cn/v3/assets/%s/children", parentFile.Id) url := fmt.Sprintf("https://jayce.api.mediatrack.cn/v3/assets/%s/children", parentFile.Id)
_, err = tempFile.Seek(0, io.SeekStart)
if err != nil {
return err
}
h := md5.New() h := md5.New()
_, err = io.Copy(h, &buf) _, err = io.Copy(h, tempFile)
if err != nil { if err != nil {
return err return err
} }

View File

@ -249,6 +249,16 @@ func (driver Native) Upload(file *model.FileStream, account *model.Account) erro
defer func() { defer func() {
_ = out.Close() _ = out.Close()
}() }()
//var buf bytes.Buffer
//reader := io.TeeReader(file, &buf)
//h := md5.New()
//_, err = io.Copy(h, reader)
//if err != nil {
// return err
//}
//hash := hex.EncodeToString(h.Sum(nil))
//log.Debugln("md5:", hash)
//_, err = io.Copy(out, &buf)
_, err = io.Copy(out, file) _, err = io.Copy(out, file)
return err return err
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"runtime/debug"
) )
func MakeDir(driver base.Driver, account *model.Account, path string, clearCache bool) error { func MakeDir(driver base.Driver, account *model.Account, path string, clearCache bool) error {
@ -78,5 +79,6 @@ func Upload(driver base.Driver, account *model.Account, file *model.FileStream,
if err != nil { if err != nil {
log.Errorf("upload error: %s", err.Error()) log.Errorf("upload error: %s", err.Error())
} }
debug.FreeOSMemory()
return err return err
} }