123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- angular.module('codexApp')
- .service('FileService', [ '$rootScope', '$http', function($rootScope, $http) {
- var notes_dir = "/Users/james/dev/codex/codex";
- var default_notes_dir = "/Users/james/dev/codex/codex/inbox";
- var notes = [];
- var current_note = "";
- var prettySize = function(bytes) {
- if (bytes <= 1024) {
- return bytes + " KB"
- } else {
- var megabytes = parseFloat(Math.round(bytes/1024 * 100) / 100).toFixed(0);
- return megabytes + " MB"
- }
- }
- var getFilePathExtension = function(path) {
- var filename = path.split('\\').pop().split('/').pop();
- var lastIndex = filename.lastIndexOf(".");
- if (lastIndex < 1) return "";
- return filename.substr(lastIndex + 1);
- }
- var getFileType = function(path) {
- var extension = getFilePathExtension(path);
- switch (extension) {
- case "pdf":
- return "Document";
- case "jpg":
- return "Image";
- case "png":
- return "Image";
- case "md":
- return "Markdown";
- default:
- return "File";
- }
- }
- var directorySize = function(dir) {
- var filesystem = require("fs");
- var size = 0;
- var results = [];
- filesystem.readdirSync(dir).forEach(function(file) {
- file_path = dir+'/'+file;
- var stat = filesystem.statSync(file_path);
- if (stat && stat.isDirectory()) {
- results = results.concat(directorySize(file_path))
- } else {
- if(file != ".DS_Store") {
- size = size + stat["size"];
-
- }
- }
- });
- return size;
- }
- var noteCount = function(){
- var count = $scope.notes.length
- switch (count) {
- case 0:
- return '0 notes';
- case 1:
- return '1 note';
- default:
- return count + ' notes';
- }
- }
- var SetFileInfo = function(jsonData, dir, file_path, stat) {
-
- if (typeof(jsonData)==='undefined') jsonData = {};
- var file_obj = {
- title: jsonData.title,
- path: file_path,
- size: prettySize(directorySize(dir)),
- type: getFileType(file_path),
- author: jsonData.author,
- index: jsonData.index,
- id: jsonData.id,
- created_at: dateFormat(stat["birthdate"], "mediumDate"),
- modified_at: dateFormat(stat["mtime"], "mediumDate"),
- accessed_at: dateFormat(stat["atime"], "mediumDate")
- }
- return file_obj;
- }
- var getNotesFromFolders = function(dir) {
- if (typeof(dir)==='undefined') dir = notes_dir;
- var filesystem = require("fs");
- filesystem.readdirSync(dir).forEach(function(file) {
- file_path = dir+'/'+file;
- var stat = filesystem.statSync(file_path);
- if (stat && stat.isDirectory()) {
- notes = notes.concat(getNotesFromFolders(file_path))
- } else {
- if(file == "info.json"){
- filesystem.readFile(file_path, function(err, data) {
- var jsonData = JSON.parse(data);
- var file_obj = SetFileInfo(jsonData, dir, file_path, stat)
- notes.push(file_obj);
- });
- }
- }
- });
- return notes;
- };
- this.getNotesFromFolders = getNotesFromFolders;
- var getAllFilesFromFolder = function(dir) {
- if (typeof(dir)==='undefined') dir = notes_dir;
- var filesystem = require("fs");
- var results = [];
- filesystem.readdirSync(dir).forEach(function(file) {
- file_path = dir+'/'+file;
- var stat = filesystem.statSync(file_path);
- if (stat && stat.isDirectory()) {
- results = results.concat(getAllFilesFromFolder(file_path))
- } else {
- if(file != ".DS_Store" && file != "info.json") {
- var jsonData = {};
- var file_obj = SetFileInfo(jsonData, dir, file_path, stat)
- results.push(file_obj);
- }
- }
-
- });
- return results;
- };
- var findNoteInFolder = function(note_id, dir) {
- if (typeof(dir)==='undefined') dir = notes_dir;
- var filesystem = require("fs");
- var results = [];
- filesystem.readdirSync(dir).forEach(function(file) {
- file_path = dir+'/'+file;
- var stat = filesystem.statSync(file_path);
- if (stat && stat.isDirectory()) {
- results = results.concat(findNoteInFolder(note_id, file_path))
- } else {
- if(file == "info.json"){
- filesystem.readFile(file_path, function(err, data) {
- var jsonData = JSON.parse(data);
- console.log(jsonData.id)
- if(jsonData.id == note_id){
- var file_obj = SetFileInfo(jsonData, dir, file_path, stat)
- results.push(file_obj);
- }
- });
- }
- }
- });
- console.log(results);
- return results[0];
- };
- this.saveFile = function(file_path, content){
- var fs = require('fs');
- fs.writeFile(file_path, content, function(err) {
- if(err) {
- return console.log(err);
- }
- console.log("-> FILE SAVED: " + file_path);
- });
- }
- this.getNote = function(file_path){
- var filesystem = require("fs");
- var stat = filesystem.statSync(file_path);
- var file_obj = {
- title: "",
- path: file_path,
- size: stat['size'],
- type: getFileType(file_path),
- author: "",
- index: "",
- id: "",
- created_at: dateFormat(stat["birthdate"], "mediumDate"),
- modified_at: dateFormat(stat["mtime"], "mediumDate"),
- accessed_at: dateFormat(stat["atime"], "mediumDate")
- }
- return file_obj
- }
-
- this.getNotes = function() {
- notes = [];
- return getAllFilesFromFolder();
- }
- this.getCurrentNote = function() {
- return current_note;
- }
- this.setCurrentNote = function(note) {
-
- current_note = note;
-
-
- }
- this.getNotesDir = function() {
- return notes_dir;
- }
- this.getDefaultNotesDir = function() {
- return default_notes_dir;
- }
- }])
|