server-ts: Remove use of (req as any)

This commit is contained in:
Elian Doran 2024-04-07 14:29:08 +03:00
parent 7fe6d1ab4d
commit 34cd2eba91
No known key found for this signature in database
7 changed files with 36 additions and 21 deletions

View File

@ -14,11 +14,12 @@ import ValidationError = require('../../errors/validation_error');
import { Request, Response } from 'express';
import BNote = require('../../becca/entities/bnote');
import BAttachment = require('../../becca/entities/battachment');
import { AppRequest } from '../route-interface';
function updateFile(req: Request) {
function updateFile(req: AppRequest) {
const note = becca.getNoteOrThrow(req.params.noteId);
const file = (req as any).file;
const file = req.file;
note.saveRevision();
note.mime = file.mimetype.toLowerCase();
@ -35,9 +36,9 @@ function updateFile(req: Request) {
};
}
function updateAttachment(req: Request) {
function updateAttachment(req: AppRequest) {
const attachment = becca.getAttachmentOrThrow(req.params.attachmentId);
const file = (req as any).file;
const file = req.file;
attachment.getNote().saveRevision();

View File

@ -7,6 +7,7 @@ import fs = require('fs');
import { Request, Response } from 'express';
import BNote = require('../../becca/entities/bnote');
import BRevision = require('../../becca/entities/brevision');
import { AppRequest } from '../route-interface';
function returnImageFromNote(req: Request, res: Response) {
const image = becca.getNote(req.params.noteId);
@ -81,9 +82,9 @@ function returnAttachedImage(req: Request, res: Response) {
res.send(attachment.getContent());
}
function updateImage(req: Request) {
function updateImage(req: AppRequest) {
const {noteId} = req.params;
const {file} = (req as any);
const {file} = req;
const note = becca.getNoteOrThrow(noteId);
@ -94,6 +95,13 @@ function updateImage(req: Request) {
};
}
if (typeof file.buffer === "string") {
return {
uploaded: false,
message: "Invalid image content."
};
}
imageService.updateImage(noteId, file.buffer, file.originalname);
return { uploaded: true };

View File

@ -13,8 +13,9 @@ import TaskContext = require('../../services/task_context');
import ValidationError = require('../../errors/validation_error');
import { Request } from 'express';
import BNote = require('../../becca/entities/bnote');
import { AppRequest } from '../route-interface';
async function importNotesToBranch(req: Request) {
async function importNotesToBranch(req: AppRequest) {
const { parentNoteId } = req.params;
const { taskId, last } = req.body;
@ -27,7 +28,7 @@ async function importNotesToBranch(req: Request) {
replaceUnderscoresWithSpaces: req.body.replaceUnderscoresWithSpaces !== 'false'
};
const file = (req as any).file;
const file = req.file;
if (!file) {
throw new ValidationError("No file has been uploaded");
@ -49,7 +50,7 @@ async function importNotesToBranch(req: Request) {
const taskContext = TaskContext.getInstance(taskId, 'importNotes', options);
try {
if (extension === '.zip' && options.explodeArchives) {
if (extension === '.zip' && options.explodeArchives && typeof file.buffer !== "string") {
note = await zipImportService.importZip(taskContext, file.buffer, parentNote);
} else if (extension === '.opml' && options.explodeArchives) {
const importResult = await opmlImportService.importOpml(taskContext, file.buffer, parentNote);
@ -96,7 +97,7 @@ async function importNotesToBranch(req: Request) {
return note.getPojo();
}
async function importAttachmentsToNote(req: Request) {
async function importAttachmentsToNote(req: AppRequest) {
const { parentNoteId } = req.params;
const { taskId, last } = req.body;
@ -104,7 +105,7 @@ async function importAttachmentsToNote(req: Request) {
shrinkImages: req.body.shrinkImages !== 'false',
};
const file = (req as any).file;
const file = req.file;
if (!file) {
throw new ValidationError("No file has been uploaded");

View File

@ -13,8 +13,9 @@ import sql = require('../../services/sql');
import ws = require('../../services/ws');
import etapiTokenService = require('../../services/etapi_tokens');
import { Request } from 'express';
import { AppRequest } from '../route-interface';
function loginSync(req: Request) {
function loginSync(req: AppRequest) {
if (!sqlInit.schemaExists()) {
return [500, { message: "DB schema does not exist, can't sync." }];
}
@ -45,7 +46,7 @@ function loginSync(req: Request) {
return [400, { message: "Sync login credentials are incorrect. It looks like you're trying to sync two different initialized documents which is not possible." }];
}
(req as any).session.loggedIn = true;
req.session.loggedIn = true;
return {
instanceId: instanceId,

View File

@ -6,13 +6,17 @@ import noteService = require('../../services/notes');
import sanitize_attribute_name = require('../../services/sanitize_attribute_name');
import specialNotesService = require('../../services/special_notes');
import { Request } from 'express';
import { AppRequest } from '../route-interface';
function uploadImage(req: Request) {
const file = (req as any).file;
function uploadImage(req: AppRequest) {
const file = req.file;
if (!["image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
return [400, `Unknown image type: ${file.mimetype}`];
}
if (typeof file.buffer === "string") {
return [400, "Invalid image content type."];
}
const uploadedImageType = imageType(file.buffer);
if (!uploadedImageType) {
@ -20,14 +24,10 @@ function uploadImage(req: Request) {
}
const originalName = `Sender image.${uploadedImageType.ext}`;
if (!req.headers["x-local-date"] || Array.isArray(req.headers["x-local-date"])) {
if (!req.headers["x-local-date"]) {
return [400, "Invalid local date"];
}
if (Array.isArray(req.headers["x-labels"])) {
return [400, "Invalid value type."];
}
const parentNote = specialNotesService.getInboxNote(req.headers['x-local-date']);
const { note, noteId } = imageService.saveImage(parentNote.noteId, file.buffer, originalName, true);

View File

@ -1,9 +1,12 @@
import { Request } from "express";
import { File } from "../services/import/common";
export interface AppRequest extends Request {
headers: {
authorization?: string;
"trilium-cred"?: string;
"x-local-date"?: string;
"x-labels"?: string;
}
session: {
loggedIn: boolean;
@ -13,4 +16,5 @@ export interface AppRequest extends Request {
};
regenerate: (callback: () => void) => void;
}
file: File;
}

View File

@ -28,7 +28,7 @@ interface OpmlOutline {
outline: OpmlOutline[];
}
async function importOpml(taskContext: TaskContext, fileBuffer: Buffer, parentNote: BNote) {
async function importOpml(taskContext: TaskContext, fileBuffer: string | Buffer, parentNote: BNote) {
const xml = await new Promise<OpmlXml>(function(resolve, reject)
{
parseString(fileBuffer, function (err: any, result: OpmlXml) {