Clean up
This commit is contained in:
@@ -38,7 +38,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
}
|
||||
|
||||
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
|
||||
link := findLinkByFragment(torrents, filenameV2, linkFragment)
|
||||
link := getLink(torrents, filenameV2, linkFragment)
|
||||
if link == "" {
|
||||
log.Println("Link not found")
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
@@ -50,27 +50,25 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
}
|
||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
||||
if resp == nil {
|
||||
// TODO: Delete the link from the database
|
||||
// TODO: Readd the file
|
||||
// when unrestricting fails, it means the file is not available anymore
|
||||
// if it's the only file, tough luck
|
||||
log.Println("Cannot unrestrict link")
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if resp.Filename != filenameV2 {
|
||||
// TODO: Redo the logic to handle mismatch
|
||||
// Filename mismatch
|
||||
// [SRS] Pokemon S22E01-35 1080p WEBRip AAC 2.0 x264 CC.rar
|
||||
// Pokemon.S22E24.The.Secret.Princess.DUBBED.1080p.WEBRip.AAC.2.0.x264-SRS.mkv
|
||||
// Filename mismatch
|
||||
// Adventure.Time.S06E10.Something.Big.1080p.HMAX.WEBRip.DD.2.0.H.265.-EDGE2020.mkv
|
||||
// Adventure.Time.S06E03.James.II.1080p.HMAX.WEBRip.DD.2.0.H.265.-EDGE2020.mkv
|
||||
// Action: schedule a "cleanup" job for the parent torrent
|
||||
log.Println("Filename mismatch", resp.Filename, filenameV2)
|
||||
}
|
||||
http.Redirect(w, r, resp.Download, http.StatusFound)
|
||||
}
|
||||
|
||||
// findLinkByFragment finds a link by a fragment, it might be wrong
|
||||
func findLinkByFragment(torrents []torrent.Torrent, filename, fragment string) string {
|
||||
// getLink finds a link by a fragment, it might be wrong
|
||||
func getLink(torrents []torrent.Torrent, filename, fragment string) string {
|
||||
for _, torrent := range torrents {
|
||||
for _, file := range torrent.SelectedFiles {
|
||||
fname := filepath.Base(file.Path)
|
||||
|
||||
@@ -51,32 +51,32 @@ func handleRoot(w http.ResponseWriter, r *http.Request) ([]byte, error) {
|
||||
|
||||
// handleListOfTorrents handles a PROPFIND request to the /torrents directory
|
||||
func handleListOfTorrents(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
|
||||
allTorrents := t.GetAll()
|
||||
allTorrentsResponse, err := createMultiTorrentResponse(allTorrents)
|
||||
torrents := t.GetAll()
|
||||
resp, err := createMultiTorrentResponse(torrents)
|
||||
if err != nil {
|
||||
log.Printf("Cannot read directory (/torrents): %v\n", err.Error())
|
||||
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
||||
return nil, nil
|
||||
}
|
||||
return xml.MarshalIndent(allTorrentsResponse, "", " ")
|
||||
return xml.MarshalIndent(resp, "", " ")
|
||||
}
|
||||
|
||||
// handleSingleTorrent handles a PROPFIND request to a single torrent directory
|
||||
func handleSingleTorrent(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
torrentName := path.Base(requestPath)
|
||||
foundTorrents := findAllTorrentsWithName(t, torrentName)
|
||||
if len(foundTorrents) == 0 {
|
||||
torrents := findAllTorrentsWithName(t, torrentName)
|
||||
if len(torrents) == 0 {
|
||||
log.Println("Cannot find directory", requestPath)
|
||||
http.Error(w, "Cannot find directory", http.StatusNotFound)
|
||||
return nil, nil
|
||||
}
|
||||
var torrentResponse *dav.MultiStatus
|
||||
torrentResponse, err := createCombinedTorrentResponse(foundTorrents, t)
|
||||
var resp *dav.MultiStatus
|
||||
resp, err := createCombinedTorrentResponse(torrents, t)
|
||||
if err != nil {
|
||||
log.Printf("Cannot read directory (%s): %v\n", requestPath, err.Error())
|
||||
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
||||
return nil, nil
|
||||
}
|
||||
return xml.MarshalIndent(torrentResponse, "", " ")
|
||||
return xml.MarshalIndent(resp, "", " ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user