2017-12-03 12:41:18 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-01 23:42:12 +08:00
|
|
|
const repository = require('../../services/repository');
|
2018-11-16 19:12:04 +08:00
|
|
|
const enexImportService = require('../../services/import/enex');
|
|
|
|
const opmlImportService = require('../../services/import/opml');
|
|
|
|
const tarImportService = require('../../services/import/tar');
|
|
|
|
const markdownImportService = require('../../services/import/markdown');
|
2018-02-26 13:07:43 +08:00
|
|
|
const path = require('path');
|
2018-05-30 08:32:13 +08:00
|
|
|
|
|
|
|
async function importToBranch(req) {
|
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
const file = req.file;
|
|
|
|
|
2018-09-11 05:41:11 +08:00
|
|
|
if (!file) {
|
2018-09-04 03:06:24 +08:00
|
|
|
return [400, "No file has been uploaded"];
|
|
|
|
}
|
|
|
|
|
2018-05-30 08:32:13 +08:00
|
|
|
const parentNote = await repository.getNote(parentNoteId);
|
|
|
|
|
|
|
|
if (!parentNote) {
|
|
|
|
return [404, `Note ${parentNoteId} doesn't exist.`];
|
|
|
|
}
|
|
|
|
|
|
|
|
const extension = path.extname(file.originalname).toLowerCase();
|
|
|
|
|
|
|
|
if (extension === '.tar') {
|
2018-11-16 21:36:50 +08:00
|
|
|
return await tarImportService.importTar(file.buffer, parentNote);
|
2018-05-30 08:32:13 +08:00
|
|
|
}
|
|
|
|
else if (extension === '.opml') {
|
2018-11-16 21:36:50 +08:00
|
|
|
return await opmlImportService.importOpml(file.buffer, parentNote);
|
2018-05-30 08:32:13 +08:00
|
|
|
}
|
2018-09-03 19:40:40 +08:00
|
|
|
else if (extension === '.md') {
|
2018-11-16 19:12:04 +08:00
|
|
|
return await markdownImportService.importMarkdown(file, parentNote);
|
2018-11-05 07:06:17 +08:00
|
|
|
}
|
|
|
|
else if (extension === '.enex') {
|
2018-11-16 19:12:04 +08:00
|
|
|
return await enexImportService.importEnex(file, parentNote);
|
2018-09-03 19:40:40 +08:00
|
|
|
}
|
2018-05-30 08:32:13 +08:00
|
|
|
else {
|
|
|
|
return [400, `Unrecognized extension ${extension}, must be .tar or .opml`];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 03:34:07 +08:00
|
|
|
module.exports = {
|
2018-05-30 08:32:13 +08:00
|
|
|
importToBranch
|
2018-03-31 03:34:07 +08:00
|
|
|
};
|