本文整理汇总了Golang中github.com/Terry-Mao/bfs/store/needle.Needle.WriteFrom方法的典型用法代码示例。如果您正苦于以下问题:Golang Needle.WriteFrom方法的具体用法?Golang Needle.WriteFrom怎么用?Golang Needle.WriteFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/Terry-Mao/bfs/store/needle.Needle
的用法示例。
在下文中一共展示了Needle.WriteFrom方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ServeHTTP
func (h httpUploadHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
var (
ok bool
vid int64
key int64
cookie int64
size int64
err error
str string
v *Volume
n *needle.Needle
file multipart.File
sr sizer
fr *os.File
fi os.FileInfo
uerr errors.Error
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) {
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
}
str = r.FormValue("key")
if key, err = strconv.ParseInt(str, 10, 64); err != nil {
log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
res["ret"] = errors.RetParamErr
return
}
str = r.FormValue("cookie")
if cookie, err = strconv.ParseInt(str, 10, 32); err != nil {
log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
res["ret"] = errors.RetParamErr
return
}
if file, _, err = r.FormFile("file"); err != nil {
res["ret"] = errors.RetInternalErr
return
}
defer file.Close()
if sr, ok = file.(sizer); ok {
size = sr.Size()
} else if fr, ok = file.(*os.File); ok {
if fi, err = fr.Stat(); err != nil {
res["ret"] = errors.RetInternalErr
return
}
size = fi.Size()
}
if size > int64(h.c.NeedleMaxSize) {
res["ret"] = errors.RetNeedleTooLarge
return
}
n = h.s.Needle()
n.InitSize(key, int32(cookie), int32(size))
if err = n.WriteFrom(file); err == nil {
if v = h.s.Volumes[int32(vid)]; v != nil {
err = v.Add(n)
} else {
err = errors.ErrVolumeNotExist
}
}
h.s.FreeNeedle(n)
if err != nil {
if uerr, ok = err.(errors.Error); ok {
res["ret"] = int(uerr)
} else {
res["ret"] = errors.RetInternalErr
}
}
return
}
示例2: ServeHTTP
func (h httpUploadHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
var (
vid int64
key int64
cookie int64
size int64
err error
str string
v *volume.Volume
n *needle.Needle
file multipart.File
res = map[string]interface{}{}
)
if r.Method != "POST" {
http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
return
}
defer HttpPostWriter(r, wr, time.Now(), &err, res)
// check total content-length
if size, err = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64); err != nil {
err = errors.ErrInternal
return
}
if size > int64(h.c.NeedleMaxSize) {
err = errors.ErrNeedleTooLarge
return
}
str = r.FormValue("vid")
if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
err = errors.ErrParam
return
}
str = r.FormValue("key")
if key, err = strconv.ParseInt(str, 10, 64); err != nil {
log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
err = errors.ErrParam
return
}
str = r.FormValue("cookie")
if cookie, err = strconv.ParseInt(str, 10, 32); err != nil {
log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
err = errors.ErrParam
return
}
if file, _, err = r.FormFile("file"); err != nil {
log.Errorf("r.FormFile() error(%v)", err)
err = errors.ErrInternal
return
}
defer file.Close()
if size, err = fileSize(file); err != nil {
log.Errorf("fileSize() error(%v)", err)
err = errors.ErrInternal
return
}
if size > int64(h.c.NeedleMaxSize) {
err = errors.ErrNeedleTooLarge
return
}
n = h.s.Needle()
if err = n.WriteFrom(key, int32(cookie), int32(size), file); err == nil {
if v = h.s.Volumes[int32(vid)]; v != nil {
err = v.Add(n)
} else {
err = errors.ErrVolumeNotExist
}
}
h.s.FreeNeedle(n)
return
}