Skip to content

Commit 2467fce

Browse files
authored
Merge pull request #1674 from hackmdio/fix/free-url-can-read-any-md-in-file-system
Fix/free url can read any md in file system
2 parents bef0701 + 965cca9 commit 2467fce

File tree

2 files changed

+78
-65
lines changed

2 files changed

+78
-65
lines changed

lib/models/note.js

Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,22 @@ module.exports = function (sequelize, DataTypes) {
9292
return new Promise(function (resolve, reject) {
9393
// if no content specified then use default note
9494
if (!note.content) {
95-
var body = null
96-
let filePath = null
97-
if (!note.alias) {
98-
filePath = config.defaultNotePath
99-
} else {
100-
filePath = path.join(config.docsPath, note.alias + '.md')
95+
let filePath = config.defaultNotePath
96+
97+
if (note.alias) {
98+
const notePathInDocPath = path.join(config.docsPath, path.basename(note.alias) + '.md')
99+
if (Note.checkFileExist(notePathInDocPath)) {
100+
filePath = notePathInDocPath
101+
}
101102
}
103+
102104
if (Note.checkFileExist(filePath)) {
103-
var fsCreatedTime = moment(fs.statSync(filePath).ctime)
104-
body = fs.readFileSync(filePath, 'utf8')
105-
note.title = Note.parseNoteTitle(body)
106-
note.content = body
105+
const noteInFS = readFileSystemNote(filePath)
106+
note.title = noteInFS.title
107+
note.content = noteInFS.content
107108
if (filePath !== config.defaultNotePath) {
108-
note.createdAt = fsCreatedTime
109+
note.createdAt = noteInFS.lastchangeAt
110+
note.lastchangeAt = noteInFS.lastchangeAt
109111
}
110112
}
111113
}
@@ -196,6 +198,29 @@ module.exports = function (sequelize, DataTypes) {
196198
})
197199
})
198200
}
201+
202+
async function syncNote (noteInFS, note) {
203+
const contentLength = noteInFS.content.length
204+
205+
let note2 = await note.update({
206+
title: noteInFS.title,
207+
content: noteInFS.content,
208+
lastchangeAt: noteInFS.lastchangeAt
209+
})
210+
const revision = await sequelize.models.Revision.saveNoteRevisionAsync(note2)
211+
// update authorship on after making revision of docs
212+
const patch = dmp.patch_fromText(revision.patch)
213+
const operations = Note.transformPatchToOperations(patch, contentLength)
214+
let authorship = note2.authorship
215+
for (let i = 0; i < operations.length; i++) {
216+
authorship = Note.updateAuthorshipByOperation(operations[i], null, authorship)
217+
}
218+
note2 = await note.update({
219+
authorship: authorship
220+
})
221+
return note2.id
222+
}
223+
199224
Note.parseNoteId = function (noteId, callback) {
200225
async.series({
201226
parseNoteIdByAlias: function (_callback) {
@@ -204,65 +229,35 @@ module.exports = function (sequelize, DataTypes) {
204229
where: {
205230
alias: noteId
206231
}
207-
}).then(function (note) {
208-
if (note) {
209-
const filePath = path.join(config.docsPath, noteId + '.md')
210-
if (Note.checkFileExist(filePath)) {
211-
// if doc in filesystem have newer modified time than last change time
212-
// then will update the doc in db
213-
var fsModifiedTime = moment(fs.statSync(filePath).mtime)
214-
var dbModifiedTime = moment(note.lastchangeAt || note.createdAt)
215-
var body = fs.readFileSync(filePath, 'utf8')
216-
var contentLength = body.length
217-
var title = Note.parseNoteTitle(body)
218-
if (fsModifiedTime.isAfter(dbModifiedTime) && note.content !== body) {
219-
note.update({
220-
title: title,
221-
content: body,
222-
lastchangeAt: fsModifiedTime
223-
}).then(function (note) {
224-
sequelize.models.Revision.saveNoteRevision(note, function (err, revision) {
225-
if (err) return _callback(err, null)
226-
// update authorship on after making revision of docs
227-
var patch = dmp.patch_fromText(revision.patch)
228-
var operations = Note.transformPatchToOperations(patch, contentLength)
229-
var authorship = note.authorship
230-
for (let i = 0; i < operations.length; i++) {
231-
authorship = Note.updateAuthorshipByOperation(operations[i], null, authorship)
232-
}
233-
note.update({
234-
authorship: authorship
235-
}).then(function (note) {
236-
return callback(null, note.id)
237-
}).catch(function (err) {
238-
return _callback(err, null)
239-
})
240-
})
241-
}).catch(function (err) {
242-
return _callback(err, null)
243-
})
232+
}).then(async function (note) {
233+
const filePath = path.join(config.docsPath, path.basename(noteId) + '.md')
234+
if (Note.checkFileExist(filePath)) {
235+
try {
236+
if (note) {
237+
// if doc in filesystem have newer modified time than last change time
238+
// then will update the doc in db
239+
const noteInFS = readFileSystemNote(filePath)
240+
if (shouldSyncNote(note, noteInFS)) {
241+
const noteId = await syncNote(noteInFS, note)
242+
return callback(null, noteId)
243+
}
244244
} else {
245+
// create new note with alias, and will sync md file in beforeCreateHook
246+
const note = await Note.create({
247+
alias: noteId,
248+
owner: null,
249+
permission: 'locked'
250+
})
245251
return callback(null, note.id)
246252
}
247-
} else {
248-
return callback(null, note.id)
249-
}
250-
} else {
251-
var filePath = path.join(config.docsPath, noteId + '.md')
252-
if (Note.checkFileExist(filePath)) {
253-
Note.create({
254-
alias: noteId,
255-
owner: null,
256-
permission: 'locked'
257-
}).then(function (note) {
258-
return callback(null, note.id)
259-
}).catch(function (err) {
260-
return _callback(err, null)
261-
})
262-
} else {
263-
return _callback(null, null)
253+
} catch (err) {
254+
return callback(err, null)
264255
}
265256
}
257+
if (!note) {
258+
return callback(null, null)
259+
}
260+
return callback(null, note.id)
266261
}).catch(function (err) {
267262
return _callback(err, null)
268263
})
@@ -589,5 +584,21 @@ module.exports = function (sequelize, DataTypes) {
589584
return operations
590585
}
591586

587+
function readFileSystemNote (filePath) {
588+
const fsModifiedTime = moment(fs.statSync(filePath).mtime)
589+
const content = fs.readFileSync(filePath, 'utf8')
590+
591+
return {
592+
lastchangeAt: fsModifiedTime,
593+
title: Note.parseNoteTitle(content),
594+
content: content
595+
}
596+
}
597+
598+
function shouldSyncNote (note, noteInFS) {
599+
const dbModifiedTime = moment(note.lastchangeAt || note.createdAt)
600+
return noteInFS.lastchangeAt.isAfter(dbModifiedTime) && note.content !== noteInFS.content
601+
}
602+
592603
return Note
593604
}

lib/models/revision.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var moment = require('moment')
66
var childProcess = require('child_process')
77
var shortId = require('shortid')
88
var path = require('path')
9+
var util = require('util')
910

1011
var Op = Sequelize.Op
1112

@@ -296,6 +297,7 @@ module.exports = function (sequelize, DataTypes) {
296297
return callback(err, null)
297298
})
298299
}
300+
Revision.saveNoteRevisionAsync = util.promisify(Revision.saveNoteRevision)
299301
Revision.finishSaveNoteRevision = function (note, revision, callback) {
300302
note.update({
301303
savedAt: revision.updatedAt

0 commit comments

Comments
 (0)