當前位置: 首頁>>代碼示例>>Golang>>正文


Golang AuthenticatedRequest.FormFile方法代碼示例

本文整理匯總了Golang中github.com/abbot/go-http-auth.AuthenticatedRequest.FormFile方法的典型用法代碼示例。如果您正苦於以下問題:Golang AuthenticatedRequest.FormFile方法的具體用法?Golang AuthenticatedRequest.FormFile怎麽用?Golang AuthenticatedRequest.FormFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/abbot/go-http-auth.AuthenticatedRequest的用法示例。


在下文中一共展示了AuthenticatedRequest.FormFile方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: insertHandler

func insertHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
	file, header, err := r.FormFile("file")
	if err != nil {
		log.Error("Invalid form data %v:%s", r.Header, err.Error())
		fmt.Fprintln(w, err)
		return
	}
	log.Debug("Insert handler started for file %s", header.Filename)
	tag := r.Request.Header.Get("File-Tag")
	if len(tag) == 0 {
		log.Error("No file-tag header")
		fmt.Fprintf(w, "No file-tag header")
		return
	}

	bytes, err := base64.StdEncoding.DecodeString(header.Filename)
	if err != nil {
		log.Error("Filename %s is not valid base64 string", header.Filename)
		fmt.Fprintf(w, "Filename is not valid base64 string")
		return
	}
	filename := string(bytes)

	id, err := RegisterFile(filename, tag, time.Now())
	if err != nil {
		log.Error("Unable to register file %s from %s in DB:%s", filename, r.Host, err.Error())
		fmt.Fprintf(w, "Unable to register file")
		return
	}

	path, dir, err := CreateFilePath(id)

	if err != nil {
		log.Error("Unable to create file path for %s from %s in DB:%s", filename, r.Host, err.Error())
		fmt.Fprintf(w, "Unable to create file path")
		return
	}

	out, err := os.Create(path)
	if err != nil {
		log.Error("Unable to save file %s from %s in DB:%s", filename, r.Host, err.Error())
		fmt.Fprintf(w, "Unable to save file")
		return
	}

	_, err = io.Copy(out, file)
	if err != nil {
		fmt.Fprintln(w, err)
	}

	out.Close()
	file.Close()
	err = SaveMetaData(id, filename, tag, dir)
	if err != nil {
		log.Error("can't save meta for %d:%s", id, err.Error())
	}
	err = ConfirmFile(id)
	if err != nil {
		log.Error("Unable to commit file %s from %s in DB:%s", filename, r.Host, err.Error())
		fmt.Fprintf(w, "Unable to commit file")
		return
	}
	w.Header().Add("File-Id", strconv.FormatInt(id, 10))
	fmt.Fprintf(w, "%d", id)
	log.Info("File %s from %s saved succesfully", filename, r.Host)
}
開發者ID:Pereiro,項目名稱:fstore,代碼行數:66,代碼來源:http.go


注:本文中的github.com/abbot/go-http-auth.AuthenticatedRequest.FormFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。