/
backupFile.go
53 lines (42 loc) · 971 Bytes
/
backupFile.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
package archiving
import (
"os"
"github.com/golang/glog"
"github.com/rokeller/bart/domain"
)
type backupFile struct {
a Archive
e domain.Entry
f *os.File
}
func (a Archive) newBackupFile(entry domain.Entry) (*backupFile, error) {
tempFile, err := os.CreateTemp(os.TempDir(), "bart-*")
if nil != err {
glog.Errorf("Failed to create temp file: %v", err)
return nil, err
}
f := backupFile{
a: a,
e: entry,
f: tempFile,
}
return &f, nil
}
// Write implements io.WriteCloser.
func (f backupFile) Write(p []byte) (n int, err error) {
return f.f.Write(p)
}
// Close implements io.WriteCloser.
func (f backupFile) Close() error {
defer os.Remove(f.f.Name())
return f.f.Close()
}
func (f backupFile) Upload() error {
// The file has just been written to. We need to seek to the beginning
// before we can upload bytes from it.
_, err := f.f.Seek(0, 0)
if nil != err {
return err
}
return f.a.storageProvider.WriteBackupFile(f.e, f.f)
}