-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_rw.go
269 lines (225 loc) · 6.26 KB
/
file_rw.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package rw
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/google/uuid"
"github.com/jomei/notionapi"
"github.com/rs/zerolog"
"github.com/shivaji17/notionbackup/src/logging"
"github.com/shivaji17/notionbackup/src/metadata"
"github.com/shivaji17/notionbackup/src/utils"
"google.golang.org/protobuf/proto"
)
const (
DATABASE_DIR_NAME = "databases"
PAGE_DIR_NAME = "pages"
BLOCK_DIR_NAME = "blocks"
OBJECT_FILE_PERM = 0400
METADATA_FILE_PERM = 0644
METADATA_FILE_NAME = "metadata.pb"
)
type FileReaderWriter struct {
baseDirPath string
databaseDirPath string
pageDirPath string
blockDirPath string
filePathList []string
}
func GetFileReaderWriter(ctx context.Context, basePath string,
createDirIfNotExist bool) (ReaderWriter, error) {
log := zerolog.Ctx(ctx)
err := utils.CheckIfDirExists(basePath)
if err != nil {
if !createDirIfNotExist {
return nil, err
}
err = utils.CreateDirectory(basePath)
if err != nil {
return nil, err
}
}
databaseDirPath := filepath.Join(basePath, DATABASE_DIR_NAME)
log.Info().Str(logging.ExportPath, databaseDirPath).Msg(
"Database objects backup path")
pageDirPath := filepath.Join(basePath, PAGE_DIR_NAME)
log.Info().Str(logging.ExportPath, pageDirPath).Msg(
"Page objects backup path")
blockDirPath := filepath.Join(basePath, BLOCK_DIR_NAME)
log.Info().Str(logging.ExportPath, blockDirPath).Msg(
"Block objects backup path")
err = utils.CreateDirectory(databaseDirPath)
if err != nil {
return nil, err
}
err = utils.CreateDirectory(pageDirPath)
if err != nil {
return nil, err
}
err = utils.CreateDirectory(blockDirPath)
if err != nil {
return nil, err
}
return &FileReaderWriter{
baseDirPath: basePath,
databaseDirPath: databaseDirPath,
pageDirPath: pageDirPath,
blockDirPath: blockDirPath,
filePathList: make([]string, 0),
}, nil
}
func GetFileReaderWriterForMetadata(ctx context.Context,
metadataFilePath string, data *metadata.MetaData) (ReaderWriter, error) {
baseDir := filepath.Dir(metadataFilePath)
absBasePath, err := filepath.Abs(baseDir)
if err != nil {
return nil, err
}
pageDir := filepath.Join(absBasePath, data.StorageConfig.GetLocal().PageDir)
databaseDir := filepath.Join(absBasePath,
data.StorageConfig.GetLocal().DatabaseDir)
blockDir := filepath.Join(absBasePath,
data.StorageConfig.GetLocal().BlocksDir)
err = utils.CheckIfDirExists(pageDir)
if err != nil {
return nil, err
}
err = utils.CheckIfDirExists(databaseDir)
if err != nil {
return nil, err
}
err = utils.CheckIfDirExists(blockDir)
if err != nil {
return nil, err
}
return &FileReaderWriter{
baseDirPath: absBasePath,
databaseDirPath: databaseDir,
pageDirPath: pageDir,
blockDirPath: blockDir,
filePathList: make([]string, 0),
}, nil
}
func (rw *FileReaderWriter) writeData(ctx context.Context, v interface{},
dirPath string) (DataIdentifier, error) {
dataIdentifier := uuid.NewString()
filePath := filepath.Join(dirPath, dataIdentifier)
dataBytes, err := json.Marshal(&v)
if err != nil {
return "", err
}
err = os.WriteFile(filePath, dataBytes, OBJECT_FILE_PERM)
if err != nil {
return "", err
}
rw.filePathList = append(rw.filePathList, filePath)
return DataIdentifier(dataIdentifier), nil
}
func (rw *FileReaderWriter) readData(ctx context.Context, filePath string,
v interface{}) error {
databytes, err := os.ReadFile(filePath)
if err != nil {
return err
}
err = json.Unmarshal(databytes, v)
if err != nil {
return err
}
return nil
}
func (rw *FileReaderWriter) WriteDatabase(ctx context.Context,
database *notionapi.Database) (DataIdentifier, error) {
if database == nil {
return "", fmt.Errorf("nullptr received for database object")
}
return rw.writeData(ctx, database, rw.databaseDirPath)
}
func (rw *FileReaderWriter) ReadDatabase(ctx context.Context,
identifier DataIdentifier) (*notionapi.Database, error) {
database := ¬ionapi.Database{}
err := rw.readData(ctx, filepath.Join(rw.databaseDirPath,
identifier.String()), &database)
if err != nil {
return nil, err
}
return database, nil
}
func (rw *FileReaderWriter) WritePage(ctx context.Context,
page *notionapi.Page) (DataIdentifier, error) {
if page == nil {
return "", fmt.Errorf("nullptr received for page object")
}
return rw.writeData(ctx, page, rw.pageDirPath)
}
func (rw *FileReaderWriter) ReadPage(ctx context.Context,
identifier DataIdentifier) (*notionapi.Page, error) {
page := ¬ionapi.Page{}
err := rw.readData(ctx, filepath.Join(rw.pageDirPath,
identifier.String()), &page)
if err != nil {
return nil, err
}
return page, nil
}
func (rw *FileReaderWriter) WriteBlock(ctx context.Context,
block notionapi.Block) (DataIdentifier, error) {
if block == nil {
return "", fmt.Errorf("nullptr received for block object")
}
return rw.writeData(ctx, block, rw.blockDirPath)
}
func (rw *FileReaderWriter) ReadBlock(ctx context.Context,
identifier DataIdentifier) (notionapi.Block, error) {
databytes, err := os.ReadFile(filepath.Join(rw.blockDirPath,
identifier.String()))
if err != nil {
return nil, err
}
var response map[string]interface{}
err = json.Unmarshal(databytes, &response)
if err != nil {
return nil, err
}
return utils.DecodeBlockObject(response)
}
func (rw *FileReaderWriter) CleanUp(ctx context.Context) error {
var externalErr error
externalErr = nil
for _, filePath := range rw.filePathList {
err := os.Remove(filePath)
if err != nil {
externalErr = err
}
}
return externalErr
}
func (rw *FileReaderWriter) WriteMetaData(ctx context.Context,
metadata *metadata.MetaData) error {
dataBytes, err := proto.Marshal(metadata)
if err != nil {
return err
}
path := filepath.Join(rw.baseDirPath, METADATA_FILE_NAME)
zerolog.Ctx(ctx).Info().Str(logging.MetaDataFilePath, path).Msg(
"Writing Metadata file")
err = os.WriteFile(path, dataBytes, METADATA_FILE_PERM)
if err != nil {
return err
}
return nil
}
func (rw *FileReaderWriter) GetStorageConfig(ctx context.Context) (
*metadata.StorageConfig, error) {
localConfig := &metadata.StorageConfig_Local{
DatabaseDir: DATABASE_DIR_NAME,
PageDir: PAGE_DIR_NAME,
BlocksDir: BLOCK_DIR_NAME,
}
return &metadata.StorageConfig{
Config: &metadata.StorageConfig_Local_{
Local: localConfig,
},
}, nil
}