Desktop markdown wiki app. Built with node, Electron Framework and AngularJS.

file-service.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. angular.module('codexApp')
  2. .service('FileService', [ '$rootScope', '$http', function($rootScope, $http) {
  3. var notes_dir = "/Users/james/dev/codex/codex";
  4. var default_notes_dir = "/Users/james/dev/codex/codex/inbox";
  5. var notes = [];
  6. var current_note = "";
  7. var prettySize = function(bytes) {
  8. if (bytes <= 1024) {
  9. return bytes + " KB"
  10. } else {
  11. var megabytes = parseFloat(Math.round(bytes/1024 * 100) / 100).toFixed(0);
  12. return megabytes + " MB"
  13. }
  14. }
  15. var getFilePathExtension = function(path) {
  16. var filename = path.split('\\').pop().split('/').pop();
  17. var lastIndex = filename.lastIndexOf(".");
  18. if (lastIndex < 1) return "";
  19. return filename.substr(lastIndex + 1);
  20. }
  21. var getFileType = function(path) {
  22. var extension = getFilePathExtension(path);
  23. switch (extension) {
  24. case "pdf":
  25. return "Document";
  26. case "jpg":
  27. return "Image";
  28. case "png":
  29. return "Image";
  30. case "md":
  31. return "Markdown";
  32. default:
  33. return "File";
  34. }
  35. }
  36. var directorySize = function(dir) {
  37. var filesystem = require("fs");
  38. var size = 0;
  39. var results = [];
  40. filesystem.readdirSync(dir).forEach(function(file) {
  41. file_path = dir+'/'+file;
  42. var stat = filesystem.statSync(file_path);
  43. if (stat && stat.isDirectory()) {
  44. results = results.concat(directorySize(file_path))
  45. } else {
  46. if(file != ".DS_Store") {
  47. size = size + stat["size"];
  48. //console.log("* " + stat["size"] + " KB -> " + file_path)
  49. }
  50. }
  51. });
  52. return size;
  53. }
  54. var noteCount = function(){
  55. var count = $scope.notes.length
  56. switch (count) {
  57. case 0:
  58. return '0 notes';
  59. case 1:
  60. return '1 note';
  61. default:
  62. return count + ' notes';
  63. }
  64. }
  65. var SetFileInfo = function(jsonData, dir, file_path, stat) {
  66. //console.log(jsonData.title);
  67. if (typeof(jsonData)==='undefined') jsonData = {};
  68. var file_obj = {
  69. title: jsonData.title,
  70. path: file_path,
  71. size: prettySize(directorySize(dir)),
  72. type: getFileType(file_path),
  73. author: jsonData.author,
  74. index: jsonData.index,
  75. id: jsonData.id,
  76. created_at: dateFormat(stat["birthdate"], "mediumDate"),
  77. modified_at: dateFormat(stat["mtime"], "mediumDate"),
  78. accessed_at: dateFormat(stat["atime"], "mediumDate")
  79. }
  80. return file_obj;
  81. }
  82. var getNotesFromFolders = function(dir) {
  83. if (typeof(dir)==='undefined') dir = notes_dir;
  84. var filesystem = require("fs");
  85. filesystem.readdirSync(dir).forEach(function(file) {
  86. file_path = dir+'/'+file;
  87. var stat = filesystem.statSync(file_path);
  88. if (stat && stat.isDirectory()) {
  89. notes = notes.concat(getNotesFromFolders(file_path))
  90. } else {
  91. if(file == "info.json"){
  92. filesystem.readFile(file_path, function(err, data) {
  93. var jsonData = JSON.parse(data);
  94. var file_obj = SetFileInfo(jsonData, dir, file_path, stat)
  95. notes.push(file_obj);
  96. });
  97. }
  98. }
  99. });
  100. return notes;
  101. };
  102. this.getNotesFromFolders = getNotesFromFolders;
  103. var getAllFilesFromFolder = function(dir) {
  104. if (typeof(dir)==='undefined') dir = notes_dir;
  105. var filesystem = require("fs");
  106. var results = [];
  107. filesystem.readdirSync(dir).forEach(function(file) {
  108. file_path = dir+'/'+file;
  109. var stat = filesystem.statSync(file_path);
  110. if (stat && stat.isDirectory()) {
  111. results = results.concat(getAllFilesFromFolder(file_path))
  112. } else {
  113. if(file != ".DS_Store" && file != "info.json") {
  114. var jsonData = {};
  115. var file_obj = SetFileInfo(jsonData, dir, file_path, stat)
  116. results.push(file_obj);
  117. }
  118. }
  119. //console.log(file_obj);
  120. });
  121. return results;
  122. };
  123. var findNoteInFolder = function(note_id, dir) {
  124. if (typeof(dir)==='undefined') dir = notes_dir;
  125. var filesystem = require("fs");
  126. var results = [];
  127. filesystem.readdirSync(dir).forEach(function(file) {
  128. file_path = dir+'/'+file;
  129. var stat = filesystem.statSync(file_path);
  130. if (stat && stat.isDirectory()) {
  131. results = results.concat(findNoteInFolder(note_id, file_path))
  132. } else {
  133. if(file == "info.json"){
  134. filesystem.readFile(file_path, function(err, data) {
  135. var jsonData = JSON.parse(data);
  136. console.log(jsonData.id)
  137. if(jsonData.id == note_id){
  138. var file_obj = SetFileInfo(jsonData, dir, file_path, stat)
  139. results.push(file_obj);
  140. }
  141. });
  142. }
  143. }
  144. });
  145. console.log(results);
  146. return results[0];
  147. };
  148. this.saveFile = function(file_path, content){
  149. var fs = require('fs');
  150. fs.writeFile(file_path, content, function(err) {
  151. if(err) {
  152. return console.log(err);
  153. }
  154. console.log("-> FILE SAVED: " + file_path);
  155. });
  156. }
  157. this.getNote = function(file_path){
  158. var filesystem = require("fs");
  159. var stat = filesystem.statSync(file_path);
  160. var file_obj = {
  161. title: "",
  162. path: file_path,
  163. size: stat['size'],
  164. type: getFileType(file_path),
  165. author: "",
  166. index: "",
  167. id: "",
  168. created_at: dateFormat(stat["birthdate"], "mediumDate"),
  169. modified_at: dateFormat(stat["mtime"], "mediumDate"),
  170. accessed_at: dateFormat(stat["atime"], "mediumDate")
  171. }
  172. return file_obj
  173. }
  174. // RESPONSE
  175. this.getNotes = function() {
  176. notes = [];
  177. return getAllFilesFromFolder();
  178. }
  179. this.getCurrentNote = function() {
  180. return current_note;
  181. }
  182. this.setCurrentNote = function(note) {
  183. //console.log("searcing for: " + note_id)
  184. current_note = note;
  185. //console.log(current_note);
  186. //console.log("Current_note: " + current_note.title)
  187. }
  188. this.getNotesDir = function() {
  189. return notes_dir;
  190. }
  191. this.getDefaultNotesDir = function() {
  192. return default_notes_dir;
  193. }
  194. }])