-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.go
86 lines (68 loc) · 1.37 KB
/
filesystem.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
sf "github.com/VictorNine/sfgo"
)
func createFile(item *sf.Item, sess *sf.Session) (string, error) {
// TODO: Check if the file exists in map. rename to Title+"1"
note, err := itemToNote(sess, item)
if err != nil {
return "", err
}
filename := "./notes/" + note.Title + ".txt"
f, err := os.Create(filename)
if err != nil {
return "", err
}
defer f.Close()
_, err = f.WriteString(note.Text)
if err != nil {
return "", err
}
f.Sync()
files[filename] = item.UUID
return filename, nil
}
// Create dir if it doesent exist
func createDir(name string) error {
_, err := os.Stat(name)
if os.IsNotExist(err) {
err = os.MkdirAll(name, 0755)
if err != nil {
return err
}
}
return nil
}
// Encrypt file content and put it in DB as unsynced
func fileToNote(uuid, filename string, sess *sf.Session, db *database) {
text, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
item, err := db.getItem(uuid)
if err != nil {
log.Fatal(err)
}
note, err := itemToNote(sess, &item)
if err != nil {
log.Fatal(err)
}
note.Text = string(text)
bNote, err := json.Marshal(¬e)
if err != nil {
log.Fatal(err)
}
item.PlanText = bNote
err = sess.EncryptItem(&item)
if err != nil {
log.Fatal(err)
}
err = db.newUnsyncedItem(&item)
if err != nil {
log.Fatal(err)
}
}