agi.share.go 4.1 KB

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