您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Golang io.copy在请求正文中两次

Golang io.copy在请求正文中两次

您不能直接执行此操作,但可以编写在io.Copy上执行哈希处理的包装器

// this works for either a reader or writer, 
//  but if you use both in the same time the hash will be wrong.
type Hasher struct {
    io.Writer
    io.Reader
    hash.Hash
    Size uint64
}

func (h *Hasher) Write(p []byte) (n int, err error) {
    n, err = h.Writer.Write(p)
    h.Hash.Write(p)
    h.Size += uint64(n)
    return
}

func (h *Hasher) Read(p []byte) (n int, err error) {
    n, err = h.Reader.Read(p)
    h.Hash.Write(p[:n]) //on error n is gonna be 0 so this is still safe.
    return
}

func (h *Hasher) Sum() string {
    return hex.EncodeToString(h.Hash.Sum(nil))
}

func (h *UploadHandle) Read() (io.Reader, string, int64, error) {
    var b bytes.Buffer

    hashedReader := &Hasher{Reader: h.Contents, Hash: sha1.New()}
    n, err := io.Copy(&b, hashedReader)

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

    return &b, hashedReader.Sum(), n, nil
}

//由于我完全忘记了io.TeeReader存在,因此根据@Dustin的评论更新了版本。

func (h *UploadHandle) Read() (io.Reader, string, int64, error) {
    var b bytes.Buffer

    hash := sha1.New()
    n, err := io.Copy(&b, io.TeeReader(h.Contents, hash))

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

    return &b, hex.EncodeToString(hash.Sum(nil)), n, nil
}
Go 2022/1/1 18:15:13 有499人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶