Big refactor

This commit is contained in:
Ben Sarmiento
2023-11-14 01:55:00 +01:00
parent 5e723a9e8a
commit 16505ec33f
26 changed files with 1602 additions and 453 deletions

View File

@@ -160,8 +160,8 @@ func createErrorFile(path, link string) *intTor.File {
return &ret
}
func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, size int, t *intTor.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) io.ReadCloser {
unres := t.UnrestrictUntilOk(file.Link)
func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, size int, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) []byte {
unres := torMgr.UnrestrictUntilOk(file.Link)
if unres == nil {
if strings.Contains(file.Link, "www.youtube.com") {
log.Errorf("Even the error page is broken! Sorry!")
@@ -169,10 +169,10 @@ func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, siz
}
log.Warnf("File %s is no longer available, torrent is marked for repair", file.Path)
if torrent != nil {
go t.Repair(torrent.AccessKey)
go torMgr.Repair(torrent.AccessKey)
}
errFile := createErrorFile("unavailable.mp4", "https://www.youtube.com/watch?v=gea_FJrtFVA")
return GetFileReader(nil, errFile, 0, 0, t, c, log)
return GetFileReader(nil, errFile, 0, 0, torMgr, cfg, log)
}
req, err := http.NewRequest(http.MethodGet, unres.Download, nil)
@@ -183,15 +183,15 @@ func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, siz
}
log.Errorf("Error creating new request: %v", err)
errFile := createErrorFile("new_request.mp4", "https://www.youtube.com/watch?v=H3NSrObyAxM")
return GetFileReader(nil, errFile, 0, 0, t, c, log)
return GetFileReader(nil, errFile, 0, 0, torMgr, cfg, log)
}
if size == 0 {
size = int(file.Bytes)
}
req.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", offset, size-1))
req.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", offset, offset+int64(size)-1))
client := zurghttp.NewHTTPClient(c.GetToken(), 10, c)
client := zurghttp.NewHTTPClient(cfg.GetToken(), 10, cfg)
resp, err := client.Do(req)
if err != nil {
if strings.Contains(file.Link, "www.youtube.com") {
@@ -200,10 +200,10 @@ func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, siz
}
log.Warnf("Cannot download file %v ; torrent is marked for repair", err)
if torrent != nil {
go t.Repair(torrent.AccessKey)
go torMgr.Repair(torrent.AccessKey)
}
errFile := createErrorFile("cannot_download.mp4", "https://www.youtube.com/watch?v=FSSd8cponAA")
return GetFileReader(nil, errFile, 0, 0, t, c, log)
return GetFileReader(nil, errFile, 0, 0, torMgr, cfg, log)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
@@ -213,11 +213,19 @@ func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, siz
}
log.Warnf("Received a %s status code ; torrent is marked for repair", resp.Status)
if torrent != nil {
go t.Repair(torrent.AccessKey)
go torMgr.Repair(torrent.AccessKey)
}
errFile := createErrorFile("not_ok_status.mp4", "https://www.youtube.com/watch?v=BcseUxviVqE")
return GetFileReader(nil, errFile, 0, 0, t, c, log)
return GetFileReader(nil, errFile, 0, 0, torMgr, cfg, log)
}
return resp.Body
defer resp.Body.Close()
requestedBytes, err := io.ReadAll(resp.Body)
if err != nil {
if err != io.EOF {
log.Errorf("Error reading bytes: %v", err)
errFile := createErrorFile("read_error.mp4", "https://www.youtube.com/watch?v=t9VgOriBHwE")
return GetFileReader(nil, errFile, 0, 0, torMgr, cfg, log)
}
}
return requestedBytes
}