123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- angular.module('codexApp')
- .service('FileService', [ '$rootScope', '$http', 'ThumbnailService', '$state', 'PrefsService', function($rootScope, $http, ThumbnailService, $state , PrefsService) {
- var defaultUserContentPath = "";
- var appDataPath = "";
- var appData = {};
- var notes_dir = "";
- var searched_files = [];
- var getAppData = function(){
- var remote = require('remote');
- var app = remote.require('app');
- appDataPath = app.getPath("userData");
- defaultUserContentPath = app.getPath("home") + "/Documents/codex";
- findOrGenerateUserDataFile(appDataPath, defaultUserContentPath);
- var raw_data = fs.readFileSync(appDataPath + '/userData.json', 'utf8');
- var data = JSON.parse(raw_data);
- appData = data;
- notes_dir = appData.UserDataDirectory;
- return data
- }
- var findOrGenerateUserDataFile = function(path, defaultUserContentPath) {
- var generate_settings = true;
- fs = require('fs')
- fs.readdirSync(path).forEach(function(file) {
- if(file == "UserData.json"){
- console.log("-> Loading Settings");
- generate_settings = false;
- }
- });
- if (generate_settings){
-
- file_path = path + "/UserData.json";
- console.log("-> Generating user settings file: '" + file_path + "'");
- var content = '{ "UserDataDirectory" : "' + defaultUserContentPath +'" }';
- mkdirSync(defaultUserContentPath);
- console.log(content);
- saveAppData(JSON.parse(content));
- return true;
- }
- }
- var saveAppData = function(data) {
- console.log("-> Saving user data...");
- console.log(data);
- fs.writeFileSync(appDataPath + "/UserData.json", JSON.stringify(data), 'utf8');
- appData = data;
- }
- var mkdirSync = function (path) {
- try {
- fs.mkdirSync(path);
- } catch(e) {
- if ( e.code != 'EEXIST' ) throw e;
- }
- }
- getAppData();
- console.log("-> Loading content from folder: " + appData.UserDataDirectory);
- 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);
- var thumbs = appData.thumbs
- switch (type) {
- case "Markdown":
- var thumb = ""
- for (var i=0; i < thumbs.length; i++) {
- if (thumbs[i].file === file_path) {
- thumb = thumbs[i].path;
- break;
- }
- }
- if(thumb == "" || thumb == undefined){
-
-
- console.log("> NO THUMBNAIL FOUND! GENERATING NEW ONE")
- thumb = saveThumbnail(file_path);
- }
- return thumb
- default:
- return "";
- }
- }
- var saveThumbnail = function(file_path){
- for (var i=0; i < appData.thumbs.length; i++) {
- if (appData.thumbs[i].file === file_path) {
- var fs = require("fs");
- fs.unlink(appData.thumbs[i].path);
- appData.thumbs.splice(i)
- break;
- }
- }
- thumb = ThumbnailService.createNoteThumbnail(file_path, appDataPath);
- obj = { "file" : file_path, "path" : thumb }
- appData.thumbs.push(obj)
- saveAppData(appData)
- console.log("> Saving thumbnail: " + thumb )
- return thumb
- }
- 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);
- saveThumbnail(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;
-
-
- for (var i = 0; i < count; i++) {
- absolute_path.shift();
- }
-
- var relative_path = absolute_path.join('/');
-
- 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));
- }
- });
-
- 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;
- }
- var deleteFile = function(file){
- var fs = require('fs');
- fs.unlinkSync(file);
- console.log('-> Successfully Deleted ' + file);
- return true;
- }
-
- this.getAllFiles = function(dir) {
- getAppData();
- if (typeof(dir)==='undefined') dir = notes_dir;
- notes = getAllFilesFromFolder(dir);
- return notes.sort(date_sort_asc);
- }
- this.getFiles = function(dir) {
- getAppData();
- if (typeof(dir)==='undefined') dir = notes_dir;
- notes = getFilesFromFolder(dir);
- return notes.sort(date_sort_asc);
- }
- this.getAllNotes = function() {
- getAppData();
- 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;
- console.log(current_note);
-
- }
- this.goToPreviousNote = function(){
- if(note_history_index > 0) {
- note_history_index = note_history_index - 1;
- current_note = note_history[note_history_index];
- changeController();
- $rootScope.$broadcast('window-view:change');
- $rootScope.$broadcast('note-view:reload');
- }
- console.log(current_note);
- }
- 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];
- changeController();
- $rootScope.$broadcast('window-view:change');
- $rootScope.$broadcast('note-view:reload');
- }
- }
- this.setNotesDir = function(dir) {
- appData.UserDataDirectory = dir[0];
- saveAppData(appData);
- notes_dir = dir[0];
- }
- this.getNotesDir = function() {
- return notes_dir;
- }
- this.getDefaultNotesDir = function() {
- return default_notes_dir;
- }
- this.getDefaultNote = function() {
- return getNote(default_home_note);
- }
- var changeController = function(){
- console.log(current_note)
- if(current_note.search_results){
- searched_files = current_note.search_results;
- }
- switch (current_note.type) {
- case "Markdown":
- $state.go("note-view");
- break;
- case "Folder":
- $state.go("index");
- break;
- case "Image":
- $state.go("image-view");
- break;
- case "All Notes":
- PrefsService.setCurrentView("All Notes");
- $state.go("index");
- break;
- case "All Files":
- PrefsService.setCurrentView("All Files");
- $state.go("index");
- break;
- }
- }
- this.changeController = function() {
- changeController();
- }
- this.deleteFile = function(file) {
- return deleteFile(file);
- }
- this.getAppDataPath = function() {
- return appDataPath;
- }
- this.saveThumbnail = function(file_path){
- return saveThumbnail(file_path);
- }
- this.setSearchedFiles = function(files){
- searched_files = files;
- }
- this.getSearchFiles = function() {
- return searched_files;
- }
- }])
|