github.com/wangyougui/gf/v2@v2.6.5/os/gres/gres_http_file.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gres 8 9 import ( 10 "bytes" 11 "os" 12 13 "github.com/wangyougui/gf/v2/errors/gerror" 14 ) 15 16 // Close implements interface of http.File. 17 func (f *File) Close() error { 18 return nil 19 } 20 21 // Readdir implements Readdir interface of http.File. 22 func (f *File) Readdir(count int) ([]os.FileInfo, error) { 23 files := f.resource.ScanDir(f.Name(), "*", false) 24 if len(files) > 0 { 25 if count <= 0 || count > len(files) { 26 count = len(files) 27 } 28 infos := make([]os.FileInfo, count) 29 for k, v := range files { 30 infos[k] = v.FileInfo() 31 } 32 return infos, nil 33 } 34 return nil, nil 35 } 36 37 // Stat implements Stat interface of http.File. 38 func (f *File) Stat() (os.FileInfo, error) { 39 return f.FileInfo(), nil 40 } 41 42 // Read implements the io.Reader interface. 43 func (f *File) Read(b []byte) (n int, err error) { 44 reader, err := f.getReader() 45 if err != nil { 46 return 0, err 47 } 48 if n, err = reader.Read(b); err != nil { 49 err = gerror.Wrapf(err, `read content failed`) 50 } 51 return 52 } 53 54 // Seek implements the io.Seeker interface. 55 func (f *File) Seek(offset int64, whence int) (n int64, err error) { 56 reader, err := f.getReader() 57 if err != nil { 58 return 0, err 59 } 60 if n, err = reader.Seek(offset, whence); err != nil { 61 err = gerror.Wrapf(err, `seek failed for offset %d, whence %d`, offset, whence) 62 } 63 return 64 } 65 66 func (f *File) getReader() (*bytes.Reader, error) { 67 if f.reader == nil { 68 f.reader = bytes.NewReader(f.Content()) 69 } 70 return f.reader, nil 71 }