pkger/pkging/stdos/file.go

74 lines
2.2 KiB
Go
Raw Normal View History

2019-09-03 18:29:28 +03:00
package stdos
2019-09-01 00:00:24 +03:00
import (
"net/http"
"os"
2019-09-05 19:22:35 +03:00
"path"
2019-09-01 00:00:24 +03:00
"github.com/markbates/pkger/here"
2019-09-02 01:02:45 +03:00
"github.com/markbates/pkger/pkging"
2019-09-01 00:00:24 +03:00
)
2019-09-02 01:02:45 +03:00
var _ pkging.File = &File{}
2019-09-01 00:00:24 +03:00
type File struct {
*os.File
2019-09-02 01:02:45 +03:00
info *pkging.FileInfo
her here.Info
2019-10-09 20:21:54 +03:00
path here.Path
2019-09-03 18:29:28 +03:00
pkging pkging.Pkger
2019-09-01 00:00:24 +03:00
}
2019-10-16 23:09:17 +03:00
// Close closes the File, rendering it unusable for I/O.
2019-09-01 00:00:24 +03:00
func (f *File) Close() error {
return f.File.Close()
}
2019-10-16 23:09:17 +03:00
// Info returns the here.Info of the file
2019-09-01 00:00:24 +03:00
func (f *File) Info() here.Info {
return f.her
}
2019-10-24 23:22:15 +03:00
// Name retuns the name of the file in pkger format
func (f File) Name() string {
return f.path.String()
2019-09-01 00:00:24 +03:00
}
2019-10-16 23:09:17 +03:00
// Readdir reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by Lstat, in directory order. Subsequent calls on the same file will yield further FileInfos.
//
// If n > 0, Readdir returns at most n FileInfo structures. In this case, if Readdir returns an empty slice, it will return a non-nil error explaining why. At the end of a directory, the error is io.EOF.
//
// If n <= 0, Readdir returns all the FileInfo from the directory in a single slice. In this case, if Readdir succeeds (reads all the way to the end of the directory), it returns the slice and a nil error. If it encounters an error before the end of the directory, Readdir returns the FileInfo read until that point and a non-nil error.
2019-09-22 19:16:17 +03:00
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
2019-10-24 23:22:15 +03:00
return f.File.Readdir(count)
2019-09-05 19:22:35 +03:00
}
2019-10-16 23:09:17 +03:00
// Open implements the http.FileSystem interface. A FileSystem implements access to a collection of named files. The elements in a file path are separated by slash ('/', U+002F) characters, regardless of host operating system convention.
2019-09-01 00:00:24 +03:00
func (f *File) Open(name string) (http.File, error) {
2019-09-05 19:22:35 +03:00
fp := path.Join(f.Path().Name, name)
f2, err := f.pkging.Open(fp)
if err != nil {
return nil, err
}
2019-09-22 19:16:17 +03:00
return f2, nil
2019-09-01 00:00:24 +03:00
}
2019-10-16 23:09:17 +03:00
// Path returns the here.Path of the file
2019-10-09 20:21:54 +03:00
func (f *File) Path() here.Path {
2019-09-01 00:00:24 +03:00
return f.path
}
2019-10-16 23:09:17 +03:00
// Stat returns the FileInfo structure describing file. If there is an error, it will be of type *PathError.
2019-09-01 00:00:24 +03:00
func (f *File) Stat() (os.FileInfo, error) {
2019-09-01 23:18:39 +03:00
if f.info != nil {
return f.info, nil
}
2019-10-15 23:40:45 +03:00
info, err := f.File.Stat()
2019-09-01 23:18:39 +03:00
if err != nil {
return nil, err
2019-09-01 00:00:24 +03:00
}
2019-10-24 23:22:15 +03:00
f.info = pkging.NewFileInfo(info)
2019-09-20 17:56:26 +03:00
return f.info, nil
2019-09-01 00:00:24 +03:00
}