server-ts: Port services/import/mime

This commit is contained in:
Elian Doran 2024-02-25 07:57:17 +02:00
parent 052a0a44f2
commit fd37fd3a45
No known key found for this signature in database
3 changed files with 15 additions and 10 deletions

View File

@ -1,9 +1,9 @@
"use strict"; "use strict";
const mimeTypes = require('mime-types'); import mimeTypes = require('mime-types');
const path = require('path'); import path = require('path');
const CODE_MIME_TYPES = { const CODE_MIME_TYPES: Record<string, boolean | string> = {
'text/plain': true, 'text/plain': true,
'text/x-csrc': true, 'text/x-csrc': true,
'text/x-c++src': true, 'text/x-c++src': true,
@ -44,7 +44,7 @@ const CODE_MIME_TYPES = {
}; };
// extensions missing in mime-db // extensions missing in mime-db
const EXTENSION_TO_MIME = { const EXTENSION_TO_MIME: Record<string, string> = {
".c": "text/x-csrc", ".c": "text/x-csrc",
".cs": "text/x-csharp", ".cs": "text/x-csharp",
".clj": "text/x-clojure", ".clj": "text/x-clojure",
@ -65,7 +65,7 @@ const EXTENSION_TO_MIME = {
}; };
/** @returns false if MIME is not detected */ /** @returns false if MIME is not detected */
function getMime(fileName) { function getMime(fileName: string) {
if (fileName.toLowerCase() === 'dockerfile') { if (fileName.toLowerCase() === 'dockerfile') {
return "text/x-dockerfile"; return "text/x-dockerfile";
} }
@ -79,7 +79,12 @@ function getMime(fileName) {
return mimeTypes.lookup(fileName); return mimeTypes.lookup(fileName);
} }
function getType(options, mime) { interface GetTypeOpts {
textImportedAsText: boolean;
codeImportedAsCode: boolean;
}
function getType(options: GetTypeOpts, mime: string) {
mime = mime ? mime.toLowerCase() : ''; mime = mime ? mime.toLowerCase() : '';
if (options.textImportedAsText && (mime === 'text/html' || ['text/markdown', 'text/x-markdown'].includes(mime))) { if (options.textImportedAsText && (mime === 'text/html' || ['text/markdown', 'text/x-markdown'].includes(mime))) {
@ -96,7 +101,7 @@ function getType(options, mime) {
} }
} }
function normalizeMimeType(mime) { function normalizeMimeType(mime: string) {
mime = mime ? mime.toLowerCase() : ''; mime = mime ? mime.toLowerCase() : '';
if (!(mime in CODE_MIME_TYPES) || CODE_MIME_TYPES[mime] === true) { if (!(mime in CODE_MIME_TYPES) || CODE_MIME_TYPES[mime] === true) {
@ -107,7 +112,7 @@ function normalizeMimeType(mime) {
} }
} }
module.exports = { export = {
getMime, getMime,
getType, getType,
normalizeMimeType normalizeMimeType

View File

@ -4,7 +4,7 @@ const noteService = require('../../services/notes');
const imageService = require('../../services/image'); const imageService = require('../../services/image');
const protectedSessionService = require('../protected_session'); const protectedSessionService = require('../protected_session');
const markdownService = require('./markdown.js'); const markdownService = require('./markdown.js');
const mimeService = require('./mime.js'); const mimeService = require('./mime');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const importUtils = require('./utils'); const importUtils = require('./utils');
const htmlSanitizer = require('../html_sanitizer'); const htmlSanitizer = require('../html_sanitizer');

View File

@ -8,7 +8,7 @@ const attributeService = require('../../services/attributes');
const BBranch = require('../../becca/entities/bbranch'); const BBranch = require('../../becca/entities/bbranch');
const path = require('path'); const path = require('path');
const protectedSessionService = require('../protected_session'); const protectedSessionService = require('../protected_session');
const mimeService = require('./mime.js'); const mimeService = require('./mime');
const treeService = require('../tree'); const treeService = require('../tree');
const yauzl = require("yauzl"); const yauzl = require("yauzl");
const htmlSanitizer = require('../html_sanitizer'); const htmlSanitizer = require('../html_sanitizer');