You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

static.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. //go:build bindata
  5. // +build bindata
  6. package public
  7. import (
  8. "bytes"
  9. "compress/gzip"
  10. "io"
  11. "mime"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. "time"
  16. "code.gitea.io/gitea/modules/log"
  17. )
  18. func fileSystem(dir string) http.FileSystem {
  19. return Assets
  20. }
  21. func Asset(name string) ([]byte, error) {
  22. f, err := Assets.Open("/" + name)
  23. if err != nil {
  24. return nil, err
  25. }
  26. defer f.Close()
  27. return io.ReadAll(f)
  28. }
  29. func AssetNames() []string {
  30. realFS := Assets.(vfsgen۰FS)
  31. var results = make([]string, 0, len(realFS))
  32. for k := range realFS {
  33. results = append(results, k[1:])
  34. }
  35. return results
  36. }
  37. func AssetIsDir(name string) (bool, error) {
  38. if f, err := Assets.Open("/" + name); err != nil {
  39. return false, err
  40. } else {
  41. defer f.Close()
  42. if fi, err := f.Stat(); err != nil {
  43. return false, err
  44. } else {
  45. return fi.IsDir(), nil
  46. }
  47. }
  48. }
  49. // serveContent serve http content
  50. func serveContent(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) {
  51. encodings := parseAcceptEncoding(req.Header.Get("Accept-Encoding"))
  52. if encodings["gzip"] {
  53. if cf, ok := fi.(*vfsgen۰CompressedFileInfo); ok {
  54. rd := bytes.NewReader(cf.GzipBytes())
  55. w.Header().Set("Content-Encoding", "gzip")
  56. ctype := mime.TypeByExtension(filepath.Ext(fi.Name()))
  57. if ctype == "" {
  58. // read a chunk to decide between utf-8 text and binary
  59. var buf [512]byte
  60. grd, _ := gzip.NewReader(rd)
  61. n, _ := io.ReadFull(grd, buf[:])
  62. ctype = http.DetectContentType(buf[:n])
  63. _, err := rd.Seek(0, io.SeekStart) // rewind to output whole file
  64. if err != nil {
  65. log.Error("rd.Seek error: %v", err)
  66. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  67. return
  68. }
  69. }
  70. w.Header().Set("Content-Type", ctype)
  71. http.ServeContent(w, req, fi.Name(), modtime, rd)
  72. return
  73. }
  74. }
  75. http.ServeContent(w, req, fi.Name(), modtime, content)
  76. return
  77. }