forked from nonomal/AcDrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.go
47 lines (43 loc) · 974 Bytes
/
history.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
)
//History 本地下载历史 用于续传
type History struct {
FileSha1 string `json:"sha1"`
BlockIndex []int `json:"block"`
}
func wHistory(history History) {
historyJSON, err := json.Marshal(history)
if err != nil {
fmt.Println(err)
return
}
f, err := os.Create(history.FileSha1 + ".json")
if err != nil {
fmt.Println("创建记录下载历史文件时出错。", err)
return
}
_, err = f.Write(historyJSON)
if err != nil {
fmt.Println("写入下载历史出错。", err)
return
}
}
func rHistory(filesha1 string) (History, error) {
var history History
f, err := os.Open(filesha1 + ".json")
if err != nil {
return history, errors.New("打开历史出错。没有历史文件。")
}
bhistory, err := ioutil.ReadAll(f)
if err != nil {
return history, errors.New("读取history出错。")
}
err = json.Unmarshal(bhistory, &history)
return history, err
}