use a new thread safe map
This commit is contained in:
@@ -46,22 +46,28 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentM
|
||||
accessKey := segments[len(segments)-2]
|
||||
filename := segments[len(segments)-1]
|
||||
|
||||
torrent, _ := t.TorrentMap.Get(accessKey)
|
||||
if torrent == nil {
|
||||
torrents, ok := t.DirectoryMap.Get(baseDirectory)
|
||||
if !ok {
|
||||
log.Warnf("Cannot find directory %s", baseDirectory)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
torrent, ok := torrents.Get(accessKey)
|
||||
if !ok {
|
||||
log.Warnf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
file, _ := torrent.SelectedFiles.Get(filename)
|
||||
if file == nil {
|
||||
file, ok := torrent.SelectedFiles.Get(filename)
|
||||
if !ok {
|
||||
log.Warnf("Cannot find file from path %s", requestPath)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if data, exists := cache.Get(requestPath); exists {
|
||||
streamFileToResponse(torrent, data, w, r, t, c, log)
|
||||
streamFileToResponse(data, w, r, t, c, log)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -75,7 +81,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentM
|
||||
|
||||
resp := t.UnrestrictUntilOk(link)
|
||||
if resp == nil {
|
||||
go t.Repair(torrent.AccessKey)
|
||||
// go t.Repair(torrent.AccessKey)
|
||||
log.Warnf("File %s is no longer available, torrent is marked for repair", file.Path)
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log)
|
||||
return
|
||||
@@ -93,15 +99,15 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentM
|
||||
}
|
||||
}
|
||||
cache.Add(requestPath, resp.Download)
|
||||
streamFileToResponse(torrent, resp.Download, w, r, t, c, log)
|
||||
streamFileToResponse(resp.Download, w, r, t, c, log)
|
||||
}
|
||||
|
||||
func streamFileToResponse(torrent *intTor.Torrent, url string, w http.ResponseWriter, r *http.Request, t *intTor.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) {
|
||||
func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) {
|
||||
// Create a new request for the file download.
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
log.Errorf("Error creating new request: %v", err)
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, t, c, log)
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, torMgr, cfg, log)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -111,25 +117,25 @@ func streamFileToResponse(torrent *intTor.Torrent, url string, w http.ResponseWr
|
||||
}
|
||||
|
||||
// Create a custom HTTP client
|
||||
client := zurghttp.NewHTTPClient(c.GetToken(), 10, c)
|
||||
client := zurghttp.NewHTTPClient(cfg.GetToken(), 10, cfg)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Warnf("Cannot download file %v ; torrent is marked for repair", err)
|
||||
if torrent != nil {
|
||||
go t.Repair(torrent.AccessKey)
|
||||
}
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, t, c, log)
|
||||
// if torrent != nil {
|
||||
// go t.Repair(torrent.AccessKey)
|
||||
// }
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, torMgr, cfg, log)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
|
||||
log.Warnf("Received a %s status code ; torrent is marked for repair", resp.Status)
|
||||
if torrent != nil {
|
||||
go t.Repair(torrent.AccessKey)
|
||||
}
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=BcseUxviVqE", w, r, t, c, log)
|
||||
// if torrent != nil {
|
||||
// go t.Repair(torrent.AccessKey)
|
||||
// }
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=BcseUxviVqE", w, r, torMgr, cfg, log)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -139,7 +145,7 @@ func streamFileToResponse(torrent *intTor.Torrent, url string, w http.ResponseWr
|
||||
}
|
||||
}
|
||||
|
||||
buf := make([]byte, c.GetNetworkBufferSize())
|
||||
buf := make([]byte, cfg.GetNetworkBufferSize())
|
||||
io.CopyBuffer(w, resp.Body, buf)
|
||||
}
|
||||
|
||||
@@ -149,7 +155,7 @@ func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, t *in
|
||||
http.Error(w, "REAL-DEBRID IS DOWN", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
streamFileToResponse(nil, resp.Download, w, r, t, c, log)
|
||||
streamFileToResponse(resp.Download, w, r, t, c, log)
|
||||
}
|
||||
|
||||
func createErrorFile(path, link string) *intTor.File {
|
||||
@@ -160,7 +166,7 @@ func createErrorFile(path, link string) *intTor.File {
|
||||
return &ret
|
||||
}
|
||||
|
||||
func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, size int, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) []byte {
|
||||
func GetFileReader(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") {
|
||||
@@ -168,11 +174,11 @@ func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, siz
|
||||
return nil
|
||||
}
|
||||
log.Warnf("File %s is no longer available, torrent is marked for repair", file.Path)
|
||||
if torrent != nil {
|
||||
go torMgr.Repair(torrent.AccessKey)
|
||||
}
|
||||
// if torrent != nil {
|
||||
// go torMgr.Repair(torrent.AccessKey)
|
||||
// }
|
||||
errFile := createErrorFile("unavailable.mp4", "https://www.youtube.com/watch?v=gea_FJrtFVA")
|
||||
return GetFileReader(nil, errFile, 0, 0, torMgr, cfg, log)
|
||||
return GetFileReader(errFile, 0, 0, torMgr, cfg, log)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, unres.Download, nil)
|
||||
@@ -183,7 +189,7 @@ 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, torMgr, cfg, log)
|
||||
return GetFileReader(errFile, 0, 0, torMgr, cfg, log)
|
||||
}
|
||||
|
||||
if size == 0 {
|
||||
@@ -199,11 +205,11 @@ func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, siz
|
||||
return nil
|
||||
}
|
||||
log.Warnf("Cannot download file %v ; torrent is marked for repair", err)
|
||||
if torrent != nil {
|
||||
go torMgr.Repair(torrent.AccessKey)
|
||||
}
|
||||
// if torrent != nil {
|
||||
// go torMgr.Repair(torrent.AccessKey)
|
||||
// }
|
||||
errFile := createErrorFile("cannot_download.mp4", "https://www.youtube.com/watch?v=FSSd8cponAA")
|
||||
return GetFileReader(nil, errFile, 0, 0, torMgr, cfg, log)
|
||||
return GetFileReader(errFile, 0, 0, torMgr, cfg, log)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
|
||||
@@ -212,11 +218,11 @@ func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, siz
|
||||
return nil
|
||||
}
|
||||
log.Warnf("Received a %s status code ; torrent is marked for repair", resp.Status)
|
||||
if torrent != nil {
|
||||
go torMgr.Repair(torrent.AccessKey)
|
||||
}
|
||||
// if torrent != nil {
|
||||
// go torMgr.Repair(torrent.AccessKey)
|
||||
// }
|
||||
errFile := createErrorFile("not_ok_status.mp4", "https://www.youtube.com/watch?v=BcseUxviVqE")
|
||||
return GetFileReader(nil, errFile, 0, 0, torMgr, cfg, log)
|
||||
return GetFileReader(errFile, 0, 0, torMgr, cfg, log)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
requestedBytes, err := io.ReadAll(resp.Body)
|
||||
@@ -224,7 +230,7 @@ func GetFileReader(torrent *intTor.Torrent, file *intTor.File, offset int64, siz
|
||||
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 GetFileReader(errFile, 0, 0, torMgr, cfg, log)
|
||||
}
|
||||
}
|
||||
return requestedBytes
|
||||
|
||||
Reference in New Issue
Block a user