Mount downloads directory

This commit is contained in:
Ben Sarmiento
2024-01-08 22:56:10 +01:00
parent 96e41d6cbc
commit 5a23d0ff7b
6 changed files with 145 additions and 62 deletions

View File

@@ -21,8 +21,8 @@ func NewGetFile(client *zurghttp.HTTPClient) *GetFile {
return &GetFile{client: client}
}
// ServeFile handles a GET request universally for both WebDAV and HTTP
func (gf *GetFile) ServeFile(directory, torrentName, fileName string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) {
// DownloadFile handles a GET request for files in torrents
func (gf *GetFile) DownloadFile(directory, torrentName, fileName string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
log.Warnf("Cannot find directory %s", directory)
@@ -53,7 +53,7 @@ func (gf *GetFile) ServeFile(directory, torrentName, fileName string, resp http.
log.Debugf("Opening file %s from torrent %s (%s)", fileName, torrentName, link)
unrestrict := torMgr.UnrestrictUntilOk(link)
if unrestrict == nil || unrestrict.Link == "" {
if unrestrict == nil {
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link)
if cfg.EnableRepair() {
file.Link = "repair"
@@ -93,6 +93,51 @@ func (gf *GetFile) ServeFile(directory, torrentName, fileName string, resp http.
}
}
// DownloadLink handles a GET request for downloads
func (gf *GetFile) DownloadLink(fileName, link string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) {
if !strings.HasPrefix(link, "http") {
// This is a dead file, serve an alternate file
log.Warnf("File %s is not available", fileName)
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
log.Debugf("Opening download %s", fileName)
unrestrict := torMgr.UnrestrictUntilOk(link)
if unrestrict == nil {
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link)
http.Error(resp, "File is not available", http.StatusNotFound)
return
} else {
if unrestrict.Filename != fileName {
// this is possible if there's only 1 streamable file in the torrent
// and then suddenly it's a rar file
actualExt := filepath.Ext(unrestrict.Filename)
expectedExt := filepath.Ext(fileName)
if actualExt != expectedExt && unrestrict.Streamable != 1 {
log.Warnf("File was changed and is not streamable: %s and %s (link=%s)", fileName, unrestrict.Filename, unrestrict.Link)
http.Error(resp, "File is not available", http.StatusNotFound)
return
} else {
log.Warnf("Filename mismatch: %s and %s", fileName, unrestrict.Filename)
}
}
if cfg.ShouldServeFromRclone() {
if cfg.ShouldVerifyDownloadLink() {
if !torMgr.Api.CanFetchFirstByte(unrestrict.Download) {
log.Warnf("File %s is not available", fileName)
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
}
redirect(resp, req, unrestrict.Download, cfg)
} else {
gf.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log)
}
return
}
}
func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.File, unrestrict *realdebrid.Download, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) {
// Create a new request for the file download.
dlReq, err := http.NewRequest(http.MethodGet, unrestrict.Download, nil)
@@ -113,7 +158,7 @@ func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.Fi
if err != nil {
if file != nil && unrestrict.Streamable == 1 {
log.Warnf("Cannot download file %s: %v", file.Path, err)
if cfg.EnableRepair() {
if cfg.EnableRepair() && torrent != nil {
file.Link = "repair"
torMgr.Repair(torrent)
} else {
@@ -128,7 +173,7 @@ func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.Fi
if download.StatusCode != http.StatusOK && download.StatusCode != http.StatusPartialContent {
if file != nil && unrestrict.Streamable == 1 {
log.Warnf("Received a %s status code for file %s", download.Status, file.Path)
if cfg.EnableRepair() {
if cfg.EnableRepair() && torrent != nil {
file.Link = "repair"
torMgr.Repair(torrent)
} else {