@@ -9,6 +9,7 @@ var cheerio = require('cheerio');
99var shortId = require ( 'shortid' ) ;
1010var Sequelize = require ( "sequelize" ) ;
1111var async = require ( 'async' ) ;
12+ var moment = require ( 'moment' ) ;
1213
1314// core
1415var 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