mirror of https://github.com/casbin/casnode.git
Add downloadFileSafe().
This commit is contained in:
parent
b77726953a
commit
29fe4370ea
|
@ -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)
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue