123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- angular.module('codexApp')
- .service('FileService', [ '$rootScope', '$http', 'ThumbnailService', function($rootScope, $http, ThumbnailService) {
- var notes_dir = "/Users/james/dev/codex/codex";
- var default_notes_dir = "/Users/james/dev/codex/codex/inbox";
- var default_home_note = "/Users/james/dev/codex/codex/index.md"
- var notes = [];
- var current_note = "";
- var note_history = [];
- var note_history_index = 0;
- 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);
- if (typeof(extension)==='undefined' || extension == "") extension = 'dir';
- switch (extension) {
- case "pdf":
- return "Document";
- case "jpg":
- return "Image";
- case "png":
- return "Image";
- case "md":
- return "Markdown";
- case 'dir':
- return "Folder";
- 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 getThumbnail = function(file_path) {
- var type = getFileType(file_path);
- switch (type) {
- case "Markdown":
- return ThumbnailService.createNoteThumbnail(file_path);
- default:
- return "";
- }
- }
- var isValidFile = function(file) {
- if(file != ".DS_Store" && file != "info.json") {
- var parts = file.split('.');
- if (parts[parts.length -2] == "thumb") {
- return false;
- } else {
- return true
- }
- }
- }
- var SetFileInfo = function(jsonData, dir, file_path, stat) {
-
- if (typeof(jsonData)==='undefined') jsonData = {};
- if(jsonData.title != "" && jsonData.title != undefined){
- var title = jsonData.title;
- } else {
- var title = getNameFromPath(file_path);
- }
- if(jsonData.thumbnail != "" && jsonData.thumbnail != undefined){
- var thumbnail_path = jsonData.thumbnail;
- } else {
- var thumbnail_path = getThumbnail(file_path);
- }
- var file_obj = {
- title: 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"),
- thumbnail: thumbnail_path
- }
- 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(isValidFile(file)) {
- var jsonData = {};
- var file_obj = SetFileInfo(jsonData, dir, file_path, stat)
- results.push(file_obj);
- }
- }
-
- });
- $rootScope.$broadcast('file-service:files-loaded');
- return results;
- };
- var getFilesFromFolder = 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(isValidFile(file)) {
- var jsonData = {};
- var file_obj = SetFileInfo(jsonData, dir, file_path, stat)
- results.push(file_obj);
- }
- });
- $rootScope.$broadcast('file-service:files-loaded');
- 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, 'utf8', function(err) {
- if(err) {
- console.log("-> ERROR SAVING FILE: " + file_path);
- console.log(err);
- } else {
- console.log("-> FILE SAVED: " + file_path);
- }
- });
- }
- var 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
- }
- var getNameFromPath = function(path) {
- path = path.split('/');
- var filename = path[path.length - 1];
-
-
-
- return filename
- }
- var getUrlParts = function(url) {
- var a = document.createElement('a');
- a.href = url;
- return {
- href: a.href,
- host: a.host,
- hostname: a.hostname,
- port: a.port,
- pathname: a.pathname,
- protocol: a.protocol,
- hash: a.hash,
- search: a.search
- };
- }
- var absoluteToRelativeURL = function(current_url, absolute_url) {
-
-
-
- var current_path = current_url.split('/');
- var absolute_path = getUrlParts(absolute_url).pathname.split('/');
- var root_path = getUrlParts(notes_dir).pathname.split('/');
-
-
- current_path.shift();
-
-
- var current_path_count = 0;
- for (var i = 0; i < current_path.length; i++) {
- current_path_count = current_path_count + 1;
- }
-
- var absolute_path_count = 0;
- for (var i = 0; i < absolute_path.length; i++) {
- absolute_path_count = absolute_path_count + 1;
- }
- var root_path_count = 0;
- for (var i = 0; i < root_path.length; i++) {
- root_path_count = root_path_count + 1;
- }
- var count = current_path_count - root_path_count;
- console.log(current_path_count + " - " + root_path_count + " = " + count);
-
- for (var i = 0; i < count; i++) {
- absolute_path.shift();
- }
-
- var relative_path = absolute_path.join('/');
- console.log("* Converted relative URL: " + relative_path)
- return relative_path;
- }
-
- this.absoluteToRelativeURL = function(current_url, absolute_url) {
- return absoluteToRelativeURL(current_url, absolute_url);
- }
- var shortenPath = function(path) {
-
-
-
- var current_path = path.split('/');
- var root_path = getUrlParts(notes_dir).pathname.split('/');
-
- var current_path_count = 0;
- for (var i = 0; i < current_path.length; i++) {
- current_path_count = current_path_count + 1;
- }
- var root_path_count = 0;
- for (var i = 0; i < root_path.length; i++) {
- root_path_count = root_path_count + 1;
- }
- var diff = current_path_count - root_path_count;
- var count = current_path_count - diff;
- console.log(current_path_count + " - " + root_path_count + " = " + count);
-
- for (var i = 0; i < count; i++) {
- current_path.shift();
- }
-
- var relative_path = current_path.join('/');
- console.log("* Shortened Path: " + relative_path)
- return relative_path;
- }
-
- this.shortenPath = function(path) {
- return shortenPath(path);
- }
-
- var getFolders = function(root) {
- if (typeof(root)==='undefined') root = notes_dir;
- var filesystem = require("fs");
- var results = [];
- filesystem.readdirSync(root).forEach(function(file) {
- file_path = root+'/'+file;
- var stat = filesystem.statSync(file_path);
- if (stat && stat.isDirectory()) {
- results.push(SetFileInfo( undefined, root, file_path, stat));
- }
- });
- console.log(results);
- return results;
- }
- this.getFolders = function(root){
- return getFolders(root);
- }
-
- var date_sort_asc = function (date1, date2) {
- if (date1.modified_at > date2.modified_at) return 1;
- if (date1.modified_at < date2.modified_at) return -1;
- return 0;
- };
- var date_sort_desc = function (date1, date2) {
- if (date1.modified_at > date2.modified_at) return -1;
- if (date1.modified_at < date2.modified_at) return 1;
- return 0;
- };
- var filterNotes = function(files) {
- var filtered = [];
- for (var i = 0; i < files.length; i++) {
- if(files[i].type == "Markdown") {
- filtered.push(files[i]);
- }
- }
- return filtered;
- }
-
- this.getAllFiles = function(dir) {
- if (typeof(dir)==='undefined') dir = notes_dir;
- notes = getAllFilesFromFolder(dir);
- return notes.sort(date_sort_asc);
- }
- this.getFiles = function(dir) {
- if (typeof(dir)==='undefined') dir = notes_dir;
- notes = getFilesFromFolder(dir);
- return notes.sort(date_sort_asc);
- }
- this.getAllNotes = function() {
- notes = getAllFilesFromFolder();
- notes = filterNotes(notes);
- return notes.sort(date_sort_asc);
- }
- this.getNote = function(path) {
- return getNote(path);
- }
- this.getCurrentNote = function() {
- return current_note;
- }
- this.setCurrentNote = function(note) {
-
- current_note = note;
- if((note_history.length -1) != note_history_index){
- var dif = note_history.length - note_history_index - 1;
- for (var i = 0; i < dif; i++) {
- note_history.pop();
- }
- }
- note_history.push(current_note);
- note_history_index = note_history.length -1;
-
-
- }
- this.goToPreviousNote = function(){
- if(note_history_index > 0) {
- note_history_index = note_history_index - 1;
- current_note = note_history[note_history_index];
- }
- }
- this.goToNextNote = function(){
- if(note_history_index < (note_history.length - 1)){
- note_history_index = note_history_index + 1;
- current_note = note_history[note_history_index];
- }
- }
- this.getNotesDir = function() {
- return notes_dir;
- }
- this.getDefaultNotesDir = function() {
- return default_notes_dir;
- }
- this.getDefaultNote = function() {
- return getNote(default_home_note);
- }
- }])
|