agi.file.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. package agi
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "sort"
  9. "strings"
  10. "github.com/robertkrimen/otto"
  11. fs "imuslab.com/arozos/mod/filesystem"
  12. user "imuslab.com/arozos/mod/user"
  13. )
  14. /*
  15. AJGI File Processing Library
  16. This is a library for handling image related functionalities in agi scripts.
  17. By Alanyueng 2020 <- This person write shitty code that need me to tidy up (by tobychui)
  18. Complete rewrite by tobychui in Sept 2020
  19. */
  20. func (g *Gateway) FileLibRegister() {
  21. err := g.RegisterLib("filelib", g.injectFileLibFunctions)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. }
  26. func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
  27. //Legacy File system API
  28. //writeFile(virtualFilepath, content) => return true/false when succeed / failed
  29. vm.Set("_filelib_writeFile", func(call otto.FunctionCall) otto.Value {
  30. vpath, err := call.Argument(0).ToString()
  31. if err != nil {
  32. g.raiseError(err)
  33. reply, _ := vm.ToValue(false)
  34. return reply
  35. }
  36. //Check for permission
  37. if !u.CanWrite(vpath) {
  38. panic(vm.MakeCustomError("PermissionDenied", "Path access denied: "+vpath))
  39. }
  40. content, err := call.Argument(1).ToString()
  41. if err != nil {
  42. g.raiseError(err)
  43. reply, _ := vm.ToValue(false)
  44. return reply
  45. }
  46. //Check if there is quota for the given length
  47. if !u.StorageQuota.HaveSpace(int64(len(content))) {
  48. //User have no remaining storage quota
  49. g.raiseError(errors.New("Storage Quota Fulled"))
  50. reply, _ := vm.ToValue(false)
  51. return reply
  52. }
  53. //Translate the virtual path to realpath
  54. rpath, err := virtualPathToRealPath(vpath, u)
  55. if err != nil {
  56. g.raiseError(err)
  57. reply, _ := vm.ToValue(false)
  58. return reply
  59. }
  60. //Check if file already exists.
  61. if fileExists(rpath) {
  62. //Check if this user own this file
  63. isOwner := u.IsOwnerOfFile(rpath)
  64. if isOwner {
  65. //This user own this system. Remove this file from his quota
  66. u.RemoveOwnershipFromFile(rpath)
  67. }
  68. }
  69. //Create and write to file using ioutil
  70. err = ioutil.WriteFile(rpath, []byte(content), 0755)
  71. if err != nil {
  72. g.raiseError(err)
  73. reply, _ := vm.ToValue(false)
  74. return reply
  75. }
  76. //Add the filesize to user quota
  77. u.SetOwnerOfFile(rpath)
  78. reply, _ := vm.ToValue(true)
  79. return reply
  80. })
  81. vm.Set("_filelib_deleteFile", func(call otto.FunctionCall) otto.Value {
  82. vpath, err := call.Argument(0).ToString()
  83. if err != nil {
  84. g.raiseError(err)
  85. reply, _ := vm.ToValue(false)
  86. return reply
  87. }
  88. //Check for permission
  89. if !u.CanWrite(vpath) {
  90. panic(vm.MakeCustomError("PermissionDenied", "Path access denied: "+vpath))
  91. }
  92. //Translate the virtual path to realpath
  93. rpath, err := virtualPathToRealPath(vpath, u)
  94. if err != nil {
  95. g.raiseError(err)
  96. reply, _ := vm.ToValue(false)
  97. return reply
  98. }
  99. //Check if file already exists.
  100. if fileExists(rpath) {
  101. //Check if this user own this file
  102. isOwner := u.IsOwnerOfFile(rpath)
  103. if isOwner {
  104. //This user own this system. Remove this file from his quota
  105. u.RemoveOwnershipFromFile(rpath)
  106. }
  107. } else {
  108. g.raiseError(errors.New("File not exists"))
  109. reply, _ := vm.ToValue(false)
  110. return reply
  111. }
  112. //Remove the file
  113. os.Remove(rpath)
  114. reply, _ := vm.ToValue(true)
  115. return reply
  116. })
  117. //readFile(virtualFilepath) => return content in string
  118. vm.Set("_filelib_readFile", func(call otto.FunctionCall) otto.Value {
  119. vpath, err := call.Argument(0).ToString()
  120. if err != nil {
  121. g.raiseError(err)
  122. reply, _ := vm.ToValue(false)
  123. return reply
  124. }
  125. //Check for permission
  126. if !u.CanRead(vpath) {
  127. panic(vm.MakeCustomError("PermissionDenied", "Path access denied: "+vpath))
  128. }
  129. //Translate the virtual path to realpath
  130. rpath, err := virtualPathToRealPath(vpath, u)
  131. if err != nil {
  132. g.raiseError(err)
  133. reply, _ := vm.ToValue(false)
  134. return reply
  135. }
  136. //Create and write to file using ioUtil
  137. content, err := ioutil.ReadFile(rpath)
  138. if err != nil {
  139. g.raiseError(err)
  140. reply, _ := vm.ToValue(false)
  141. return reply
  142. }
  143. reply, _ := vm.ToValue(string(content))
  144. return reply
  145. })
  146. //Listdir
  147. //readdir("user:/Desktop") => return filelist in array
  148. vm.Set("_filelib_readdir", func(call otto.FunctionCall) otto.Value {
  149. vpath, err := call.Argument(0).ToString()
  150. if err != nil {
  151. g.raiseError(err)
  152. reply, _ := vm.ToValue(false)
  153. return reply
  154. }
  155. //Translate the virtual path to realpath
  156. rpath, err := virtualPathToRealPath(vpath, u)
  157. if err != nil {
  158. g.raiseError(err)
  159. reply, _ := vm.ToValue(false)
  160. return reply
  161. }
  162. fileList, err := specialGlob(rpath)
  163. if err != nil {
  164. g.raiseError(err)
  165. reply, _ := vm.ToValue(false)
  166. return reply
  167. }
  168. //Translate all paths to virtual paths
  169. results := []string{}
  170. for _, file := range fileList {
  171. if IsDir(file) {
  172. thisRpath, _ := realpathToVirtualpath(file, u)
  173. results = append(results, thisRpath)
  174. }
  175. }
  176. reply, _ := vm.ToValue(results)
  177. return reply
  178. })
  179. //Usage
  180. //filelib.walk("user:/") => list everything recursively
  181. //filelib.walk("user:/", "folder") => list all folder recursively
  182. //filelib.walk("user:/", "file") => list all files recursively
  183. vm.Set("_filelib_walk", func(call otto.FunctionCall) otto.Value {
  184. vpath, err := call.Argument(0).ToString()
  185. if err != nil {
  186. g.raiseError(err)
  187. reply, _ := vm.ToValue(false)
  188. return reply
  189. }
  190. mode, err := call.Argument(1).ToString()
  191. if err != nil {
  192. mode = "all"
  193. }
  194. rpath, err := virtualPathToRealPath(vpath, u)
  195. if err != nil {
  196. g.raiseError(err)
  197. reply, _ := vm.ToValue(false)
  198. return reply
  199. }
  200. results := []string{}
  201. err = filepath.Walk(rpath, func(path string, info os.FileInfo, err error) error {
  202. thisVpath, err := realpathToVirtualpath(path, u)
  203. if mode == "file" {
  204. if !info.IsDir() {
  205. results = append(results, thisVpath)
  206. }
  207. } else if mode == "folder" {
  208. if info.IsDir() {
  209. results = append(results, thisVpath)
  210. }
  211. } else {
  212. results = append(results, thisVpath)
  213. }
  214. return nil
  215. })
  216. reply, _ := vm.ToValue(results)
  217. return reply
  218. })
  219. //Glob
  220. //glob("user:/Desktop/*.mp3") => return fileList in array
  221. //glob("/") => return a list of root directories
  222. vm.Set("_filelib_glob", func(call otto.FunctionCall) otto.Value {
  223. regex, err := call.Argument(0).ToString()
  224. if err != nil {
  225. g.raiseError(err)
  226. reply, _ := vm.ToValue(false)
  227. return reply
  228. }
  229. //Handle when regex = "." or "./" (listroot)
  230. if filepath.ToSlash(filepath.Clean(regex)) == "/" || filepath.Clean(regex) == "." {
  231. //List Root
  232. rootDirs := []string{}
  233. fileHandlers := u.GetAllFileSystemHandler()
  234. for _, fsh := range fileHandlers {
  235. rootDirs = append(rootDirs, fsh.UUID+":/")
  236. }
  237. reply, _ := vm.ToValue(rootDirs)
  238. return reply
  239. } else {
  240. //Check for permission
  241. if !u.CanRead(regex) {
  242. panic(vm.MakeCustomError("PermissionDenied", "Path access denied"))
  243. }
  244. //This function can only handle wildcard in filename but not in dir name
  245. vrootPath := filepath.Dir(regex)
  246. regexFilename := filepath.Base(regex)
  247. //Translate the virtual path to realpath
  248. rrootPath, err := virtualPathToRealPath(vrootPath, u)
  249. if err != nil {
  250. g.raiseError(err)
  251. reply, _ := vm.ToValue(false)
  252. return reply
  253. }
  254. suitableFiles, err := filepath.Glob(filepath.Join(rrootPath, regexFilename))
  255. if err != nil {
  256. g.raiseError(err)
  257. reply, _ := vm.ToValue(false)
  258. return reply
  259. }
  260. results := []string{}
  261. for _, file := range suitableFiles {
  262. thisRpath, _ := realpathToVirtualpath(filepath.ToSlash(file), u)
  263. results = append(results, thisRpath)
  264. }
  265. reply, _ := vm.ToValue(results)
  266. return reply
  267. }
  268. })
  269. //Advance Glob using file system special Glob, cannot use to scan root dirs
  270. vm.Set("_filelib_aglob", func(call otto.FunctionCall) otto.Value {
  271. regex, err := call.Argument(0).ToString()
  272. if err != nil {
  273. g.raiseError(err)
  274. reply, _ := vm.ToValue(false)
  275. return reply
  276. }
  277. sortMode, _ := call.Argument(1).ToString()
  278. if regex != "/" && !u.CanRead(regex) {
  279. panic(vm.MakeCustomError("PermissionDenied", "Path access denied"))
  280. }
  281. //This function can only handle wildcard in filename but not in dir name
  282. vrootPath := filepath.Dir(regex)
  283. regexFilename := filepath.Base(regex)
  284. //Translate the virtual path to realpath
  285. rrootPath, err := virtualPathToRealPath(vrootPath, u)
  286. if err != nil {
  287. g.raiseError(err)
  288. reply, _ := vm.ToValue(false)
  289. return reply
  290. }
  291. suitableFiles, err := specialGlob(filepath.Join(rrootPath, regexFilename))
  292. if err != nil {
  293. g.raiseError(err)
  294. reply, _ := vm.ToValue(false)
  295. return reply
  296. }
  297. type SortingFileData struct {
  298. Filename string
  299. Filepath string
  300. Filesize int64
  301. ModTime int64
  302. }
  303. parsedFilelist := []fs.FileData{}
  304. for _, file := range suitableFiles {
  305. vpath, err := realpathToVirtualpath(filepath.ToSlash(file), u)
  306. if err != nil {
  307. g.raiseError(err)
  308. reply, _ := vm.ToValue(false)
  309. return reply
  310. }
  311. modtime, _ := fs.GetModTime(file)
  312. parsedFilelist = append(parsedFilelist, fs.FileData{
  313. Filename: filepath.Base(file),
  314. Filepath: vpath,
  315. Filesize: fs.GetFileSize(file),
  316. ModTime: modtime,
  317. })
  318. }
  319. if sortMode != "" {
  320. if sortMode == "reverse" {
  321. //Sort by reverse name
  322. sort.Slice(parsedFilelist, func(i, j int) bool {
  323. return strings.ToLower(parsedFilelist[i].Filename) > strings.ToLower(parsedFilelist[j].Filename)
  324. })
  325. } else if sortMode == "smallToLarge" {
  326. sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].Filesize < parsedFilelist[j].Filesize })
  327. } else if sortMode == "largeToSmall" {
  328. sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].Filesize > parsedFilelist[j].Filesize })
  329. } else if sortMode == "mostRecent" {
  330. sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].ModTime > parsedFilelist[j].ModTime })
  331. } else if sortMode == "leastRecent" {
  332. sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].ModTime < parsedFilelist[j].ModTime })
  333. } else {
  334. sort.Slice(parsedFilelist, func(i, j int) bool {
  335. return strings.ToLower(parsedFilelist[i].Filename) < strings.ToLower(parsedFilelist[j].Filename)
  336. })
  337. }
  338. }
  339. //Parse the results (Only extract the filepath)
  340. results := []string{}
  341. for _, fileData := range parsedFilelist {
  342. results = append(results, fileData.Filepath)
  343. }
  344. reply, _ := vm.ToValue(results)
  345. return reply
  346. })
  347. //filesize("user:/Desktop/test.txt")
  348. vm.Set("_filelib_filesize", func(call otto.FunctionCall) otto.Value {
  349. vpath, err := call.Argument(0).ToString()
  350. if err != nil {
  351. g.raiseError(err)
  352. reply, _ := vm.ToValue(false)
  353. return reply
  354. }
  355. //Check for permission
  356. if !u.CanRead(vpath) {
  357. panic(vm.MakeCustomError("PermissionDenied", "Path access denied"))
  358. }
  359. //Translate the virtual path to realpath
  360. rpath, err := virtualPathToRealPath(vpath, u)
  361. if err != nil {
  362. g.raiseError(err)
  363. reply, _ := vm.ToValue(false)
  364. return reply
  365. }
  366. //Get filesize of file
  367. rawsize := fs.GetFileSize(rpath)
  368. if err != nil {
  369. g.raiseError(err)
  370. reply, _ := vm.ToValue(false)
  371. return reply
  372. }
  373. reply, _ := vm.ToValue(rawsize)
  374. return reply
  375. })
  376. //fileExists("user:/Desktop/test.txt") => return true / false
  377. vm.Set("_filelib_fileExists", func(call otto.FunctionCall) otto.Value {
  378. vpath, err := call.Argument(0).ToString()
  379. if err != nil {
  380. g.raiseError(err)
  381. reply, _ := vm.ToValue(false)
  382. return reply
  383. }
  384. //Check for permission
  385. if !u.CanRead(vpath) {
  386. panic(vm.MakeCustomError("PermissionDenied", "Path access denied"))
  387. }
  388. //Translate the virtual path to realpath
  389. rpath, err := virtualPathToRealPath(vpath, u)
  390. if err != nil {
  391. g.raiseError(err)
  392. reply, _ := vm.ToValue(false)
  393. return reply
  394. }
  395. if fileExists(rpath) {
  396. reply, _ := vm.ToValue(true)
  397. return reply
  398. } else {
  399. reply, _ := vm.ToValue(false)
  400. return reply
  401. }
  402. })
  403. //fileExists("user:/Desktop/test.txt") => return true / false
  404. vm.Set("_filelib_isDir", func(call otto.FunctionCall) otto.Value {
  405. vpath, err := call.Argument(0).ToString()
  406. if err != nil {
  407. g.raiseError(err)
  408. reply, _ := vm.ToValue(false)
  409. return reply
  410. }
  411. //Check for permission
  412. if !u.CanRead(vpath) {
  413. panic(vm.MakeCustomError("PermissionDenied", "Path access denied: "+vpath))
  414. }
  415. //Translate the virtual path to realpath
  416. rpath, err := virtualPathToRealPath(vpath, u)
  417. if err != nil {
  418. g.raiseError(err)
  419. reply, _ := vm.ToValue(false)
  420. return reply
  421. }
  422. if _, err := os.Stat(rpath); os.IsNotExist(err) {
  423. //File not exists
  424. panic(vm.MakeCustomError("File Not Exists", "Required path not exists"))
  425. }
  426. if IsDir(rpath) {
  427. reply, _ := vm.ToValue(true)
  428. return reply
  429. } else {
  430. reply, _ := vm.ToValue(false)
  431. return reply
  432. }
  433. })
  434. //Make directory command
  435. vm.Set("_filelib_mkdir", func(call otto.FunctionCall) otto.Value {
  436. vdir, err := call.Argument(0).ToString()
  437. if err != nil {
  438. return otto.FalseValue()
  439. }
  440. //Check for permission
  441. if !u.CanWrite(vdir) {
  442. panic(vm.MakeCustomError("PermissionDenied", "Path access denied"))
  443. }
  444. //Translate the path to realpath
  445. rdir, err := virtualPathToRealPath(vdir, u)
  446. if err != nil {
  447. log.Println(err.Error())
  448. return otto.FalseValue()
  449. }
  450. //Create the directory at rdir location
  451. err = os.MkdirAll(rdir, 0755)
  452. if err != nil {
  453. log.Println(err.Error())
  454. return otto.FalseValue()
  455. }
  456. return otto.TrueValue()
  457. })
  458. //Get MD5 of the given filepath
  459. vm.Set("_filelib_md5", func(call otto.FunctionCall) otto.Value {
  460. log.Println("Call to MD5 Functions!")
  461. return otto.FalseValue()
  462. })
  463. //Get the root name of the given virtual path root
  464. vm.Set("_filelib_rname", func(call otto.FunctionCall) otto.Value {
  465. //Get virtual path from the function input
  466. vpath, err := call.Argument(0).ToString()
  467. if err != nil {
  468. g.raiseError(err)
  469. return otto.FalseValue()
  470. }
  471. //Get fs handler from the vpath
  472. fsHandler, err := u.GetFileSystemHandlerFromVirtualPath(vpath)
  473. if err != nil {
  474. g.raiseError(err)
  475. return otto.FalseValue()
  476. }
  477. //Return the name of the fsHandler
  478. name, _ := vm.ToValue(fsHandler.Name)
  479. return name
  480. })
  481. vm.Set("_filelib_mtime", func(call otto.FunctionCall) otto.Value {
  482. vpath, err := call.Argument(0).ToString()
  483. if err != nil {
  484. g.raiseError(err)
  485. reply, _ := vm.ToValue(false)
  486. return reply
  487. }
  488. //Check for permission
  489. if !u.CanRead(vpath) {
  490. panic(vm.MakeCustomError("PermissionDenied", "Path access denied"))
  491. }
  492. parseToUnix, err := call.Argument(1).ToBoolean()
  493. if err != nil {
  494. parseToUnix = false
  495. }
  496. rpath, err := virtualPathToRealPath(vpath, u)
  497. if err != nil {
  498. log.Println(err.Error())
  499. return otto.FalseValue()
  500. }
  501. info, err := os.Stat(rpath)
  502. if err != nil {
  503. log.Println(err.Error())
  504. return otto.FalseValue()
  505. }
  506. modTime := info.ModTime()
  507. if parseToUnix {
  508. result, _ := otto.ToValue(modTime.Unix())
  509. return result
  510. } else {
  511. result, _ := otto.ToValue(modTime.Format("2006-01-02 15:04:05"))
  512. return result
  513. }
  514. })
  515. /*
  516. vm.Set("_filelib_decodeURI", func(call otto.FunctionCall) otto.Value {
  517. originalURI, err := call.Argument(0).ToString()
  518. if err != nil {
  519. g.raiseError(err)
  520. reply, _ := vm.ToValue(false)
  521. return reply
  522. }
  523. decodedURI := specialURIDecode(originalURI)
  524. result, err := otto.ToValue(decodedURI)
  525. if err != nil {
  526. g.raiseError(err)
  527. reply, _ := vm.ToValue(false)
  528. return reply
  529. }
  530. return result
  531. })
  532. */
  533. //Other file operations, wip
  534. //Wrap all the native code function into an imagelib class
  535. vm.Run(`
  536. var filelib = {};
  537. filelib.writeFile = _filelib_writeFile;
  538. filelib.readFile = _filelib_readFile;
  539. filelib.deleteFile = _filelib_deleteFile;
  540. filelib.readdir = _filelib_readdir;
  541. filelib.walk = _filelib_walk;
  542. filelib.glob = _filelib_glob;
  543. filelib.aglob = _filelib_aglob;
  544. filelib.filesize = _filelib_filesize;
  545. filelib.fileExists = _filelib_fileExists;
  546. filelib.isDir = _filelib_isDir;
  547. filelib.md5 = _filelib_md5;
  548. filelib.mkdir = _filelib_mkdir;
  549. filelib.mtime = _filelib_mtime;
  550. filelib.rname = _filelib_rname;
  551. `)
  552. }