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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "io/ioutil"
  12. "mime"
  13. "net/http"
  14. "os"
  15. "path/filepath"
  16. "time"
  17. "code.gitea.io/gitea/modules/log"
  18. )
  19. func fileSystem(dir string) http.FileSystem {
  20. return Assets
  21. }
  22. func Asset(name string) ([]byte, error) {
  23. f, err := Assets.Open("/" + name)
  24. if err != nil {
  25. return nil, err
  26. }
  27. defer f.Close()
  28. return ioutil.ReadAll(f)
  29. }
  30. func AssetNames() []string {
  31. realFS := Assets.(vfsgen۰FS)
  32. var results = make([]string, 0, len(realFS))
  33. for k := range realFS {
  34. results = append(results, k[1:])
  35. }
  36. return results
  37. }
  38. func AssetIsDir(name string) (bool, error) {
  39. if f, err := Assets.Open("/" + name); err != nil {
  40. return false, err
  41. } else {
  42. defer f.Close()
  43. if fi, err := f.Stat(); err != nil {
  44. return false, err
  45. } else {
  46. return fi.IsDir(), nil
  47. }
  48. }
  49. }
  50. // serveContent serve http content
  51. func serveContent(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) {
  52. encodings := parseAcceptEncoding(req.Header.Get("Accept-Encoding"))
  53. if encodings["gzip"] {
  54. if cf, ok := fi.(*vfsgen۰CompressedFileInfo); ok {
  55. rd := bytes.NewReader(cf.GzipBytes())
  56. w.Header().Set("Content-Encoding", "gzip")
  57. ctype := mime.TypeByExtension(filepath.Ext(fi.Name()))
  58. if ctype == "" {
  59. // read a chunk to decide between utf-8 text and binary
  60. var buf [512]byte
  61. grd, _ := gzip.NewReader(rd)
  62. n, _ := io.ReadFull(grd, buf[:])
  63. ctype = http.DetectContentType(buf[:n])
  64. _, err := rd.Seek(0, io.SeekStart) // rewind to output whole file
  65. if err != nil {
  66. log.Error("rd.Seek error: %v", err)
  67. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  68. return
  69. }
  70. }
  71. w.Header().Set("Content-Type", ctype)
  72. http.ServeContent(w, req, fi.Name(), modtime, rd)
  73. return
  74. }
  75. }
  76. http.ServeContent(w, req, fi.Name(), modtime, content)
  77. return
  78. }