@@ -2,13 +2,16 @@ package filesystem
2
2
3
3
import (
4
4
"context"
5
+ "crypto/md5"
6
+ "crypto/sha1"
5
7
model "github.com/cloudreve/Cloudreve/v3/models"
6
8
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
7
9
"github.com/cloudreve/Cloudreve/v3/pkg/cluster"
8
10
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local"
9
11
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
10
12
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
11
13
"github.com/cloudreve/Cloudreve/v3/pkg/util"
14
+ "io"
12
15
"io/ioutil"
13
16
"net/http"
14
17
"strconv"
@@ -282,23 +285,29 @@ func NewWebdavAfterUploadHook(request *http.Request) func(ctx context.Context, f
282
285
modtime = time .Unix (timeUnix , 0 )
283
286
}
284
287
}
285
- checksum := request .Header .Get ("OC-Checksum" )
286
288
287
289
return func (ctx context.Context , fs * FileSystem , newFile fsctx.FileHeader ) error {
288
290
file := newFile .Info ().Model .(* model.File )
289
291
if ! modtime .IsZero () {
290
- err := model .DB .Model (file ).UpdateColumn ("updated_at" , modtime ).Error
291
- if err != nil {
292
- return err
293
- }
294
- }
295
-
296
- if checksum != "" {
297
- return file .UpdateMetadata (map [string ]string {
298
- model .ChecksumMetadataKey : checksum ,
299
- })
292
+ return model .DB .Model (file ).UpdateColumn ("updated_at" , modtime ).Error
300
293
}
301
294
302
295
return nil
303
296
}
304
297
}
298
+
299
+ // NewChecksumFileStreamAndAfterUploadHook 创建一个计算hash的数据流和相应的钩子函数
300
+ func NewChecksumFileStreamAndAfterUploadHook (rc io.ReadCloser ) (io.ReadCloser , Hook ) {
301
+ cfs := & fsctx.ChecksumFileStream {
302
+ Md5 : md5 .New (),
303
+ Sha1 : sha1 .New (),
304
+ Closer : rc ,
305
+ }
306
+ cfs .Reader = io .TeeReader (rc , io .MultiWriter (cfs .Md5 , cfs .Sha1 ))
307
+ return cfs , func (ctx context.Context , fs * FileSystem , newFile fsctx.FileHeader ) error {
308
+ file := newFile .Info ().Model .(* model.File )
309
+ return file .UpdateMetadata (map [string ]string {
310
+ model .ChecksumMetadataKey : cfs .Hash (),
311
+ })
312
+ }
313
+ }
0 commit comments