This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
forked from jpillora/cloud-torrent
/
engineCache.go
192 lines (171 loc) · 5.17 KB
/
engineCache.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
package engine
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/boypt/simple-torrent/common"
)
const (
cacheSavedPrefix = "_CLDAUTOSAVED_"
)
func (e *Engine) newMagnetCacheFile(magnetURI, infohash string) {
// create .info file with hash as filename
if w, err := os.Stat(e.cacheDir); err == nil && w.IsDir() {
cacheInfoPath := filepath.Join(e.cacheDir,
fmt.Sprintf("%s%s.info", cacheSavedPrefix, infohash))
if _, err := os.Stat(cacheInfoPath); os.IsNotExist(err) {
cf, err := os.Create(cacheInfoPath)
if err == nil {
defer cf.Close()
_, err := cf.WriteString(magnetURI)
common.HandleError(err)
log.Println("created magnet cache info file", infohash)
}
}
}
}
func (e *Engine) newTorrentCacheFile(meta *metainfo.MetaInfo) {
// create .torrent file
infohash := meta.HashInfoBytes().HexString()
if w, err := os.Stat(e.cacheDir); err == nil && w.IsDir() {
cacheFilePath := filepath.Join(e.cacheDir,
fmt.Sprintf("%s%s.torrent", cacheSavedPrefix, infohash))
// only create the cache file if not exists
// avoid recreating cache files during boot import
if _, err := os.Stat(cacheFilePath); os.IsNotExist(err) {
cf, err := os.Create(cacheFilePath)
if err == nil {
defer cf.Close()
common.FancyHandleError(meta.Write(cf))
log.Println("created torrent cache file", infohash)
} else {
log.Println("failed to create torrent file", err)
}
}
}
}
func (e *Engine) removeMagnetCache(infohash string) {
// remove both magnet and torrent cache if exists.
cacheInfoPath := filepath.Join(e.cacheDir,
fmt.Sprintf("%s%s.info", cacheSavedPrefix, infohash))
if err := os.Remove(cacheInfoPath); err == nil {
log.Printf("removed magnet info file %s", cacheInfoPath)
} else if !os.IsNotExist(err) { // it's fine if the cache is not exists
log.Printf("fail to removed cache file [%s], %s", infohash, err)
}
}
func (e *Engine) removeTorrentCache(infohash string, toTrash bool) {
fileName := fmt.Sprintf("%s%s.torrent", cacheSavedPrefix, infohash)
cacheFilePath := filepath.Join(e.cacheDir, fileName)
if toTrash {
trashFilePath := filepath.Join(e.trashDir, fileName)
if err := os.Rename(cacheFilePath, trashFilePath); err == nil {
log.Printf("move torrent file to trash [%s]", trashFilePath)
} else {
log.Println("fail to move to trash", err)
}
} else {
if err := os.Remove(cacheFilePath); err == nil {
log.Printf("removed torrent file [%s]", cacheFilePath)
} else if !os.IsNotExist(err) { // it's fine if the cache is not exists
log.Printf("fail to removed cache file [%s] %s", infohash, err)
}
}
}
func (e *Engine) TorrentCacheFileName(infohash string) string {
cacheFilePath := filepath.Join(e.cacheDir,
fmt.Sprintf("%s%s.torrent", cacheSavedPrefix, infohash))
return cacheFilePath
}
func (e *Engine) PushWaitTask(ih string) error {
log.Println("Pushed task to wait", ih)
e.pushWaitTask(ih, taskTorrent)
info, err := metainfo.LoadFromFile(e.TorrentCacheFileName(ih))
if err != nil {
return err
}
spec := torrent.TorrentSpecFromMetaInfo(info)
_, err = e.upsertTorrent(ih, spec.DisplayName, true)
return err
}
func (e *Engine) RestoreTask(fn string) error {
isCachedFile := strings.HasPrefix(filepath.Base(fn), cacheSavedPrefix)
if strings.HasSuffix(fn, ".torrent") {
if err := e.NewTorrentByFilePath(fn); err != nil {
return err
}
if isCachedFile {
log.Printf("[RestoreTask] Restored Torrent: %s \n", fn)
} else {
log.Printf("Task: added %s, file removed\n", fn)
os.Remove(fn)
}
} else if strings.HasSuffix(fn, ".info") && isCachedFile {
mag, err := ioutil.ReadFile(fn)
if err != nil {
log.Printf("Task: fail to read %s\n", fn)
return err
}
if err := e.NewMagnet(string(mag)); err != nil {
return err
}
log.Printf("[RestoreMagnet] Restored: %s \n", fn)
} else {
log.Println("Cache file doesn't match", fn)
}
return nil
}
func (e *Engine) RestoreCacheDir() {
files, err := ioutil.ReadDir(e.cacheDir)
if err != nil {
log.Println("RestoreCacheDir failed read cachedir", err)
return
}
// sort by modtime
sort.Slice(files, func(i, j int) bool {
return files[i].ModTime().Before(files[j].ModTime())
})
for _, i := range files {
if i.IsDir() {
continue
}
common.FancyHandleError(e.RestoreTask(path.Join(e.cacheDir, i.Name())))
}
}
func (e *Engine) NextWaitTask() error {
if !e.isReadyAddTask() {
log.Println("NextWaitTask: engine tasks max")
return ErrMaxConnTasks
}
for {
if elm := e.waitList.Pop(); elm != nil {
var res string
te := elm.(taskElem)
switch te.tp {
case taskTorrent:
res = fmt.Sprintf("%s%s.torrent", cacheSavedPrefix, te.ih)
case taskMagnet:
res = fmt.Sprintf("%s%s.info", cacheSavedPrefix, te.ih)
}
fn := path.Join(e.cacheDir, res)
if _, err := os.Stat(fn); err != nil {
log.Println("NextWaitTask RestoreTask err:", fn, err)
continue
}
return e.RestoreTask(fn)
} else {
log.Println("NextWaitTask: wait list empty")
return ErrWaitListEmpty
}
}
}
func (e *Engine) pushWaitTask(ih string, tp taskType) {
e.waitList.Push(taskElem{ih: ih, tp: tp})
log.Println("waitqueue len", e.waitList.Len())
}