Add downloadFileSafe().

This commit is contained in:
Gucheng Wang 2021-12-04 15:22:44 +08:00
parent b77726953a
commit 29fe4370ea
2 changed files with 21 additions and 1 deletions

View File

@ -78,7 +78,7 @@ func uploadDiscuzxFile(username string, fileBytes []byte, fileName string, creat
func getRecordFromAttachment(attachment *Attachment, post *Post) *object.UploadFileRecord {
oldFileUrl := fmt.Sprintf("%s%s", discuzxAttachmentBaseUrl, attachment.Attachment)
fileBytes, _, err := downloadFile(oldFileUrl)
fileBytes, _, err := downloadFileSafe(oldFileUrl)
if err != nil {
if urlError, ok := err.(*url.Error); ok {
fmt.Printf("\t\t[%d]: getRecordFromAttachment() error: %s, the attachement is deleted: %s\n", post.Pid, urlError.Error(), attachment.Attachment)

View File

@ -144,6 +144,26 @@ func downloadFile(url string) ([]byte, string, error) {
return bs, newUrl, nil
}
func downloadFileSafe(url string) ([]byte, string, error) {
var bs []byte
var newUrl string
var err error
times := 0
for {
bs, newUrl, err = downloadFile(url)
if err != nil {
times += 1
time.Sleep(3 * time.Second)
if times >= 10 {
return nil, "", err
}
} else {
break
}
}
return bs, newUrl, nil
}
func getStringHash(s string) int {
h := fnv.New32a()
h.Write([]byte(s))