diff options
author | Cheng-Han, Wu | 2016-05-30 12:43:51 +0800 |
---|---|---|
committer | Cheng-Han, Wu | 2016-05-30 12:43:51 +0800 |
commit | dfd2c6297c9dd3e0d7469bed23568489efb1df21 (patch) | |
tree | 2996282d81856638cddf7ebaf3bf1372d64e8738 /lib/models | |
parent | da45b7dc10594e0197008078d9e0c0aa1eb9542f (diff) |
Update note model if doc in filesystem have newer modified will update it in db
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/note.js | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/lib/models/note.js b/lib/models/note.js index 84da7851..4c501d2d 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)) { |