agi.share.go 3.8 KB

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