From dfd2c6297c9dd3e0d7469bed23568489efb1df21 Mon Sep 17 00:00:00 2001 From: Cheng-Han, Wu Date: Mon, 30 May 2016 12:43:51 +0800 Subject: Update note model if doc in filesystem have newer modified will update it in db --- lib/models/note.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'lib/models') 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)) { -- cgit v1.2.3