image import/export related fixes

This commit is contained in:
zadam 2019-07-10 23:01:30 +02:00
parent c3e1126489
commit 9de2927304
4 changed files with 8 additions and 9 deletions

View File

@ -1,9 +1,9 @@
"use strict"; "use strict";
const imageType = require('image-type');
const imageService = require('../../services/image'); const imageService = require('../../services/image');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const dateNoteService = require('../../services/date_notes'); const dateNoteService = require('../../services/date_notes');
const sql = require('../../services/sql');
const noteService = require('../../services/notes'); const noteService = require('../../services/notes');
const passwordEncryptionService = require('../../services/password_encryption'); const passwordEncryptionService = require('../../services/password_encryption');
const optionService = require('../../services/options'); const optionService = require('../../services/options');
@ -36,9 +36,11 @@ async function uploadImage(req) {
return [400, "Unknown image type: " + file.mimetype]; return [400, "Unknown image type: " + file.mimetype];
} }
const originalName = "Sender image." + imageType(file.buffer).ext;
const parentNote = await dateNoteService.getDateNote(req.headers['x-local-date']); const parentNote = await dateNoteService.getDateNote(req.headers['x-local-date']);
const {noteId} = await imageService.saveImage(file.buffer, "Sender image", parentNote.noteId, true); const {noteId} = await imageService.saveImage(file.buffer, originalName, parentNote.noteId, true);
return { return {
noteId: noteId noteId: noteId

View File

@ -32,11 +32,11 @@ async function exportToTar(exportContext, branch, format, res) {
do { do {
index = existingFileNames[lcFileName]++; index = existingFileNames[lcFileName]++;
newName = lcFileName + "_" + index; newName = index + "_" + lcFileName;
} }
while (newName in existingFileNames); while (newName in existingFileNames);
return fileName + "_" + index; return index + "_" + fileName;
} }
else { else {
existingFileNames[lcFileName] = 1; existingFileNames[lcFileName] = 1;

View File

@ -26,8 +26,7 @@ async function saveImage(buffer, originalName, parentNoteId, shrinkImageSwitch)
const parentNote = await repository.getNote(parentNoteId); const parentNote = await repository.getNote(parentNoteId);
const fileNameWithoutExtension = originalName.replace(/\.[^/.]+$/, ""); const fileName = sanitizeFilename(originalName);
const fileName = sanitizeFilename(fileNameWithoutExtension + "." + imageFormat.ext);
const {note} = await noteService.createNote(parentNoteId, fileName, finalImageBuffer, { const {note} = await noteService.createNote(parentNoteId, fileName, finalImageBuffer, {
target: 'into', target: 'into',

View File

@ -75,8 +75,6 @@ function getMime(fileName) {
const ext = path.extname(fileName).toLowerCase(); const ext = path.extname(fileName).toLowerCase();
if (ext in EXTENSION_TO_MIME) { if (ext in EXTENSION_TO_MIME) {
console.log(EXTENSION_TO_MIME[ext]);
return EXTENSION_TO_MIME[ext]; return EXTENSION_TO_MIME[ext];
} }
@ -108,7 +106,7 @@ async function importSingleFile(importContext, file, parentNote) {
} }
async function importImage(file, parentNote, importContext) { async function importImage(file, parentNote, importContext) {
const {note} = await imageService.saveImage(file.buffer, getFileNameWithoutExtension(file.originalname), parentNote.noteId, importContext.shrinkImages); const {note} = await imageService.saveImage(file.buffer, file.originalname, parentNote.noteId, importContext.shrinkImages);
importContext.increaseProgressCount(); importContext.increaseProgressCount();