agi.share.go 4.1 KB

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