forked from cloudfoundry/bosh-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob_manager.go
113 lines (89 loc) · 2.69 KB
/
blob_manager.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package blobstore
import (
"io"
"os"
"path"
"strings"
"fmt"
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type BlobManager struct {
fs boshsys.FileSystem
blobstorePath string
}
func NewBlobManager(fs boshsys.FileSystem, blobstorePath string) (manager BlobManager) {
manager.fs = fs
manager.blobstorePath = blobstorePath
return
}
func (manager BlobManager) Fetch(blobID string) (boshsys.File, error, int) {
blobPath := path.Join(manager.blobstorePath, blobID)
readOnlyFile, err := manager.fs.OpenFile(blobPath, os.O_RDONLY, os.ModeDir)
if err != nil {
statusCode := 500
if strings.Contains(err.Error(), "no such file") {
statusCode = 404
}
return nil, bosherr.WrapError(err, "Reading blob"), statusCode
}
return readOnlyFile, nil, 200
}
func (manager BlobManager) Write(blobID string, reader io.Reader) error {
blobPath := path.Join(manager.blobstorePath, blobID)
writeOnlyFile, err := manager.fs.OpenFile(blobPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
err = bosherr.WrapError(err, "Opening blob store file")
return err
}
defer func() {
_ = writeOnlyFile.Close()
}()
_, err = io.Copy(writeOnlyFile, reader)
if err != nil {
err = bosherr.WrapError(err, "Updating blob")
}
return err
}
func (manager BlobManager) GetPath(blobID string, digest boshcrypto.Digest) (string, error) {
if !manager.BlobExists(blobID) {
return "", bosherr.Errorf("Blob '%s' not found", blobID)
}
tempFilePath, err := manager.copyToTmpFile(path.Join(manager.blobstorePath, blobID))
if err != nil {
return "", err
}
file, err := os.Open(tempFilePath)
if err != nil {
return "", err
}
defer file.Close()
err = digest.Verify(file)
if err != nil {
return "", bosherr.WrapError(err, fmt.Sprintf("Checking blob '%s'", blobID))
}
return tempFilePath, nil
}
func (manager BlobManager) Delete(blobID string) error {
localBlobPath := path.Join(manager.blobstorePath, blobID)
return manager.fs.RemoveAll(localBlobPath)
}
func (manager BlobManager) BlobExists(blobID string) bool {
blobPath := path.Join(manager.blobstorePath, blobID)
return manager.fs.FileExists(blobPath)
}
func (manager BlobManager) copyToTmpFile(srcFileName string) (string, error) {
file, err := manager.fs.TempFile("blob-manager-copyToTmpFile")
if err != nil {
return "", bosherr.WrapError(err, "Creating temporary file")
}
defer file.Close()
destTmpFileName := file.Name()
err = manager.fs.CopyFile(srcFileName, destTmpFileName)
if err != nil {
manager.fs.RemoveAll(destTmpFileName)
return "", bosherr.WrapError(err, "Copying file")
}
return destTmpFileName, nil
}