Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate file checksum when updating file using wopi or text/code ed… #1802

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
24 changes: 24 additions & 0 deletions pkg/filesystem/fsctx/stream.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package fsctx

import (
"encoding/hex"
"errors"
"github.com/HFO4/aliyun-oss-go-sdk/oss"
"hash"
"io"
"strings"
"time"
)

Expand Down Expand Up @@ -121,3 +124,24 @@ func (file *FileStream) SetSize(size uint64) {
func (file *FileStream) SetModel(fileModel interface{}) {
file.Model = fileModel
}

type ChecksumFileStream struct {
Md5 hash.Hash
Sha1 hash.Hash
io.Reader
io.Closer
}

func (file *ChecksumFileStream) Hash() string {
var sb strings.Builder

sb.WriteString("MD5:")
sb.WriteString(hex.EncodeToString(file.Md5.Sum(nil)))

sb.WriteByte(' ')

sb.WriteString("SHA1:")
sb.WriteString(hex.EncodeToString(file.Sha1.Sum(nil)))

return sb.String()
}
31 changes: 20 additions & 11 deletions pkg/filesystem/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package filesystem

import (
"context"
"crypto/md5"
"crypto/sha1"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/cluster"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"io"
"io/ioutil"
"net/http"
"strconv"
Expand Down Expand Up @@ -282,23 +285,29 @@ func NewWebdavAfterUploadHook(request *http.Request) func(ctx context.Context, f
modtime = time.Unix(timeUnix, 0)
}
}
checksum := request.Header.Get("OC-Checksum")

return func(ctx context.Context, fs *FileSystem, newFile fsctx.FileHeader) error {
file := newFile.Info().Model.(*model.File)
if !modtime.IsZero() {
err := model.DB.Model(file).UpdateColumn("updated_at", modtime).Error
if err != nil {
return err
}
}

if checksum != "" {
return file.UpdateMetadata(map[string]string{
model.ChecksumMetadataKey: checksum,
})
return model.DB.Model(file).UpdateColumn("updated_at", modtime).Error
}

return nil
}
}

// NewChecksumFileStreamAndAfterUploadHook 创建一个计算hash的数据流和相应的钩子函数
func NewChecksumFileStreamAndAfterUploadHook(rc io.ReadCloser) (io.ReadCloser, Hook) {
cfs := &fsctx.ChecksumFileStream{
Md5: md5.New(),
Sha1: sha1.New(),
Closer: rc,
}
cfs.Reader = io.TeeReader(rc, io.MultiWriter(cfs.Md5, cfs.Sha1))
return cfs, func(ctx context.Context, fs *FileSystem, newFile fsctx.FileHeader) error {
file := newFile.Info().Model.(*model.File)
return file.UpdateMetadata(map[string]string{
model.ChecksumMetadataKey: cfs.Hash(),
})
}
}
7 changes: 6 additions & 1 deletion pkg/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,13 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *filesyst
}
fileName := path.Base(reqPath)
filePath := path.Dir(reqPath)

// 计算hash
file, hook := filesystem.NewChecksumFileStreamAndAfterUploadHook(r.Body)

fileData := fsctx.FileStream{
MimeType: r.Header.Get("Content-Type"),
File: r.Body,
File: file,
Size: fileSize,
Name: fileName,
VirtualPath: filePath,
Expand Down Expand Up @@ -391,6 +395,7 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *filesyst

// rclone 请求
fs.Use("AfterUpload", filesystem.NewWebdavAfterUploadHook(r))
fs.Use("AfterUpload", hook)

// 执行上传
err = fs.Upload(ctx, &fileData)
Expand Down
6 changes: 5 additions & 1 deletion service/explorer/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,12 @@ func (service *FileIDService) PutContent(ctx context.Context, c *gin.Context) se
return serializer.ParamErr("Invalid content-length value", err)
}

// 计算hash
file, hook := filesystem.NewChecksumFileStreamAndAfterUploadHook(c.Request.Body)

fileData := fsctx.FileStream{
MimeType: c.Request.Header.Get("Content-Type"),
File: c.Request.Body,
File: file,
Size: fileSize,
Mode: fsctx.Overwrite,
}
Expand Down Expand Up @@ -453,6 +456,7 @@ func (service *FileIDService) PutContent(ctx context.Context, c *gin.Context) se
fs.Use("BeforeUpload", filesystem.HookValidateFile)
fs.Use("BeforeUpload", filesystem.HookValidateCapacityDiff)
fs.Use("AfterUpload", filesystem.GenericAfterUpdate)
fs.Use("AfterUpload", hook)

// 执行上传
uploadCtx = context.WithValue(uploadCtx, fsctx.FileModelCtx, originFile[0])
Expand Down
Loading