forked from Quanwei1992/ebookdownloader
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmetaFile.go
67 lines (57 loc) · 1.64 KB
/
metaFile.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
package ebookdownloader
import (
"encoding/json"
"os"
"path/filepath"
)
// Meta 小说的metainfo
type Meta struct {
Ebhost string `json:"ebhost" storm:"index"`
Bookid string `json:"bookid"`
BookName string `json:"bookname" storm:"index"`
BookISBN string `json:"isbn" storm:"index"`
BookUUID string `json:"uuid" storm:"id,index,unique"`
Author string `json:"author" storm:"index"`
CoverURL string `json:"cover_url"`
Description string `json:"description"`
TxtURLPath string `json:"txt_url_path"`
MobiURLPath string `json:"mobi_url_path"`
EPUBURLPath string `json:"epub_url_path"`
AZW3URLPath string `json:"azw3_url_path"`
TxtMD5 string `json:"txt_md5_info"`
MobiMD5 string `json:"mobi_md5_info"`
EPUBMD5 string `json:"epub_md5_info"`
AZW3MD5 string `json:"azw3_md5_info"`
}
// WriteFile 把json数据写入 filename定义的文件中
func (this Meta) WriteFile(filename string) error {
// 创建文件
fileAbs, _ := filepath.Abs(filename) //使用相对路径
filePtr, err := os.Create(fileAbs)
if err != nil {
return err
}
defer filePtr.Close()
// 带JSON缩进格式写文件
data, err := json.MarshalIndent(this, "", " ")
if err != nil {
return err
}
//写入文件中
filePtr.Write(data)
return nil
}
// GetMetaData 从文件中读取meta信息,并返回
func GetMetaData(filename string) (Meta, error) {
fileAbs, _ := filepath.Abs(filename)
filePtr, _ := os.Open(fileAbs)
defer filePtr.Close()
var metainfo Meta
// 创建json解码器
decoder := json.NewDecoder(filePtr)
err := decoder.Decode(&metainfo)
if err != nil {
return Meta{}, err
}
return metainfo, nil
}