agi.share.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. //Resolve the file path
  28. rpath, err := u.VirtualPathToRealPath(vpath)
  29. if err != nil {
  30. return otto.New().MakeCustomError("Path resolve failed", "Unable to resolve virtual path: "+err.Error())
  31. }
  32. //Check if file exists
  33. if !fileExists(rpath) {
  34. return otto.New().MakeCustomError("File not exists", "Share file vpath not exists")
  35. }
  36. //Create a share object for this request
  37. shareID, err := g.Option.ShareManager.CreateNewShare(u, vpath)
  38. if err != nil {
  39. log.Println("[AGI] Create Share Failed: " + err.Error())
  40. return otto.New().MakeCustomError("Share failed", err.Error())
  41. }
  42. if timeout > 0 {
  43. go func(timeout int) {
  44. time.Sleep(time.Duration(timeout) * time.Second)
  45. g.Option.ShareManager.RemoveShareByUUID(shareID.UUID)
  46. log.Println("[AGI] Share auto-removed: " + shareID.UUID)
  47. }(int(timeout))
  48. }
  49. r, _ := otto.ToValue(shareID.UUID)
  50. return r
  51. })
  52. vm.Set("_share_removeShare", func(call otto.FunctionCall) otto.Value {
  53. shareUUID, err := call.Argument(0).ToString()
  54. if err != nil {
  55. return otto.New().MakeCustomError("Failed to remove share", "No share UUID given")
  56. }
  57. err = g.Option.ShareManager.RemoveShareByUUID(shareUUID)
  58. if err != nil {
  59. log.Println("[AGI] Share remove failed: " + err.Error())
  60. return otto.New().MakeCustomError("Failed to remove share", err.Error())
  61. }
  62. return otto.TrueValue()
  63. })
  64. vm.Set("_share_getShareUUID", func(call otto.FunctionCall) otto.Value {
  65. vpath, err := call.Argument(0).ToString()
  66. if err != nil {
  67. log.Println("[AGI] Failed to get share UUID: filepath not given")
  68. return otto.NullValue()
  69. }
  70. //Resolve the vpath to realpath
  71. rpath, err := u.VirtualPathToRealPath(vpath)
  72. if err != nil {
  73. log.Println("[AGI] Failed to get share UUID: Unabel to resovle")
  74. return otto.NullValue()
  75. }
  76. shareObject := g.Option.ShareManager.GetShareObjectFromRealPath(rpath)
  77. if shareObject == nil {
  78. log.Println("[AGI] Failed to get share UUID: File not shared")
  79. return otto.NullValue()
  80. }
  81. shareUUID := shareObject.UUID
  82. val, _ := otto.ToValue(shareUUID)
  83. return val
  84. })
  85. vm.Set("_share_checkShareExists", func(call otto.FunctionCall) otto.Value {
  86. shareUUID, err := call.Argument(0).ToString()
  87. if err != nil {
  88. return otto.New().MakeCustomError("Failed to check share exists", "No share UUID given")
  89. }
  90. shareObject := g.Option.ShareManager.GetShareObjectFromUUID(shareUUID)
  91. r, _ := otto.ToValue(!(shareObject == nil))
  92. return r
  93. })
  94. vm.Set("_share_fileIsShared", func(call otto.FunctionCall) otto.Value {
  95. vpath, err := call.Argument(0).ToString()
  96. if err != nil {
  97. return otto.New().MakeCustomError("Failed to check share exists", "No filepath given")
  98. }
  99. rpath, err := u.VirtualPathToRealPath(vpath)
  100. if err != nil {
  101. return otto.New().MakeCustomError("Filepath resolve failed", err.Error())
  102. }
  103. isShared := g.Option.ShareManager.FileIsShared(rpath)
  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. `)
  116. }