[Skip to content](https://github.com/YouROK/TorrServer/blob/master/server/torr/apihelper.go#start-of-content)

You signed in with another tab or window. [Reload](https://github.com/YouROK/TorrServer/blob/master/server/torr/apihelper.go) to refresh your session.You signed out in another tab or window. [Reload](https://github.com/YouROK/TorrServer/blob/master/server/torr/apihelper.go) to refresh your session.You switched accounts on another tab or window. [Reload](https://github.com/YouROK/TorrServer/blob/master/server/torr/apihelper.go) to refresh your session.Dismiss alert

{{ message }}

[YouROK](https://github.com/YouROK)/ **[TorrServer](https://github.com/YouROK/TorrServer)** Public

- [Notifications](https://github.com/login?return_to=%2FYouROK%2FTorrServer) You must be signed in to change notification settings
- [Fork\\
328](https://github.com/login?return_to=%2FYouROK%2FTorrServer)
- [Star\\
2.8k](https://github.com/login?return_to=%2FYouROK%2FTorrServer)


## Collapse file tree

## Files

master

Search this repository(forward slash)` forward slash/`

/

# apihelper.go

Copy path

Blame

More file actions

Blame

More file actions

## Latest commit

[![VuzzyM](https://avatars.githubusercontent.com/u/66425682?v=4&size=40)](https://github.com/VuzzyM)[VuzzyM](https://github.com/YouROK/TorrServer/commits?author=VuzzyM)

[Fix critical server hangs (](https://github.com/YouROK/TorrServer/commit/e5eca400c298d85822a33246b9b9b2ed20e24396) [#731](https://github.com/YouROK/TorrServer/pull/731) [)](https://github.com/YouROK/TorrServer/commit/e5eca400c298d85822a33246b9b9b2ed20e24396)

Open commit detailsfailure

2 months agoMay 28, 2026

[e5eca40](https://github.com/YouROK/TorrServer/commit/e5eca400c298d85822a33246b9b9b2ed20e24396) · 2 months agoMay 28, 2026

## History

[History](https://github.com/YouROK/TorrServer/commits/master/server/torr/apihelper.go)

Open commit details

[View commit history for this file.](https://github.com/YouROK/TorrServer/commits/master/server/torr/apihelper.go) History

302 lines (264 loc) · 6.13 KB

/

# apihelper.go

Copy path

Top

## File metadata and controls

- Code

- Blame


302 lines (264 loc) · 6.13 KB

[Raw](https://github.com/YouROK/TorrServer/raw/refs/heads/master/server/torr/apihelper.go)

Copy raw file

Download raw file

Open symbols panel

Edit and raw actions

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

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

package torr

import (

"io"

"os"

"path/filepath"

"sort"

"time"

"github.com/anacrolix/torrent"

"github.com/anacrolix/torrent/metainfo"

"server/log"

sets "server/settings"

)

varbts\*BTServer

funcInitApiHelper(bt\*BTServer) {

bts=bt

}

funcLoadTorrent(tor\*Torrent) \*Torrent {

iftor.TorrentSpec==nil {

returnnil

}

tr, err:=NewTorrent(tor.TorrentSpec, bts)

iferr!=nil {

returnnil

}

if!tr.WaitInfo() {

returnnil

}

tr.Title=tor.Title

tr.Poster=tor.Poster

tr.Data=tor.Data

returntr

}

funcAddTorrent(spec\*torrent.TorrentSpec, title, posterstring, datastring, categorystring) (\*Torrent, error) {

torr, err:=NewTorrent(spec, bts)

iferr!=nil {

log.TLogln("error add torrent:", err)

returnnil, err

}

torDB:=GetTorrentDB(spec.InfoHash)

iftorr.Title=="" {

torr.Title=title

iftitle==""&&torDB!=nil {

torr.Title=torDB.Title

}

iftorr.Title==""&&torr.Torrent!=nil&&torr.Torrent.Info() !=nil {

torr.Title=torr.Info().Name

}

}

iftorr.Category=="" {

torr.Category=category

iftorr.Category==""&&torDB!=nil {

torr.Category=torDB.Category

}

}

iftorr.Poster=="" {

torr.Poster=poster

iftorr.Poster==""&&torDB!=nil {

torr.Poster=torDB.Poster

}

}

iftorr.Data=="" {

torr.Data=data

iftorr.Data==""&&torDB!=nil {

torr.Data=torDB.Data

}

}

returntorr, nil

}

funcSaveTorrentToDB(torr\*Torrent) {

log.TLogln("save to db:", torr.Hash())

AddTorrentDB(torr)

}

funcGetTorrent(hashHexstring) \*Torrent {

hash:=metainfo.NewHashFromHex(hashHex)

timeout:=time.Second\*time.Duration(sets.BTsets.TorrentDisconnectTimeout)

iftimeout>time.Minute {

timeout=time.Minute

}

tor:=bts.GetTorrent(hash)

iftor!=nil {

tor.AddExpiredTime(timeout)

returntor

}

tr:=GetTorrentDB(hash)

iftr!=nil {

tor=tr

gofunc() {

log.TLogln("New torrent", tor.Hash())

tr, \_:=NewTorrent(tor.TorrentSpec, bts)

iftr!=nil {

tr.Title=tor.Title

tr.Poster=tor.Poster

tr.Data=tor.Data

tr.Size=tor.Size

tr.Timestamp=tor.Timestamp

tr.Category=tor.Category

tr.GotInfo()

}

}()

}

returntor

}

funcSetTorrent(hashHex, title, poster, categorystring, datastring) \*Torrent {

hash:=metainfo.NewHashFromHex(hashHex)

torr:=bts.GetTorrent(hash)

torrDb:=GetTorrentDB(hash)

iftitle==""&&torr==nil&&torrDb!=nil {

torr=GetTorrent(hashHex)

torr.GotInfo()

iftorr.Torrent!=nil&&torr.Torrent.Info() !=nil {

title=torr.Info().Name

}

}

iftorr!=nil {

iftitle==""&&torr.Torrent!=nil&&torr.Torrent.Info() !=nil {

title=torr.Info().Name

}

torr.Title=title

torr.Poster=poster

torr.Category=category

ifdata!="" {

torr.Data=data

}

}

// update torrent data in DB

iftorrDb!=nil {

torrDb.Title=title

torrDb.Poster=poster

torrDb.Category=category

ifdata!="" {

torrDb.Data=data

}

AddTorrentDB(torrDb)

}

iftorr!=nil {

returntorr

} else {

returntorrDb

}

}

funcRemTorrent(hashHexstring) {

ifsets.ReadOnly {

log.TLogln("API RemTorrent: Read-only DB mode!", hashHex)

return

}

hash:=metainfo.NewHashFromHex(hashHex)

// Download the torrent before deleting it to get the "closed" status

torr:=bts.GetTorrent(hash)

iftorr==nil {

// If the torrent isn't in memory, just delete it from the database and the files

RemTorrentDB(hash)

ifsets.BTsets.UseDisk&&hashHex!=""&&hashHex!="/" {

name:=filepath.Join(sets.BTsets.TorrentsSavePath, hashHex)

os.RemoveAll(name)

}

return

}

closedChan:=torr.closed

// Clear from memory

ifbts.RemoveTorrent(hash) {

// Waiting for confirmation from the library via the closed channel

select {

case<-closedChan:

// The library has confirmed the closure

log.TLogln("Torrent closed by library:", hashHex)

case<-time.After(5\*time.Second):

log.TLogln("Warning: timeout waiting for torrent close:", hashHex)

}

// Now we can safely delete the files from the disk

ifsets.BTsets.UseDisk&&hashHex!=""&&hashHex!="/" {

name:=filepath.Join(sets.BTsets.TorrentsSavePath, hashHex)

if\_, err:=os.Stat(name); err==nil {

log.TLogln("Removing cache files for:", hashHex)

os.RemoveAll(name)

}

}

}

// Delete from the database

RemTorrentDB(hash)

}

funcListTorrent() \[\]\*Torrent {

btlist:=bts.ListTorrents()

dblist:=ListTorrentsDB()

forhash, t:=rangedblist {

if\_, ok:=btlist\[hash\]; !ok {

btlist\[hash\] =t

}

}

varret \[\]\*Torrent

for\_, t:=rangebtlist {

ret=append(ret, t)

}

sort.Slice(ret, func(i, jint) bool {

ifret\[i\].Timestamp!=ret\[j\].Timestamp {

returnret\[i\].Timestamp>ret\[j\].Timestamp

} else {

returnret\[i\].Title>ret\[j\].Title

}

})

returnret

}

funcDropTorrent(hashHexstring) {

hash:=metainfo.NewHashFromHex(hashHex)

bts.RemoveTorrent(hash)

}

funcSetSettings(set\*sets.BTSets) {

ifsets.ReadOnly {

log.TLogln("API SetSettings: Read-only DB mode!")

return

}

sets.SetBTSets(set)

log.TLogln("drop all torrents")

dropAllTorrent()

time.Sleep(time.Second\*1)

log.TLogln("disconect")

bts.Disconnect()

log.TLogln("connect")

bts.Connect()

time.Sleep(time.Second\*1)

log.TLogln("end set settings")

}

funcSetDefSettings() {

ifsets.ReadOnly {

log.TLogln("API SetDefSettings: Read-only DB mode!")

return

}

sets.SetDefaultConfig()

log.TLogln("drop all torrents")

dropAllTorrent()

time.Sleep(time.Second\*1)

log.TLogln("disconect")

bts.Disconnect()

log.TLogln("connect")

bts.Connect()

time.Sleep(time.Second\*1)

log.TLogln("end set default settings")

}

funcdropAllTorrent() {

for\_, torr:=rangebts.torrents {

torr.drop()

<-torr.closed

}

}

funcShutdown() {

bts.Disconnect()

sets.CloseDB()

log.TLogln("Received shutdown. Quit")

os.Exit(0)

}

funcWriteStatus(w io.Writer) {

bts.client.WriteStatus(w)

}

funcPreload(torr\*Torrent, indexint) {

cache:=float32(sets.BTsets.CacheSize)

preload:=float32(sets.BTsets.PreloadCache)

size:=int64((cache/100.0) \*preload)

ifsize<=0 {

return

}

ifsize>sets.BTsets.CacheSize {

size=sets.BTsets.CacheSize

}

torr.Preload(index, size)

}

You can’t perform that action at this time.