-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.js
125 lines (111 loc) · 3.97 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const { req, res } = api;
if (req.method == "POST") {
// try to create under a note with label - messageInbox, you can rename it .
let messageInbox = api.getNoteWithLabel("messageInbox");
if (!messageInbox) {
// fallback to day note
messageInbox = api.getDayNote(api.dayjs().format("YYYY-MM-DD"));
}
// text note
let { content } = req.body;
if (content !== undefined) {
let { title, labelString = "" } = req.body;
if (!content) {
content = "";
if (!title) {
title = "from ios shortcut";
}
} else {
if (!title) {
title = content.slice(0, 20).replace("\n", " ");
}
// normalize \n from message
content = content.split("\n").reduce((final, block) => {
return (final += `<p>${block}</p>`);
}, "");
}
let note;
const exist = api.searchForNote(`note.title = '${title}'`);
if (exist) {
note = exist;
note.setContent(`${note.getContent()}${content}`);
} else {
note = api.createNewNote({
parentNoteId: messageInbox.noteId,
title,
content,
type: "text",
}).note;
const labels = labelString.replace(/\n/g, "").split("#");
const trimedLabels = labels
.map((label) => label.trim())
.filter((label) => label);
if (trimedLabels.length) {
trimedLabels.forEach((label) => {
const [name, value] = label.split(" ");
if (value) {
note.setLabel(name, value);
} else {
note.setLabel(name);
}
});
} else {
note.setLabel("from ios shortcut");
}
}
res.status(200).json({
code: 200,
msg: "success",
params: req.body,
result: note.getPojo(),
});
// files
} else {
const multer = require("multer");
const importRoute = require("../routes/api/import");
const multerOptions = {
fileFilter: (req, file, cb) => {
// UTF-8 file names are not well decoded by multer/busboy, so we handle the conversion on our side.
// See https://github.com/expressjs/multer/pull/1102.
file.originalname = Buffer.from(
file.originalname,
"latin1"
).toString("utf-8");
cb(null, true);
},
};
const uploadMiddleware = multer(multerOptions).single("upload");
uploadMiddleware(req, res, () => {
req.body.taskId = api.randomString(10);
req.body.last = "true";
req.params.parentNoteId = messageInbox.noteId;
const options = {
safeImport: "true", // set to "false" (with quotes) if you don't need it, same below.
shrinkImages: "true",
textImportedAsText: "true",
codeImportedAsCode: "true",
explodeArchives: "true",
replaceUnderscoresWithSpaces: "true",
};
req.body = { ...req.body, ...options };
importRoute
.importToBranch(req)
.then((resualt) => {
res.status(200).json({
code: 200,
msg: "success",
params: req.body,
result: resualt,
});
})
.catch((err) => {
res.status(500).json({
code: 500,
msg: "fail",
params: req.body,
error: err,
});
});
});
}
}