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.3KB

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