3

我知道有一些 Go 库可以创建像VFS这样的整个文件系统。但我只想把一个字节数组做成可以满足File接口的东西。

4

3 回答 3

7

标准库中没有现成的解决方案,但自己做并不难。

我们需要的是这个http.File接口:

type File interface {
        io.Closer
        io.Reader
        io.Seeker
        Readdir(count int) ([]os.FileInfo, error)
        Stat() (os.FileInfo, error)
}

请注意,我们可以利用bytes.Reader它来完成繁重的任务,因为它单独实现io.Readerio.Seeker. io.Closer可以是 noop,并且Readdir()可能会nil, nil在我们模拟文件而不是目录时返回,它Readdir()甚至不会被调用。

“最难”的部分是模拟Stat()返回一个实现的值os.FileInfo

这是一个简单的模拟FileInfo

type myFileInfo struct {
    name string
    data []byte
}

func (mif myFileInfo) Name() string       { return mif.name }
func (mif myFileInfo) Size() int64        { return int64(len(mif.data)) }
func (mif myFileInfo) Mode() os.FileMode  { return 0444 }        // Read for all
func (mif myFileInfo) ModTime() time.Time { return time.Time{} } // Return anything
func (mif myFileInfo) IsDir() bool        { return false }
func (mif myFileInfo) Sys() interface{}   { return nil }

有了它,我们就拥有了创建 mocked 的一切http.File

type MyFile struct {
    *bytes.Reader
    mif myFileInfo
}

func (mf *MyFile) Close() error { return nil } // Noop, nothing to do

func (mf *MyFile) Readdir(count int) ([]os.FileInfo, error) {
    return nil, nil // We are not a directory but a single file
}

func (mf *MyFile) Stat() (os.FileInfo, error) {
    return mf.mif, nil
}

使用它的示例(在Go Playground上尝试):

data := []byte{0, 1, 2, 3}

mf := &MyFile{
    Reader: bytes.NewReader(data),
    mif: myFileInfo{
        name: "somename.txt",
        data: data,
    },
}

var f http.File = mf
_ = f
于 2018-10-08T08:05:51.160 回答
0

看起来很简单,可以自己模拟一下。

type MockFile struct {
    data    []byte
    isOpen  bool
    offset  int64
}

type MockFileInfo struct {
    mockFile *MockFile
}
func (mfi *MockFileInfo) Name() string       { return "MockFile" }
func (mfi *MockFileInfo) Size() int64        { return len(mfi.data) }
func (mfi *MockFileInfo) Mode() os.FileMode  { return os.ModeIrregular }
func (mfi *MockFileInfo) ModTime() time.Time { return time.Now() }
func (mfi *MockFileInfo) IsDir() bool        { return false }
func (mfi *MockFileInfo) Sys() interface     { return nil }

func (mf *MockFile) Read(p []byte) (n int, err error) {
    if mf.isOpen {
        n = copy(p, mf.data[mf.offset:])
        mf.offset += n
    } else {
        err = errors.New("Cannot read from closed MockFile")
    }
    return
}

func (mf *MockFile) Close() error {
    if !mf.isOpen {
        return errors.New("Cannot close an already closed MockFile")
    mf.isOpen = false
    return nil
}

func (mf *MockFile) Seek(offset int64, whence int) (ret int64, err error) {
    var relativeTo int64
    switch whence {
        case 0:
            relativeTo = 0
        case 1:
            relativeTo = mf.offset
        case 2:
            relativeTo = len(mf.data)
    }
    ret := relativeTo + offset
    if ret < 0 || ret > len(mf.data) {
        return -1, errors.New("New offset would fall outside of the MockFile")
    }
    mf.offset = ret
    return
}

func (mf *MockFile) Readdir(count int) ([]os.FileInfo, error) {
    if count <= 0 {
        return []os.FileInfo{}, nil
    }
    return []os.FileInfo{}, errors.New("MockFiles have no associated directory")
}

func (mf *MockFile) Stat() (os.FileInfo, error) {
    return MockFileInfo{mf}
}

func OpenMockFile(data []byte) *MockFile {
    mf := MockFile{data, true, 0}
}
于 2018-10-08T07:56:21.080 回答
0

如果您正在寻找 gzip http2 push 的答案,您可以使用以下选项:

options := &http.PushOptions{
                Header: http.Header{
                    "Accept-Encoding": r.Header["Accept-Encoding"],
                },
            }
            if err := pusher.Push(filePush, options); err != nil {
                log.Printf("Failed to push: %v", err)
                return
            }
于 2020-04-20T11:29:34.183 回答