-
Notifications
You must be signed in to change notification settings - Fork 11
/
zip.go
178 lines (165 loc) · 3.27 KB
/
zip.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
package main
import (
"archive/zip"
"crypto/aes"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
func zipData(fromDir, toFile string) error {
dir, err := os.Open(fromDir)
if err != nil {
return err
}
defer dir.Close()
//open zip
fileToZip, err := os.Create(toFile)
if err != nil {
return err
}
defer fileToZip.Close()
zipFile := zip.NewWriter(fileToZip)
defer zipFile.Close()
files, err := dir.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range files {
filename := filepath.Join(fromDir, name)
fp, err := os.Open(filename)
if err != nil {
return err
}
//add to zip
writer, err := zipFile.Create(name)
if err != nil {
return err
}
_, err = io.Copy(writer, fp)
if err != nil {
return err
}
fp.Close()
}
return nil
}
func (s *myWindow) importFromZip(filename string, pwd string) error {
zipFile, err := zip.OpenReader(filename)
if err != nil {
return err
}
defer zipFile.Close()
//unzip diary.db
var diaryDbName string
for _, zf := range zipFile.File {
if zf.Name == "diary.db" {
diaryDbName = filepath.Join(os.TempDir(), "diary.db")
fp, err := os.Create(diaryDbName)
if err != nil {
return err
}
reader, err := zf.Open()
if err != nil {
fp.Close()
return err
}
_, err = io.Copy(fp, reader)
reader.Close()
if err != nil {
fp.Close()
reader.Close()
return err
}
fp.Close()
break
}
}
db, err := sql.Open("sqlite3", diaryDbName)
if err != nil {
return err
}
defer os.Remove(diaryDbName)
defer db.Close()
res, err := db.Query("select cdate,title,filename,category from diaries")
if err != nil {
return err
}
//get filename title dict
type diaryRecod struct {
Cdate string
Title string
Category int
}
var titleDict = make(map[string]diaryRecod)
var cdate, title, fname string
var category int
for res.Next() {
err := res.Scan(&cdate, &title, &fname, &category)
if err != nil {
res.Close()
return err
}
titleDict[fname] = diaryRecod{cdate, title, category}
}
res.Close()
//get real key
key, err := dbGetRealKey(db, pwd)
if err != nil {
return err
}
//import every file from zip
id := s.db.NextId()
tx, err := s.db.BeginTx()
if err != nil {
return err
}
for _, zf := range zipFile.File {
if zf.Name == "diary.db" {
break
}
record, ok := titleDict[zf.Name]
if !ok {
break
}
reader, err := zf.Open()
if err != nil {
return err
}
data, err := decodeFromReader(reader, key)
reader.Close()
if err != nil {
return err
}
//save diary
datName := fmt.Sprintf("%d.dat", id)
encodeToFile(data, datName, s.key)
s.db.AddDiaryTx(tx, id, record.Cdate, record.Title, datName, record.Category)
s.setStatusBar(datName)
id++
}
return tx.Commit()
}
func dbGetRealKey(db *sql.DB, pwd string) ([]byte, error) {
sha40 := getSha40String(pwd)
row := db.QueryRow("select realkey from user where sha40=?;", sha40)
var realKey string
err := row.Scan(&realKey)
if err != nil {
return nil, err
}
key, err := hex.DecodeString(realKey)
if err != nil {
return nil, err
}
if len(key) != 32+aes.BlockSize {
return nil, errors.New("Data invalid.")
}
iv := key[:aes.BlockSize]
data := key[aes.BlockSize:]
ukey := getSha4(pwd)
return decodeRealKey(ukey, data, iv)
}