当前位置: 首页>>代码示例>>Golang>>正文


Golang File.Read方法代码示例

本文整理汇总了Golang中mime/multipart.File.Read方法的典型用法代码示例。如果您正苦于以下问题:Golang File.Read方法的具体用法?Golang File.Read怎么用?Golang File.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mime/multipart.File的用法示例。


在下文中一共展示了File.Read方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: StoreRelease

// StoreRelease saves what was uploaded. Buffered in case of large files.
func (r Release) StoreRelease(data multipart.File) {
	f, err := os.Create(r.ReleaseFilePath())
	if err != nil {
		log.Println("Can't open file: " + r.ReleaseFilePath())
		return
	}
	defer f.Close()

	buf := make([]byte, 1024)

	for {
		n, err := data.Read(buf)

		if err != nil && err != io.EOF {
			log.Println("Couldn't write file: " + r.ReleaseFilePath())
			break
		}

		if n == 0 {
			break
		}

		if _, err := f.Write(buf[:n]); err != nil {
			log.Println("Couldn't write file: " + r.ReleaseFilePath())
			break
		}
	}
}
开发者ID:geetarista,项目名称:fondu,代码行数:29,代码来源:release.go

示例2: ServeHTTP

func (h httpUploadHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		v                *Volume
		n                int
		vid, key, cookie int64
		err              error
		buf              []byte
		file             multipart.File
		res              = map[string]interface{}{"ret": RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	if err = r.ParseMultipartForm(NeedleMaxSize); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	if vid, err = strconv.ParseInt(r.FormValue("vid"), 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("vid"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if key, err = strconv.ParseInt(r.FormValue("key"), 10, 64); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("key"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if cookie, err = strconv.ParseInt(r.FormValue("cookie"), 10, 64); err !=
		nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("cookie"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if v = h.s.Volumes[int32(vid)]; v == nil {
		res["ret"] = RetNoVolume
		return
	}
	if file, _, err = r.FormFile("file"); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	buf = v.Buffer()
	if n, err = file.Read(buf); err == nil {
		err = v.Add(key, cookie, buf[:n])
	}
	file.Close()
	v.FreeBuffer(buf)
	if err != nil {
		res["ret"] = RetUploadErr
	}
	return
}
开发者ID:qida,项目名称:bfs,代码行数:57,代码来源:http_api.go

示例3: judgeContentType

func judgeContentType(f multipart.File) (string, string, error) {
	f.Seek(0, 0)
	buf := make([]byte, 16)
	count, err := f.Read(buf)
	if err != nil {
		return "", "", err
	}

	if count >= 10 && string(buf[6:10]) == "JFIF" {
		return "image/jpeg", ".jpg", nil
	}
	if count >= 4 && string(buf[0:3]) == "GIF" {
		return "image/gif", ".gif", nil
	}
	if count >= 2 && string(buf[1:4]) == "PNG" {
		return "image/png", ".png", nil
	}

	return "application/octet-stream", "", nil
}
开发者ID:moznion,项目名称:gyazo-server,代码行数:20,代码来源:image.go

示例4: uploadFile

func uploadFile(file multipart.File) string {
	clientid := os.Getenv("DROPBOX_KEY")
	clientsecret := os.Getenv("DROPBOX_TOKEN")
	token := os.Getenv("DROPBOX_ACCESS_TOKEN")

	DB := dropbox.NewDropbox()
	DB.SetAppInfo(clientid, clientsecret)
	DB.SetAccessToken(token)

	filename := fmt.Sprintf("%s.png", strconv.FormatInt(rand.Int63(), 32))
	filepath := fmt.Sprintf("/Public/Screenshots/%s", filename)

	var tmpstr []byte
	length, _ := file.Read(tmpstr)
	DB.FilesPut(file, int64(length), filepath, true, "")

	account, _ := DB.GetAccountInfo()
	uid := strconv.FormatInt(int64(account.UID), 10)

	return fmt.Sprintf("https://dl.dropbox.com/u/%s/Screenshots/%s", uid, filename)
}
开发者ID:Randuin,项目名称:imagehost,代码行数:21,代码来源:imagehost.go

示例5: isSupported

func isSupported(file multipart.File) (bool, string, error) {
	buff := make([]byte, 512)
	_, err := file.Read(buff)

	// Reset the file
	file.Seek(0, 0)

	if err != nil {
		return false, "", err
	}

	filetype := http.DetectContentType(buff)

	for _, i := range supportedFileTypes {
		if i == filetype {
			return true, filetype, nil
		}
	}

	log.Println("File not supported: " + filetype)
	return false, filetype, nil
}
开发者ID:kizbitz,项目名称:webapp,代码行数:22,代码来源:photo.go

示例6: ServeHTTP

func (h httpUploadsHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		i, rn, tn, nn, nb int
		ok                bool
		buf               []byte
		err               error
		vid               int64
		key               int64
		cookie            int64
		size              int64
		str               string
		keys              []string
		cookies           []string
		sr                sizer
		fr                *os.File
		fi                os.FileInfo
		v                 *Volume
		n                 *needle.Needle
		ns                []needle.Needle
		uerr              errors.Error
		file              multipart.File
		fh                *multipart.FileHeader
		fhs               []*multipart.FileHeader
		res               = map[string]interface{}{"ret": errors.RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	// check total content-length
	if size, err = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if size > int64(h.c.NeedleMaxSize*h.c.BatchMaxNum) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	keys = r.MultipartForm.Value["keys"]
	cookies = r.MultipartForm.Value["cookies"]
	if len(keys) != len(cookies) {
		log.Errorf("param length not match, keys: %d, cookies: %d", len(keys), len(cookies))
		res["ret"] = errors.RetParamErr
		return
	}
	fhs = r.MultipartForm.File["file"]
	nn = len(fhs)
	if len(keys) != nn {
		log.Errorf("param length not match, keys: %d, cookies: %d, files: %d", len(keys), len(cookies), len(fhs))
		res["ret"] = errors.RetParamErr
		return
	}
	nb = int(size-1)/(h.c.NeedleMaxSize) + 1
	buf = h.s.Buffer(nb)
	ns = h.s.Needle(nn)
	for i, fh = range fhs {
		if key, err = strconv.ParseInt(keys[i], 10, 64); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", keys[i], err)
			err = errors.ErrParam
			break
		}
		if cookie, err = strconv.ParseInt(cookies[i], 10, 32); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", cookies[i], err)
			err = errors.ErrParam
			break
		}
		file, err = fh.Open()
		file.Close()
		if err != nil {
			log.Errorf("fh.Open() error(%v)", err)
			break
		}
		// check size
		if sr, ok = file.(sizer); ok {
			size = sr.Size()
		} else if fr, ok = file.(*os.File); ok {
			if fi, err = fr.Stat(); err != nil {
				break
			}
			size = fi.Size()
		}
		if size > int64(h.c.NeedleMaxSize) {
			err = errors.ErrNeedleTooLarge
			break
		}
		if rn, err = file.Read(buf[tn:]); err != nil {
			log.Errorf("file.Read() error(%v)", err)
			break
		}
		n = &(ns[i])
		n.Parse(key, int32(cookie), buf[tn:tn+rn])
		tn += rn
	}
//.........这里部分代码省略.........
开发者ID:wtmmac,项目名称:bfs,代码行数:101,代码来源:http_api.go


注:本文中的mime/multipart.File.Read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。