Skip to content

Commit 9fba268

Browse files
author
Daan Sprenkels
committed
Prevent subdirectories in user export
This commit also refactors the code a bit, and adds a '-' separator between a filename and its duplicate index. This commit fixes #1079. Signed-off-by: Daan Sprenkels <hello@dsprenkels.com>
1 parent 8bace89 commit 9fba268

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

lib/web/userRouter.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,20 @@ UserRouter.get('/me/export', function (req, res) {
9090
ownerId: user.id
9191
}
9292
}).then(function (notes) {
93-
let list = []
93+
let filenames = {}
9494
async.each(notes, function (note, callback) {
95-
let title
96-
let extension = ''
95+
let basename = note.title.replace(/\//g, '-') // Prevent subdirectories
96+
let filename
97+
let suffix = ''
9798
do {
98-
title = note.title + extension
99-
extension++
100-
} while (list.indexOf(title) !== -1)
99+
let seperator = typeof suffix === 'number' ? '-' : ''
100+
filename = basename + seperator + suffix + '.md'
101+
suffix++
102+
} while (filenames[filename])
103+
filenames[filename] = true
101104

102-
list.push(title)
103-
logger.debug('Write: ' + title + '.md')
104-
archive.append(Buffer.from(note.content), { name: title + '.md', date: note.lastchangeAt })
105+
logger.debug('Write: ' + filename)
106+
archive.append(Buffer.from(note.content), { name: filename, date: note.lastchangeAt })
105107
callback(null, null)
106108
}, function (err) {
107109
if (err) {

0 commit comments

Comments
 (0)