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.

dynamic.go 1.2 kB

3 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. "os"
  10. "path"
  11. "code.gitea.io/gitea/modules/setting"
  12. "gitea.com/macaron/macaron"
  13. "github.com/unknwon/com"
  14. )
  15. // Static implements the macaron static handler for serving assets.
  16. func Static(opts *Options) macaron.Handler {
  17. return opts.staticHandler(opts.Directory)
  18. }
  19. func Dir(name string) ([]string, error) {
  20. var (
  21. result []string
  22. )
  23. staticDir := path.Join(setting.StaticRootPath, "public", name)
  24. if com.IsDir(staticDir) {
  25. files, err := com.StatDir(staticDir, true)
  26. if err != nil {
  27. return []string{}, fmt.Errorf("Failed to read img directory. %v", err)
  28. }
  29. result = append(result, files...)
  30. }
  31. return result, nil
  32. }
  33. func Asset(name string) ([]byte, error) {
  34. staticPath := path.Join(setting.StaticRootPath, "public", name)
  35. if com.IsFile(staticPath) {
  36. f, err := os.Open(staticPath)
  37. defer f.Close()
  38. if err == nil {
  39. return ioutil.ReadAll(f)
  40. }
  41. }
  42. return nil, fmt.Errorf("Asset file does not exist: %s", name)
  43. }