mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 03:16:11 +01:00
relation between notes and images
This commit is contained in:
@@ -24,11 +24,16 @@ router.get('/:imageId/:filename', auth.checkApiAuth, async (req, res, next) => {
|
||||
res.send(image.data);
|
||||
});
|
||||
|
||||
router.post('/upload', auth.checkApiAuth, multer.single('upload'), async (req, res, next) => {
|
||||
router.post('', auth.checkApiAuth, multer.single('upload'), async (req, res, next) => {
|
||||
const sourceId = req.headers.source_id;
|
||||
const noteId = req.query.noteId;
|
||||
const file = req.file;
|
||||
|
||||
const imageId = utils.newNoteId();
|
||||
const note = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]);
|
||||
|
||||
if (!note) {
|
||||
return req.status(404).send(`Note ${noteId} doesn't exist.`);
|
||||
}
|
||||
|
||||
if (!file.mimetype.startsWith("image/")) {
|
||||
return req.send("Unknown image type: " + file.mimetype);
|
||||
@@ -39,6 +44,8 @@ router.post('/upload', auth.checkApiAuth, multer.single('upload'), async (req, r
|
||||
const resizedImage = await resize(file.buffer);
|
||||
const optimizedImage = await optimize(resizedImage);
|
||||
|
||||
const imageId = utils.newImageId();
|
||||
|
||||
await sql.doInTransaction(async () => {
|
||||
await sql.insert("images", {
|
||||
image_id: imageId,
|
||||
@@ -52,11 +59,24 @@ router.post('/upload', auth.checkApiAuth, multer.single('upload'), async (req, r
|
||||
});
|
||||
|
||||
await sync_table.addImageSync(imageId, sourceId);
|
||||
|
||||
const noteImageId = utils.newNoteImageId();
|
||||
|
||||
await sql.insert("notes_image", {
|
||||
note_image_id: noteImageId,
|
||||
note_id: noteId,
|
||||
image_id: imageId,
|
||||
is_deleted: 0,
|
||||
date_modified: now,
|
||||
date_created: now
|
||||
});
|
||||
|
||||
await sync_table.addNoteImageSync(noteImageId, sourceId);
|
||||
});
|
||||
|
||||
res.send({
|
||||
uploaded: true,
|
||||
url: `/api/image/${imageId}/${file.originalname}`
|
||||
url: `/api/images/${imageId}/${file.originalname}`
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -133,6 +133,12 @@ router.get('/images/:imageId', auth.checkApiAuth, async (req, res, next) => {
|
||||
res.send(entity);
|
||||
});
|
||||
|
||||
router.get('/notes_image/:noteImageId', auth.checkApiAuth, async (req, res, next) => {
|
||||
const noteImageId = req.params.noteImageId;
|
||||
|
||||
res.send(await sql.getFirst("SELECT * FROM notes_image WHERE note_image_id = ?", [noteImageId]));
|
||||
});
|
||||
|
||||
router.put('/notes', auth.checkApiAuth, async (req, res, next) => {
|
||||
await syncUpdate.updateNote(req.body.entity, req.body.sourceId);
|
||||
|
||||
@@ -175,4 +181,10 @@ router.put('/images', auth.checkApiAuth, async (req, res, next) => {
|
||||
res.send({});
|
||||
});
|
||||
|
||||
router.put('/notes_image', auth.checkApiAuth, async (req, res, next) => {
|
||||
await syncUpdate.updateNoteImage(req.body.entity, req.body.sourceId);
|
||||
|
||||
res.send({});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user