diff --git a/lib/models/note.js b/lib/models/note.js index 84da785..4c501d2 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -9,6 +9,7 @@ var cheerio = require('cheerio'); var shortId = require('shortid'); var Sequelize = require("sequelize"); var async = require('async'); +var moment = require('moment'); // core var config = require("../config.js"); @@ -91,7 +92,28 @@ module.exports = function (sequelize, DataTypes) { } }).then(function (note) { if (note) { - return callback(null, note.id); + var filePath = path.join(config.docspath, noteId + '.md'); + if (Note.checkFileExist(filePath)) { + // if doc in filesystem have newer modified time than last change time + // then will update the doc in db + var fsModifiedTime = moment(fs.statSync(filePath).mtime); + var dbModifiedTime = moment(note.lastchangeAt || note.createdAt); + if (fsModifiedTime.isAfter(dbModifiedTime)) { + var body = fs.readFileSync(filePath, 'utf8'); + note.title = LZString.compressToBase64(Note.parseNoteTitle(body)); + note.content = LZString.compressToBase64(body); + note.lastchangeAt = fsModifiedTime; + note.save().then(function (note) { + return callback(null, note.id); + }).catch(function (err) { + return _callback(err, null); + }); + } else { + return callback(null, note.id); + } + } else { + return callback(null, note.id); + } } else { var filePath = path.join(config.docspath, noteId + '.md'); if (Note.checkFileExist(filePath)) {