listPosts.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. ListPost.js
  3. Author: tobychui
  4. This script list all the post within the system
  5. Require paramter:
  6. webroot //The web deploy root of this user
  7. */
  8. //Require the file library
  9. requirelib("filelib");
  10. //Require the internal library for handling the path of blog posting
  11. includes("config.js");
  12. includes("helper.js");
  13. var blogPostStorage = getBlogPostStore();
  14. function main(){
  15. //Filter out the last / if exists
  16. blogPostStorage = filepathClean(blogPostStorage);
  17. filelib.mkdir(blogPostStorage + "/posts/");
  18. //List all the created post
  19. var posts = filelib.aglob(blogPostStorage + "/posts/*.json","reverse");
  20. var renderedPostList = [];
  21. for(var i = 0; i < posts.length; i++){
  22. //Extract post information
  23. var postInfo = extractPostInfo( posts[i]);
  24. //Get the poost modification time
  25. var modTime = filelib.mtime(posts[i], false);
  26. //Check if this post is published
  27. webroot = filepathClean(webroot);
  28. var postView = "private";
  29. var postfilename = posts[i].split("/").pop().replace(".json", ".html");
  30. if (filelib.fileExists(webroot + "/blog/posts/" + postfilename)){
  31. postView = "public";
  32. }
  33. renderedPostList.push({
  34. "title": postInfo[0],
  35. "content":postInfo[1],
  36. "tags": postInfo[2],
  37. "modtime": modTime,
  38. "view": postView,
  39. "filepath": posts[i]
  40. });
  41. }
  42. //Return the value of the posts
  43. sendJSONResp(JSON.stringify(renderedPostList));
  44. }
  45. main();