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

static.go 1.7 kB

3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "fmt"
  8. "io/ioutil"
  9. "gitea.com/macaron/macaron"
  10. )
  11. // Static implements the macaron static handler for serving assets.
  12. func Static(opts *Options) macaron.Handler {
  13. opts.FileSystem = Assets
  14. // we don't need to pass the directory, because the directory var is only
  15. // used when in the options there is no FileSystem.
  16. return opts.staticHandler("")
  17. }
  18. func Dir(name string) ([]string, error) {
  19. files, err := AssetDir(name)
  20. if err != nil {
  21. return []string{}, fmt.Errorf("Failed to read embedded directory. %v", err)
  22. }
  23. return files, nil
  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 AssetDir(dirName string) ([]string, error) {
  34. d, err := Assets.Open(dirName)
  35. if err != nil {
  36. return nil, err
  37. }
  38. defer d.Close()
  39. files, err := d.Readdir(-1)
  40. if err != nil {
  41. return nil, err
  42. }
  43. var results = make([]string, 0, len(files))
  44. for _, file := range files {
  45. results = append(results, file.Name())
  46. }
  47. return results, nil
  48. }
  49. func AssetNames() []string {
  50. realFS := Assets.(vfsgen۰FS)
  51. var results = make([]string, 0, len(realFS))
  52. for k := range realFS {
  53. results = append(results, k[1:])
  54. }
  55. return results
  56. }
  57. func AssetIsDir(name string) (bool, error) {
  58. if f, err := Assets.Open("/" + name); err != nil {
  59. return false, err
  60. } else {
  61. defer f.Close()
  62. if fi, err := f.Stat(); err != nil {
  63. return false, err
  64. } else {
  65. return fi.IsDir(), nil
  66. }
  67. }
  68. }