/
service.go
187 lines (157 loc) · 4.31 KB
/
service.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package api
import (
"context"
"fmt"
"os"
"time"
pb "github.com/codynhat/ipfs-ios-backup/api/pb"
"github.com/codynhat/ipfs-ios-backup/idevice"
"github.com/golang/protobuf/ptypes"
"github.com/ipfs/go-cid"
files "github.com/ipfs/go-ipfs-files"
icore "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/options"
core "github.com/textileio/go-threads/core/db"
"github.com/textileio/go-threads/db"
"github.com/textileio/go-threads/util"
)
type Backup struct {
ID core.InstanceID `json:"_id"` // DeviceID
LatestBackupCid string
UpdatedAt time.Time
}
// Service is a gRPC service
type Service struct {
ipfs icore.CoreAPI
d *db.DB
backupCollection *db.Collection
}
func NewService(ipfs icore.CoreAPI, d *db.DB) (*Service, error) {
collection := d.GetCollection("Backup")
return &Service{
ipfs: ipfs,
d: d,
backupCollection: collection,
}, nil
}
// AddBackup adds a new backup to IPFS
func (s *Service) AddBackup(ctx context.Context, req *pb.AddBackupRequest) (*pb.AddBackupReply, error) {
backupCid, err := s.addBackupToIpfs(ctx, req.BackupDir)
if err != nil {
return nil, err
}
return &pb.AddBackupReply{
BackupCid: backupCid.String(),
}, nil
}
// UpdateLatestBackup saves a reference to the latest backup
func (s *Service) UpdateLatestBackup(ctx context.Context, req *pb.UpdateLatestBackupRequest) (*pb.UpdateLatestBackupReply, error) {
deviceID := idevice.DeviceID(req.DeviceID)
backup := &Backup{
ID: core.InstanceID(deviceID),
LatestBackupCid: req.BackupCid,
UpdatedAt: time.Now(),
}
backupExists, err := s.backupExistsForDevice(deviceID)
if err != nil {
return nil, err
}
if backupExists {
err := s.backupCollection.Save(util.JSONFromInstance(backup))
if err != nil {
return nil, err
}
} else {
_, err := s.backupCollection.Create(util.JSONFromInstance(backup))
if err != nil {
return nil, err
}
}
t, err := ptypes.TimestampProto(backup.UpdatedAt)
if err != nil {
return nil, err
}
return &pb.UpdateLatestBackupReply{
Backup: &pb.Backup{
DeviceID: backup.ID.String(),
BackupCid: backup.LatestBackupCid,
UpdatedAt: t,
},
}, nil
}
// ListBackups lists all known backups
func (s *Service) ListBackups(ctx context.Context, req *pb.ListBackupsRequest) (*pb.ListBackupsReply, error) {
// Get all backups
backups, err := s.backupCollection.Find(&db.Query{})
if err != nil {
return nil, err
}
var results []*pb.Backup
for _, b := range backups {
backup := &Backup{}
util.InstanceFromJSON(b, backup)
t, err := ptypes.TimestampProto(backup.UpdatedAt)
if err != nil {
return nil, err
}
results = append(results, &pb.Backup{
DeviceID: backup.ID.String(),
BackupCid: backup.LatestBackupCid,
UpdatedAt: t,
})
}
return &pb.ListBackupsReply{
Backups: results,
}, nil
}
// Export returns the information needed to share backups with another device
func (s *Service) Export(ctx context.Context, req *pb.ExportRequest) (*pb.ExportReply, error) {
addrs, key, err := s.d.GetDBInfo()
if err != nil {
return nil, err
}
var rawAddrs []string
for _, addr := range addrs {
rawAddrs = append(rawAddrs, addr.String())
}
return &pb.ExportReply{
Addrs: rawAddrs,
ThreadKey: key.String(),
}, nil
}
func (s *Service) addBackupToIpfs(ctx context.Context, backupDir string) (cid.Cid, error) {
backupDirNode, err := getUnixfsNode(backupDir)
if err != nil {
return cid.Undef, fmt.Errorf("Failed to find backup: %s", err)
}
opts := []options.UnixfsAddOption{
options.Unixfs.Pin(true),
options.Unixfs.Nocopy(true),
}
cidDirectory, err := s.ipfs.Unixfs().Add(ctx, backupDirNode, opts...)
if err != nil {
return cid.Undef, fmt.Errorf("Failed to add backup to IPFS: %s", err)
}
return cidDirectory.Cid(), nil
}
func (s *Service) backupExistsForDevice(deviceID idevice.DeviceID) (bool, error) {
backups, err := s.backupCollection.FindByID(core.InstanceID(deviceID))
if err != nil && err != db.ErrNotFound {
return false, err
}
if err == db.ErrNotFound {
return false, nil
}
return len(backups) > 0, nil
}
func getUnixfsNode(path string) (files.Node, error) {
st, err := os.Stat(path)
if err != nil {
return nil, err
}
f, err := files.NewSerialFile(path, false, st)
if err != nil {
return nil, err
}
return f, nil
}