ao_backend.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. ao_backend.js
  3. Author: tobychui
  4. JavaScript wrapper for AGI Gateway script
  5. Designed for front-end WebApps
  6. Usage:
  7. 1. Copy and paste this file into your module's root
  8. 2. In your html, include this script in <script> element
  9. 3. Start the script using aoBackend.start("MyWebApp/ao_backend.js", "../");
  10. */
  11. if (typeof(BUILD_VERSION) != "undefined"){
  12. //Executing in backend VM
  13. if (typeof(opr) != "undefined"){
  14. /*
  15. Appdata Library
  16. */
  17. if (opr == "appdata.readFile"){
  18. requirelib("appdata");
  19. var content = appdata.readFile(filepath);
  20. if (content == false){
  21. sendJSONResp(JSON.stringify({
  22. error: "Unable to get appdata from app folder"
  23. }));
  24. }else{
  25. sendResp(content)
  26. }
  27. }else if (opr == "appdata.listDir"){
  28. requirelib("appdata");
  29. var content = appdata.listDir(filepath);
  30. if (content == false){
  31. sendJSONResp(JSON.stringify({
  32. error: "Unable to list backend appdata"
  33. }));
  34. }else{
  35. sendJSONResp(content);
  36. }
  37. /*
  38. File library
  39. */
  40. }else if (opr == "file.writeFile"){
  41. requirelib("filelib");
  42. filelib.writeFile(filepath,content);
  43. sendOK();
  44. }else if (opr == "file.readFile"){
  45. requirelib("filelib");
  46. sendResp(filelib.readFile(filepath));
  47. }else if (opr == "file.deleteFile"){
  48. requirelib("filelib");
  49. filelib.deleteFile(filepath);
  50. sendOK();
  51. }else if (opr == "file.readdir"){
  52. requirelib("filelib");
  53. var dirlist = filelib.readdir(filepath);
  54. sendJSONResp(JSON.stringify(dirlist));
  55. }else if (opr == "file.walk"){
  56. requirelib("filelib");
  57. var filelist = filelib.walk(filepath, mode);
  58. sendJSONResp(JSON.stringify(filelist));
  59. }else if (opr == "file.glob"){
  60. requirelib("filelib");
  61. var filelist = filelib.glob(wildcard, sort);
  62. sendJSONResp(JSON.stringify(filelist));
  63. }else if (opr == "file.aglob"){
  64. requirelib("filelib");
  65. var filelist = filelib.aglob(wildcard, sort);
  66. sendJSONResp(JSON.stringify(filelist));
  67. }else if (opr == "file.filesize"){
  68. requirelib("filelib");
  69. var fileSize = filelib.filesize(filepath);
  70. sendJSONResp(JSON.stringify(fileSize));
  71. }else if (opr == "file.fileExists"){
  72. requirelib("filelib");
  73. var exists = filelib.fileExists(filepath);
  74. sendJSONResp(JSON.stringify(exists));
  75. }else if (opr == "file.isDir"){
  76. requirelib("filelib");
  77. sendJSONResp(JSON.stringify(filelib.isDir(filepath)));
  78. }else if (opr == "file.mkdir"){
  79. requirelib("filelib");
  80. filelib.mkdir(filepath);
  81. sendOK();
  82. }else if (opr == "file.mtime"){
  83. requirelib("filelib");
  84. var unixmtime = filelib.mtime(filepath, true);
  85. sendJSONResp(JSON.stringify(unixmtime));
  86. }else if (opr == "file.rootName"){
  87. requirelib("filelib");
  88. var rootname = filelib.rootName(filepath);
  89. sendJSONResp(JSON.stringify(rootname));
  90. /*
  91. HTTP library
  92. */
  93. }else if (opr == "http.get"){
  94. requirelib("http");
  95. var respbody = http.get(targetURL);
  96. sendResp(respbody);
  97. }else if (opr == "http.post"){
  98. requirelib("http");
  99. if (postdata == ""){
  100. postdata = {};
  101. }
  102. var respbody = http.post(targetURL,postdata);
  103. sendResp(respbody);
  104. }else if (opr == "http.head"){
  105. requirelib("http");
  106. var respHeader = JSON.parse(http.head(targetURL, header));
  107. sendJSONResp(JSON.stringify(respHeader));
  108. }else if (opr == "http.download"){
  109. requirelib("http");
  110. var success = http.download(targetURL,saveDir,saveFilename);
  111. if (success){
  112. sendOk();
  113. }else{
  114. sendJSONResp(JSON.stringify({
  115. error: "Download failed"
  116. }));
  117. }
  118. }else{
  119. sendJSONResp(JSON.stringify({
  120. error: "Unknown operator: " + opr
  121. }));
  122. }
  123. }else{
  124. //Invalid request operation
  125. sendJSONResp(JSON.stringify({
  126. "error":"invalid or not supported operation given"
  127. }));
  128. }
  129. }else{
  130. console.log("INVALID USAGE")
  131. }