Implement new fsm
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package universal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
@@ -49,20 +50,25 @@ func (dl *Downloader) DownloadFile(
|
||||
}
|
||||
|
||||
file, ok := torrent.SelectedFiles.Get(fileName)
|
||||
if !ok || file.IsDeleted {
|
||||
if !ok || !file.State.Is("ok") {
|
||||
log.Warnf("Cannot find file %s from path %s", fileName, req.URL.Path)
|
||||
http.Error(resp, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if file.IsBroken {
|
||||
if !file.State.Is("ok") {
|
||||
http.Error(resp, "File is not available", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
unrestrict := torMgr.UnrestrictFileUntilOk(file)
|
||||
if unrestrict == nil {
|
||||
file.IsBroken = true
|
||||
err := file.State.Event(context.Background(), "break")
|
||||
if err != nil {
|
||||
log.Errorf("File %s is stale: %v", fileName, err)
|
||||
http.Error(resp, "File is stale, please try again", http.StatusLocked)
|
||||
return
|
||||
}
|
||||
if cfg.EnableRepair() {
|
||||
log.Warnf("File %s cannot be unrestricted (link=%s) (repairing...)", fileName, file.Link)
|
||||
torMgr.TriggerRepair(torrent)
|
||||
@@ -148,7 +154,12 @@ func (dl *Downloader) streamFileToResponse(
|
||||
downloadResp, err := dl.client.Do(dlReq)
|
||||
if err != nil {
|
||||
if file != nil && unrestrict.Streamable == 1 {
|
||||
file.IsBroken = true
|
||||
err := file.State.Event(context.Background(), "break")
|
||||
if err != nil {
|
||||
log.Errorf("File %s is stale: %v", file.Path, err)
|
||||
http.Error(resp, "File is stale, please try again", http.StatusLocked)
|
||||
return
|
||||
}
|
||||
if cfg.EnableRepair() && torrent != nil {
|
||||
log.Warnf("Cannot download file %s: %v (repairing...)", unrestrict.Download, err)
|
||||
torMgr.TriggerRepair(torrent)
|
||||
@@ -166,7 +177,12 @@ func (dl *Downloader) streamFileToResponse(
|
||||
// Check if the download was not successful
|
||||
if downloadResp.StatusCode/100 != 2 {
|
||||
if file != nil && unrestrict.Streamable == 1 {
|
||||
file.IsBroken = true
|
||||
err := file.State.Event(context.Background(), "break")
|
||||
if err != nil {
|
||||
log.Errorf("File %s is stale: %v", file.Path, err)
|
||||
http.Error(resp, "File is stale, please try again", http.StatusLocked)
|
||||
return
|
||||
}
|
||||
if cfg.EnableRepair() && torrent != nil {
|
||||
log.Warnf("Received a %s status code for file %s (repairing...)", downloadResp.Status, file.Path)
|
||||
torMgr.TriggerRepair(torrent)
|
||||
|
||||
Reference in New Issue
Block a user