gf/os/gres/gres_http_file.go

64 lines
1.4 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-08-14 00:13:42 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gres
import (
"bytes"
"os"
)
2019-08-14 22:03:52 +08:00
// Close implements Close interface of http.File.
func (f *File) Close() error {
return nil
2019-08-14 00:13:42 +08:00
}
2019-08-14 22:03:52 +08:00
// Readdir implements Readdir interface of http.File.
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
2019-08-31 18:04:12 +08:00
files := f.resource.ScanDir(f.Name(), "*", false)
2019-08-14 22:03:52 +08:00
if len(files) > 0 {
if count <= 0 || count > len(files) {
2019-08-14 22:03:52 +08:00
count = len(files)
}
infos := make([]os.FileInfo, count)
for k, v := range files {
infos[k] = v.FileInfo()
}
return infos, nil
2019-08-14 00:13:42 +08:00
}
2019-08-14 22:03:52 +08:00
return nil, nil
2019-08-14 00:13:42 +08:00
}
2019-08-14 22:03:52 +08:00
// Stat implements Stat interface of http.File.
func (f *File) Stat() (os.FileInfo, error) {
return f.FileInfo(), nil
2019-08-14 00:13:42 +08:00
}
// Read implements the io.Reader interface.
func (f *File) Read(b []byte) (n int, err error) {
reader, err := f.getReader()
if err != nil {
return 0, err
}
return reader.Read(b)
}
// Seek implements the io.Seeker interface.
func (f *File) Seek(offset int64, whence int) (int64, error) {
reader, err := f.getReader()
if err != nil {
return 0, err
}
return reader.Seek(offset, whence)
}
func (f *File) getReader() (*bytes.Reader, error) {
if f.reader == nil {
2019-08-16 00:29:14 +08:00
f.reader = bytes.NewReader(f.Content())
2019-08-14 00:13:42 +08:00
}
return f.reader, nil
}