ab0x.go 2.8 KB

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