Skip to content

Commit dfd2c62

Browse files
committed
Update note model if doc in filesystem have newer modified will update it in db
1 parent da45b7d commit dfd2c62

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

lib/models/note.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var cheerio = require('cheerio');
99
var shortId = require('shortid');
1010
var Sequelize = require("sequelize");
1111
var async = require('async');
12+
var moment = require('moment');
1213

1314
// core
1415
var config = require("../config.js");
@@ -91,7 +92,28 @@ module.exports = function (sequelize, DataTypes) {
9192
}
9293
}).then(function (note) {
9394
if (note) {
94-
return callback(null, note.id);
95+
var filePath = path.join(config.docspath, noteId + '.md');
96+
if (Note.checkFileExist(filePath)) {
97+
// if doc in filesystem have newer modified time than last change time
98+
// then will update the doc in db
99+
var fsModifiedTime = moment(fs.statSync(filePath).mtime);
100+
var dbModifiedTime = moment(note.lastchangeAt || note.createdAt);
101+
if (fsModifiedTime.isAfter(dbModifiedTime)) {
102+
var body = fs.readFileSync(filePath, 'utf8');
103+
note.title = LZString.compressToBase64(Note.parseNoteTitle(body));
104+
note.content = LZString.compressToBase64(body);
105+
note.lastchangeAt = fsModifiedTime;
106+
note.save().then(function (note) {
107+
return callback(null, note.id);
108+
}).catch(function (err) {
109+
return _callback(err, null);
110+
});
111+
} else {
112+
return callback(null, note.id);
113+
}
114+
} else {
115+
return callback(null, note.id);
116+
}
95117
} else {
96118
var filePath = path.join(config.docspath, noteId + '.md');
97119
if (Note.checkFileExist(filePath)) {

0 commit comments

Comments
 (0)