Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
Fix storage of attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Jun 21, 2019
1 parent fd6d3b2 commit d2393cc
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions normalize.go
Expand Up @@ -21,7 +21,7 @@ type attachment struct {
ContentType string `json:"content_type"`
Stub bool `json:"stub,omitempty"`
Content *os.File `json:"data,omitempty"`
Size int64 `json:"size"`
Size int64 `json:"length"`
Digest string `json:"digest"`
}

Expand Down Expand Up @@ -69,7 +69,7 @@ func (a *attachment) UnmarshalJSON(p []byte) error {
ContentType string `json:"content_type"`
Stub bool `json:"stub"`
Content []byte `json:"data"`
Size int64 `json:"size"`
Size int64 `json:"length"`
Digest string `json:"digest"`
}
if err := json.Unmarshal(p, &att); err != nil {
Expand Down Expand Up @@ -119,7 +119,7 @@ func (a *attachment) MarshalJSON() ([]byte, error) {
type att struct {
ContentType string `json:"content_type"`
Data []byte `json:"data"`
Size int64 `json:"size"`
Size int64 `json:"length"`
Digest string `json:"digest"`
}
content, err := ioutil.ReadAll(a.Content)
Expand All @@ -138,7 +138,7 @@ func (a *attachment) stubMarshalJSON() ([]byte, error) {
type stub struct {
ContentType string `json:"content_type"`
Stub bool `json:"stub"`
Size int64 `json:"size,omitempty"`
Size int64 `json:"length,omitempty"`
Digest string `json:"digest,omitempty"`
}
return json.Marshal(stub{
Expand Down
4 changes: 2 additions & 2 deletions normalize_test.go
Expand Up @@ -66,7 +66,7 @@ func TestAttachmentUnmarshalJSON_stub(t *testing.T) {
"data": "VGVzdGluZwo=",
"stub": true,
"digest": "md5-ec4d59b2732f2f153240a8ff746282a6",
"size": 8
"length": 8
}`
result := attachment{}
if err := json.Unmarshal([]byte(in), &result); err != nil {
Expand All @@ -89,7 +89,7 @@ func TestAttachmentUnmarshalJSON_file(t *testing.T) {
"content_type": "text/plain",
"data": "VGVzdGluZwo=",
"digest": "md5-ec4d59b2732f2f153240a8ff746282a6",
"size": 8
"length": 8
}`
result := attachment{}
if err := json.Unmarshal([]byte(in), &result); err != nil {
Expand Down
22 changes: 12 additions & 10 deletions put.go
Expand Up @@ -131,6 +131,7 @@ func (d *db) Put(_ context.Context, docID string, doc interface{}, opts map[stri
if err != nil {
return "", err
}
defer ndoc.cleanup() // nolint: errcheck
currev, err := d.currentRev(filename)
if err != nil {
return "", err
Expand All @@ -151,16 +152,6 @@ func (d *db) Put(_ context.Context, docID string, doc interface{}, opts map[stri
}
}()

tmp, err := ioutil.TempFile(d.path(), ".")
if err != nil {
return "", err
}
defer tmp.Close() // nolint: errcheck
toRename[tmp.Name()] = d.path(filename)
if err := json.NewEncoder(tmp).Encode(ndoc); err != nil {
return "", err
}

if atts != nil {
base := base(filename)
if err := os.Mkdir(d.path(base), 0777); err != nil {
Expand All @@ -175,9 +166,20 @@ func (d *db) Put(_ context.Context, docID string, doc interface{}, opts map[stri
if _, err := io.Copy(tmp, att.Content); err != nil {
return "", err
}
att.Stub = true
}
}

tmp, err := ioutil.TempFile(d.path(), ".")
if err != nil {
return "", err
}
defer tmp.Close() // nolint: errcheck
toRename[tmp.Name()] = d.path(filename)
if err := json.NewEncoder(tmp).Encode(ndoc); err != nil {
return "", err
}

if err := tmp.Close(); err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions put_test.go
Expand Up @@ -149,9 +149,8 @@ func TestPut(t *testing.T) {
"_attachments": map[string]interface{}{
"foo.txt": map[string]interface{}{
"content_type": "text/plain",
"digest": "md5-xxx",
"length": 123,
"revpos": 1,
"digest": "md5-fa6a5a3224d7da66d9e0bdec25f62cf0",
"length": 7,
"stub": true,
},
},
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestAttachmentMarshalJSON_file
Expand Up @@ -2,5 +2,5 @@
"content_type": "text/plain",
"data": "VGVzdGluZwo=",
"digest": "md5-ec4d59b2732f2f153240a8ff746282a6",
"size": 8
"length": 8
}
2 changes: 1 addition & 1 deletion testdata/TestAttachmentMarshalJSON_stub
@@ -1,6 +1,6 @@
{
"content_type": "text/plain",
"digest": "md5-ec4d59b2732f2f153240a8ff746282a6",
"size": 8,
"length": 8,
"stub": true
}
2 changes: 1 addition & 1 deletion testdata/TestNormalDocMarshalJSON_attachment
Expand Up @@ -4,7 +4,7 @@
"content_type": "text/plain",
"data": "VGVzdGluZwo=",
"digest": "md5-ec4d59b2732f2f153240a8ff746282a6",
"size": 8
"length": 8
}
},
"_id": "foo",
Expand Down
2 changes: 1 addition & 1 deletion testdata/TestNormalDocUnmarshalJSON_attachment
Expand Up @@ -4,7 +4,7 @@
"foo.txt": {
"content_type": "text/plain",
"data": "VGVzdGluZwo=",
"size": 8,
"length": 8,
"digest": "md5-ec4d59b2732f2f153240a8ff746282a6"
}
},
Expand Down

0 comments on commit d2393cc

Please sign in to comment.