Update note model if doc in filesystem have newer modified will update it in db

This commit is contained in:
Cheng-Han, Wu 2016-05-30 12:43:51 +08:00
parent da45b7dc10
commit dfd2c6297c
1 changed files with 23 additions and 1 deletions

View File

@ -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)) {