agi.share.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_checkShareExists", func(call otto.FunctionCall) otto.Value {
  65. shareUUID, err := call.Argument(0).ToString()
  66. if err != nil {
  67. return otto.New().MakeCustomError("Failed to check share exists", "No share UUID given")
  68. }
  69. shareObject := g.Option.ShareManager.GetShareObjectFromUUID(shareUUID)
  70. r, _ := otto.ToValue(!(shareObject == nil))
  71. return r
  72. })
  73. vm.Set("_share_fileIsShared", func(call otto.FunctionCall) otto.Value {
  74. vpath, err := call.Argument(0).ToString()
  75. if err != nil {
  76. return otto.New().MakeCustomError("Failed to check share exists", "No filepath given")
  77. }
  78. rpath, err := u.VirtualPathToRealPath(vpath)
  79. if err != nil {
  80. return otto.New().MakeCustomError("Filepath resolve failed", err.Error())
  81. }
  82. isShared := g.Option.ShareManager.FileIsShared(rpath)
  83. r, _ := otto.ToValue(isShared)
  84. return r
  85. })
  86. //Wrap all the native code function into an imagelib class
  87. vm.Run(`
  88. var share = {};
  89. share.shareFile = _share_file;
  90. share.removeShare = _share_removeShare;
  91. share.checkShareExists = _share_checkShareExists;
  92. share.fileIsShared = _share_fileIsShared;
  93. `)
  94. }