agi.share.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package agi
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/robertkrimen/otto"
  7. "imuslab.com/arozos/mod/agi/static"
  8. "imuslab.com/arozos/mod/info/logger"
  9. )
  10. func (g *Gateway) ShareLibRegister() {
  11. err := g.RegisterLib("share", g.injectShareFunctions)
  12. if err != nil {
  13. logger.PrintAndLog("Agi", fmt.Sprint(err), nil)
  14. os.Exit(1)
  15. }
  16. }
  17. func (g *Gateway) injectShareFunctions(payload *static.AgiLibInjectionPayload) {
  18. vm := payload.VM
  19. u := payload.User
  20. //scriptFsh := payload.ScriptFsh
  21. //scriptPath := payload.ScriptPath
  22. //w := payload.Writer
  23. //r := payload.Request
  24. vm.Set("_share_file", func(call otto.FunctionCall) otto.Value {
  25. //Get the vpath of file to share
  26. vpath, err := call.Argument(0).ToString()
  27. if err != nil {
  28. return otto.New().MakeCustomError("Unable to decode filepath", "No given filepath for sharing")
  29. }
  30. //Get the timeout from the 2nd parameter for how long this share will exists
  31. timeout, err := call.Argument(1).ToInteger()
  32. if err != nil {
  33. //Not defined -> Do not expire
  34. timeout = 0
  35. }
  36. //Create a share object for this request
  37. vpathSourceFsh := u.GetRootFSHFromVpathInUserScope(vpath)
  38. shareID, err := g.Option.ShareManager.CreateNewShare(u, vpathSourceFsh, vpath)
  39. if err != nil {
  40. logger.PrintAndLog("Agi", "[AGI] Create Share Failed: "+err.Error(), nil)
  41. return otto.New().MakeCustomError("Share failed", err.Error())
  42. }
  43. if timeout > 0 {
  44. go func(timeout int) {
  45. time.Sleep(time.Duration(timeout) * time.Second)
  46. g.Option.ShareManager.RemoveShareByUUID(u, shareID.UUID)
  47. logger.PrintAndLog("Agi", "[AGI] Share auto-removed: "+shareID.UUID, nil)
  48. }(int(timeout))
  49. }
  50. r, _ := otto.ToValue(shareID.UUID)
  51. return r
  52. })
  53. vm.Set("_share_removeShare", func(call otto.FunctionCall) otto.Value {
  54. shareUUID, err := call.Argument(0).ToString()
  55. if err != nil {
  56. return otto.New().MakeCustomError("Failed to remove share", "No share UUID given")
  57. }
  58. err = g.Option.ShareManager.RemoveShareByUUID(u, shareUUID)
  59. if err != nil {
  60. logger.PrintAndLog("Agi", "[AGI] Share remove failed: "+err.Error(), nil)
  61. return otto.New().MakeCustomError("Failed to remove share", err.Error())
  62. }
  63. return otto.TrueValue()
  64. })
  65. vm.Set("_share_getShareUUID", func(call otto.FunctionCall) otto.Value {
  66. vpath, err := call.Argument(0).ToString()
  67. if err != nil {
  68. logger.PrintAndLog("Agi", "[AGI] Failed to get share UUID: filepath not given", nil)
  69. return otto.NullValue()
  70. }
  71. shareObject := g.Option.ShareManager.GetShareObjectFromUserAndVpath(u, vpath)
  72. if shareObject == nil {
  73. logger.PrintAndLog("Agi", "[AGI] Failed to get share UUID: File not shared", nil)
  74. return otto.NullValue()
  75. }
  76. shareUUID := shareObject.UUID
  77. val, _ := otto.ToValue(shareUUID)
  78. return val
  79. })
  80. vm.Set("_share_checkShareExists", func(call otto.FunctionCall) otto.Value {
  81. shareUUID, err := call.Argument(0).ToString()
  82. if err != nil {
  83. return otto.New().MakeCustomError("Failed to check share exists", "No share UUID given")
  84. }
  85. shareObject := g.Option.ShareManager.GetShareObjectFromUUID(shareUUID)
  86. r, _ := otto.ToValue(!(shareObject == nil))
  87. return r
  88. })
  89. vm.Set("_share_checkSharePermission", func(call otto.FunctionCall) otto.Value {
  90. shareUUID, err := call.Argument(0).ToString()
  91. if err != nil {
  92. return otto.New().MakeCustomError("Failed to check share permission", "No share UUID given")
  93. }
  94. shareObject := g.Option.ShareManager.GetShareObjectFromUUID(shareUUID)
  95. if shareObject == nil {
  96. return otto.NullValue()
  97. }
  98. r, _ := otto.ToValue(shareObject.Permission)
  99. return r
  100. })
  101. vm.Set("_share_fileIsShared", func(call otto.FunctionCall) otto.Value {
  102. vpath, err := call.Argument(0).ToString()
  103. if err != nil {
  104. return otto.New().MakeCustomError("Failed to check share exists", "No filepath given")
  105. }
  106. isShared := g.Option.ShareManager.FileIsShared(u, vpath)
  107. r, _ := otto.ToValue(isShared)
  108. return r
  109. })
  110. //Wrap all the native code function into an imagelib class
  111. vm.Run(`
  112. var share = {};
  113. share.shareFile = _share_file;
  114. share.removeShare = _share_removeShare;
  115. share.checkShareExists = _share_checkShareExists;
  116. share.fileIsShared = _share_fileIsShared;
  117. share.getFileShareUUID = _share_getShareUUID;
  118. share.checkSharePermission = _share_checkSharePermission;
  119. `)
  120. }