github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/swagger-files/ab0x.go (about)

     1  // Code generated by fileb0x at "2022-06-10 22:55:21.84892167 +0300 EEST m=+0.038018428" from config file "b0x.yaml" DO NOT EDIT.
     2  // modification hash(c06e09df5514d5bdf3c145144658221e.84893f7d7f6af7d7916db9fe20160151)
     3  
     4  package swaggerFiles
     5  
     6  import (
     7  	"bytes"
     8  
     9  	"context"
    10  	"io"
    11  	"github.com/hellobchain/newcryptosm/http"
    12  	"os"
    13  	"path"
    14  
    15  	"github.com/hellobchain/third_party/gin-swagger/webdav"
    16  )
    17  
    18  var (
    19  	// CTX is a context for webdav vfs
    20  	CTX = context.Background()
    21  
    22  	// FS is a virtual memory file system
    23  	FS = webdav.NewMemFS()
    24  
    25  	// Handler is used to server files through a http handler
    26  	Handler *webdav.Handler
    27  
    28  	// HTTP is the http file system
    29  	HTTP http.FileSystem = new(HTTPFS)
    30  )
    31  
    32  // HTTPFS implements http.FileSystem
    33  type HTTPFS struct {
    34  	// Prefix allows to limit the path of all requests. F.e. a prefix "css" would allow only calls to /css/*
    35  	Prefix string
    36  }
    37  
    38  func init() {
    39  	err := CTX.Err()
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	Handler = &webdav.Handler{
    45  		FileSystem: FS,
    46  		LockSystem: webdav.NewMemLS(),
    47  	}
    48  
    49  }
    50  
    51  // Open a file
    52  func (hfs *HTTPFS) Open(path string) (http.File, error) {
    53  	path = hfs.Prefix + path
    54  
    55  	f, err := FS.OpenFile(CTX, path, os.O_RDONLY, 0644)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return f, nil
    61  }
    62  
    63  // ReadFile is adapTed from ioutil
    64  func ReadFile(path string) ([]byte, error) {
    65  	f, err := FS.OpenFile(CTX, path, os.O_RDONLY, 0644)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	buf := bytes.NewBuffer(make([]byte, 0, bytes.MinRead))
    71  
    72  	// If the buffer overflows, we will get bytes.ErrTooLarge.
    73  	// Return that as an error. Any other panic remains.
    74  	defer func() {
    75  		e := recover()
    76  		if e == nil {
    77  			return
    78  		}
    79  		if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge {
    80  			err = panicErr
    81  		} else {
    82  			panic(e)
    83  		}
    84  	}()
    85  	_, err = buf.ReadFrom(f)
    86  	return buf.Bytes(), err
    87  }
    88  
    89  // WriteFile is adapTed from ioutil
    90  func WriteFile(filename string, data []byte, perm os.FileMode) error {
    91  	f, err := FS.OpenFile(CTX, filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	n, err := f.Write(data)
    96  	if err == nil && n < len(data) {
    97  		err = io.ErrShortWrite
    98  	}
    99  	if err1 := f.Close(); err == nil {
   100  		err = err1
   101  	}
   102  	return err
   103  }
   104  
   105  // WalkDirs looks for files in the given dir and returns a list of files in it
   106  // usage for all files in the b0x: WalkDirs("", false)
   107  func WalkDirs(name string, includeDirsInList bool, files ...string) ([]string, error) {
   108  	f, err := FS.OpenFile(CTX, name, os.O_RDONLY, 0)
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	fileInfos, err := f.Readdir(0)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	err = f.Close()
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	for _, info := range fileInfos {
   124  		filename := path.Join(name, info.Name())
   125  
   126  		if includeDirsInList || !info.IsDir() {
   127  			files = append(files, filename)
   128  		}
   129  
   130  		if info.IsDir() {
   131  			files, err = WalkDirs(filename, includeDirsInList, files...)
   132  			if err != nil {
   133  				return nil, err
   134  			}
   135  		}
   136  	}
   137  
   138  	return files, nil
   139  }