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.

file_cache.go 1.6 kB

2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package controllers
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/astaxie/beego"
  6. "github.com/casbin/casibase/util"
  7. )
  8. var cacheDir string
  9. var appDir string
  10. var cacheMap = map[string]string{}
  11. func init() {
  12. cacheDir = beego.AppConfig.String("cacheDir")
  13. appDir = beego.AppConfig.String("appDir")
  14. }
  15. func getAppPath(filename string) string {
  16. return fmt.Sprintf("%s/%s.ctf", appDir, filename)
  17. }
  18. func getCachePrefix(filename string) string {
  19. if !strings.HasPrefix(filename, "ECG_") && !strings.HasPrefix(filename, "EEG_") && !strings.HasPrefix(filename, "Impedance_") {
  20. return ""
  21. }
  22. tokens := strings.SplitN(filename, "_", 2)
  23. res := tokens[0]
  24. return res
  25. }
  26. func addFileToCache(key string, filename string, bs []byte) {
  27. prefix := getCachePrefix(filename)
  28. if prefix == "" {
  29. return
  30. }
  31. path := fmt.Sprintf("%s/%s/%s", cacheDir, key, filename)
  32. util.EnsureFileFolderExists(path)
  33. util.WriteBytesToPath(bs, path)
  34. }
  35. func (c *ApiController) ActivateFile() {
  36. _, ok := c.RequireSignedIn()
  37. if !ok {
  38. return
  39. }
  40. key := c.Input().Get("key")
  41. filename := c.Input().Get("filename")
  42. prefix := getCachePrefix(filename)
  43. if prefix == "" {
  44. c.Data["json"] = false
  45. c.ServeJSON()
  46. return
  47. }
  48. path := fmt.Sprintf("%s/%s", cacheDir, key)
  49. cacheMap[prefix] = path
  50. fmt.Printf("%v\n", cacheMap)
  51. if !util.FileExist(getAppPath(filename)) {
  52. util.CopyFile(getAppPath(filename), getAppPath(prefix))
  53. }
  54. c.Data["json"] = true
  55. c.ServeJSON()
  56. }
  57. func (c *ApiController) GetActiveFile() {
  58. prefix := c.Input().Get("prefix")
  59. res := ""
  60. if v, ok := cacheMap[prefix]; ok {
  61. res = v
  62. }
  63. c.Data["json"] = res
  64. c.ServeJSON()
  65. }

基于Casbin的开源AI领域知识库平台

Contributors (1)