Rewrite state machines
This commit is contained in:
@@ -27,7 +27,7 @@ func CheckFile(directory, torrentName, fileName string, w http.ResponseWriter, r
|
||||
}
|
||||
|
||||
file, ok := torrent.SelectedFiles.Get(fileName)
|
||||
if !ok || !file.State.Is("ok") {
|
||||
if !ok || !file.State.Is("ok_file") {
|
||||
log.Warnf("Cannot find file %s from path %s", fileName, req.URL.Path)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
|
||||
@@ -50,20 +50,20 @@ func (dl *Downloader) DownloadFile(
|
||||
}
|
||||
|
||||
file, ok := torrent.SelectedFiles.Get(fileName)
|
||||
if !ok || !file.State.Is("ok") {
|
||||
if !ok || !file.State.Is("ok_file") {
|
||||
log.Warnf("Cannot find file %s from path %s", fileName, req.URL.Path)
|
||||
http.Error(resp, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if !file.State.Is("ok") {
|
||||
if !file.State.Is("ok_file") {
|
||||
http.Error(resp, "File is not available", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
unrestrict := torMgr.UnrestrictFileUntilOk(file)
|
||||
if unrestrict == nil {
|
||||
err := file.State.Event(context.Background(), "break")
|
||||
err := file.State.Event(context.Background(), "break_file")
|
||||
if err != nil {
|
||||
log.Errorf("File %s is stale: %v", fileName, err)
|
||||
http.Error(resp, "File is stale, please try again", http.StatusLocked)
|
||||
@@ -154,7 +154,7 @@ func (dl *Downloader) streamFileToResponse(
|
||||
downloadResp, err := dl.client.Do(dlReq)
|
||||
if err != nil {
|
||||
if file != nil && unrestrict.Streamable == 1 {
|
||||
err := file.State.Event(context.Background(), "break")
|
||||
err := file.State.Event(context.Background(), "break_file")
|
||||
if err != nil {
|
||||
log.Errorf("File %s is stale: %v", file.Path, err)
|
||||
http.Error(resp, "File is stale, please try again", http.StatusLocked)
|
||||
@@ -177,7 +177,7 @@ func (dl *Downloader) streamFileToResponse(
|
||||
// Check if the download was not successful
|
||||
if downloadResp.StatusCode/100 != 2 {
|
||||
if file != nil && unrestrict.Streamable == 1 {
|
||||
err := file.State.Event(context.Background(), "break")
|
||||
err := file.State.Event(context.Background(), "break_file")
|
||||
if err != nil {
|
||||
log.Errorf("File %s is stale: %v", file.Path, err)
|
||||
http.Error(resp, "File is stale, please try again", http.StatusLocked)
|
||||
|
||||
Reference in New Issue
Block a user