agi.share.go 4.3 KB

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