-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileinfo.go
44 lines (38 loc) · 1.1 KB
/
fileinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package docdb
import (
"bytes"
"context"
"github.com/domonda/go-errs"
"github.com/ungerik/go-fs"
"github.com/ungerik/go-fs/fsimpl"
)
type FileInfo struct {
Name string
Size int64
Hash string
}
// ContentHash returns a Dropbox compatible 64 hex character content hash
// by reading from an io.Reader until io.EOF.
// See https://www.dropbox.com/developers/reference/content-hash
func ContentHash(data []byte) string {
hash, err := fsimpl.DropboxContentHash(context.Background(), bytes.NewReader(data))
if err != nil {
panic(errs.Errorf("should never happen: %w", err))
}
return hash
}
// ReadFileInfo reads the file content from file and returns a FileInfo with the file name, size and hash.
func ReadFileInfo(ctx context.Context, file fs.FileReader) (info FileInfo, err error) {
defer errs.WrapWithFuncParams(&err, ctx, file)
data, err := file.ReadAllContext(ctx)
if err != nil {
return FileInfo{}, err
}
info.Name = file.Name()
info.Size = int64(len(data))
info.Hash, err = fsimpl.DropboxContentHash(ctx, bytes.NewReader(data))
if err != nil {
return FileInfo{}, err
}
return info, nil
}