-
Notifications
You must be signed in to change notification settings - Fork 342
/
operation_utils.go
59 lines (52 loc) · 1.53 KB
/
operation_utils.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package operations
import (
"net/http"
"strings"
"time"
"github.com/treeverse/lakefs/pkg/catalog"
"github.com/treeverse/lakefs/pkg/logging"
)
const amzMetaHeaderPrefix = "X-Amz-Meta-"
// amzMetaAsMetadata prepare metadata based on amazon user metadata request headers
func amzMetaAsMetadata(req *http.Request) catalog.Metadata {
metadata := make(catalog.Metadata)
for k := range req.Header {
if strings.HasPrefix(k, amzMetaHeaderPrefix) {
metadata[k] = req.Header.Get(k)
}
}
return metadata
}
// amzMetaWriteHeaders set amazon user metadata on http response
func amzMetaWriteHeaders(w http.ResponseWriter, metadata catalog.Metadata) {
h := w.Header()
for k, v := range metadata {
if strings.HasPrefix(k, amzMetaHeaderPrefix) {
h.Set(k, v)
}
}
}
func (o *PathOperation) finishUpload(req *http.Request, checksum, physicalAddress string, size int64, relative bool, metadata map[string]string, contentType string) error {
// write metadata
writeTime := time.Now()
entry := catalog.NewDBEntryBuilder().
Path(o.Path).
RelativeAddress(relative).
PhysicalAddress(physicalAddress).
Checksum(checksum).
Metadata(metadata).
Size(size).
CreationDate(writeTime).
ContentType(contentType).
Build()
err := o.Catalog.CreateEntry(req.Context(), o.Repository.Name, o.Reference, entry)
if err != nil {
o.Log(req).WithError(err).Error("could not update metadata")
return err
}
tookMeta := time.Since(writeTime)
o.Log(req).WithFields(logging.Fields{
"took": tookMeta,
}).Debug("metadata update complete")
return nil
}