// TODO(mpl): do not use just an endTimeout. instead, only start a countdown if/when size of download stops progressing. and reset it everytime dowload makes progress again.
varendTimeouttime.Time
startTimeout:=time.Now().Add(time.Minute)
tick:=500*time.Millisecond
varfileSizeint64
timeout:=time.Now().Add(time.Minute)
for{
time.Sleep(tick)
ifstarted&&time.Now().After(endTimeout){
return"",fmt.Errorf("timeout while downloading in %q",dir)
}
// TODO(mpl): download starts late if it's a video. figure out if dl can only
// start after video has started playing or something like that?
if!started&&time.Now().After(startTimeout){
if!started&&time.Now().After(timeout){
return"",fmt.Errorf("downloading in %q took too long to start",dir)
}
ifstarted&&time.Now().After(timeout){
return"",fmt.Errorf("timeout while downloading in %q",dir)
}
entries,err:=ioutil.ReadDir(dir)
iferr!=nil{
return"",err
}
varfileEntries[]string
varfileEntries[]os.FileInfo
for_,v:=rangeentries{
ifv.IsDir(){
continue
@ -171,24 +170,29 @@ func main() {
ifv.Name()==".lastdone"{
continue
}
fileEntries=append(fileEntries,v.Name())
fileEntries=append(fileEntries,v)
}
iflen(fileEntries)<1{
continue
}
iflen(fileEntries)>1{
return"",fmt.Errorf("more than one file (%d) in download dir %q",len(fileEntries),dir)
}
if!started{
iflen(fileEntries)>0{
started=true
endTimeout =time.Now().Add(time.Minute)
timeout =time.Now().Add(time.Minute)
}
continue
}
iflen(fileEntries)>1{
return"",fmt.Errorf("more than one file (%d) in download dir %q",len(fileEntries),dir)
newFileSize:=fileEntries[0].Size()
ifnewFileSize>fileSize{
// push back the timeout as long as we make progress