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 2.2 kB

2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package controllers
  15. import (
  16. "fmt"
  17. "strings"
  18. "github.com/astaxie/beego"
  19. "github.com/casbin/casibase/util"
  20. )
  21. var (
  22. cacheDir string
  23. appDir string
  24. cacheMap = map[string]string{}
  25. )
  26. func init() {
  27. cacheDir = beego.AppConfig.String("cacheDir")
  28. appDir = beego.AppConfig.String("appDir")
  29. }
  30. func getAppPath(filename string) string {
  31. return fmt.Sprintf("%s/%s.ctf", appDir, filename)
  32. }
  33. func getCachePrefix(filename string) string {
  34. if !strings.HasPrefix(filename, "ECG_") && !strings.HasPrefix(filename, "EEG_") && !strings.HasPrefix(filename, "Impedance_") {
  35. return ""
  36. }
  37. tokens := strings.SplitN(filename, "_", 2)
  38. res := tokens[0]
  39. return res
  40. }
  41. func addFileToCache(key string, filename string, bs []byte) {
  42. prefix := getCachePrefix(filename)
  43. if prefix == "" {
  44. return
  45. }
  46. path := fmt.Sprintf("%s/%s/%s", cacheDir, key, filename)
  47. util.EnsureFileFolderExists(path)
  48. util.WriteBytesToPath(bs, path)
  49. }
  50. func (c *ApiController) ActivateFile() {
  51. _, ok := c.RequireSignedIn()
  52. if !ok {
  53. return
  54. }
  55. key := c.Input().Get("key")
  56. filename := c.Input().Get("filename")
  57. prefix := getCachePrefix(filename)
  58. if prefix == "" {
  59. c.ResponseOk(false)
  60. return
  61. }
  62. path := fmt.Sprintf("%s/%s", cacheDir, key)
  63. cacheMap[prefix] = path
  64. fmt.Printf("%v\n", cacheMap)
  65. if !util.FileExist(getAppPath(filename)) {
  66. util.CopyFile(getAppPath(filename), getAppPath(prefix))
  67. }
  68. c.ResponseOk(true)
  69. }
  70. func (c *ApiController) GetActiveFile() {
  71. prefix := c.Input().Get("prefix")
  72. res := ""
  73. if v, ok := cacheMap[prefix]; ok {
  74. res = v
  75. }
  76. c.ResponseOk(res)
  77. }