亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 mime/multipart 上傳會損壞文件

使用 mime/multipart 上傳會損壞文件

Go
人到中年有點甜 2023-08-07 14:39:36
我寫了一個服務器,有上傳圖片的路由。這是一個接收一些參數的表單:title、description和visibility。picture該頁面還使用Authentication標題。func UploadPictureRoute(prv *services.Provider) http.HandlerFunc {    return func(w http.ResponseWriter, r *http.Request) {        user, err := auth.ValidateRequest(prv, w, r)        if auth.RespondError(w, err) {            return        }        r.ParseMultipartForm(10 << 20) // 10 meg max        title := r.FormValue("title")        desc := r.FormValue("description")        visib := r.FormValue("visibility")        visibInt, err := strconv.Atoi(visib)        visibility := int8(visibInt) // Visibility can be either 0, 1, 2        if err != nil {            w.WriteHeader(http.StatusBadRequest)        }        file, _, err := r.FormFile("picture")        if err != nil {            w.WriteHeader(http.StatusBadRequest)            return        }        defer file.Close()        mimeType, _, err := mimetype.DetectReader(file) // Package gabriel-vasile/mimetype        if err != nil {            w.WriteHeader(http.StatusBadRequest)            return        }        if !utils.IsValidMimetype(mimeType) { // Basically just comparing to image/png, image/jpg. Crashes here            w.WriteHeader(http.StatusBadRequest)            return        }        parentFolder := prv.PicturePath + "/" + strconv.FormatInt(*user.ID, 10) + "/"        _, err = os.Stat(parentFolder)        if os.IsNotExist(err) {            err = os.MkdirAll(parentFolder, os.ModePerm)            if err != nil {                w.WriteHeader(http.StatusInternalServerError)                return            }        }        pict := model.Picture{            Title:       title,            Description: desc,            Creator:     &user,            Visibility:  visibility,            Ext:         utils.GetExtForMimetype(mimeType),        }這段代碼會導致服務器崩潰。圖片的mimetype變成application/octet-stream并且圖像標題被破壞(它仍然在某些編輯器中打開,但EyesOfGnome基本上說圖片不是JPG/PNG文件,因為它找不到開頭的幻數)如何修復HTTP go客戶端才能成功上傳圖片?
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

調用mimetype.DetectReader(file)讀取部分文件。調用 _, err = io.Copy(pict, file)讀取文件的其余部分。要讀取整個文件,請回溯到調用 之前的偏移量 0 io.Copy。


文件在偏移量 0 處打開。調用 后無需立即查找偏移量 0 Open。


通過交換調用順序來修復問題:


...


mime, _, err := mimetype.DetectReader(file)

if err != nil {

    fmt.Println("Can't read the file")

    return

}


// Rewind to the start of the file

_, err = file.Seek(0, io.SeekStart)

if err != nil {

    fmt.Println("Can't read the file")

    return

}


...

服務器也有類似的問題。檢測類型后回退:


mimeType, _, err := mimetype.DetectReader(file) // Package gabriel-vasile/mimetype

if err != nil {

    w.WriteHeader(http.StatusBadRequest)

    return

}


// Rewind to the start of the file

_, err = file.Seek(0, io.SeekStart)

if err != nil {

    w.WriteHeader(http.StatusInternalServerError)

    return

}


查看完整回答
反對 回復 2023-08-07
  • 1 回答
  • 0 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號