1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-08 13:06:15 +02:00

Chore: Migrate mime-utils.js to TypeScript (#10536)

This commit is contained in:
Henry Heino 2024-06-04 01:50:18 -07:00 committed by GitHub
parent 32710e44c3
commit 96850b7b98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 72 additions and 63 deletions

View File

@ -874,6 +874,7 @@ packages/lib/markdownUtils.js
packages/lib/markdownUtils2.test.js
packages/lib/markupLanguageUtils.js
packages/lib/migrations/42.js
packages/lib/mime-utils.js
packages/lib/models/Alarm.js
packages/lib/models/BaseItem.test.js
packages/lib/models/BaseItem.js

1
.gitignore vendored
View File

@ -853,6 +853,7 @@ packages/lib/markdownUtils.js
packages/lib/markdownUtils2.test.js
packages/lib/markupLanguageUtils.js
packages/lib/migrations/42.js
packages/lib/mime-utils.js
packages/lib/models/Alarm.js
packages/lib/models/BaseItem.test.js
packages/lib/models/BaseItem.js

View File

@ -15,7 +15,7 @@ import markupRenderOptions from './markupRenderOptions';
import { fileExtension, filename, safeFileExtension, safeFilename } from '@joplin/utils/path';
const joplinRendererUtils = require('@joplin/renderer').utils;
const { clipboard } = require('electron');
const mimeUtils = require('@joplin/lib/mime-utils.js').mime;
import * as mimeUtils from '@joplin/lib/mime-utils';
const md5 = require('md5');
const path = require('path');

View File

@ -24,7 +24,7 @@ import NavService, { OnNavigateCallback as OnNavigateCallback } from '@joplin/li
import BaseModel, { ModelType } from '@joplin/lib/BaseModel';
import ActionButton from '../ActionButton';
const { fileExtension, safeFileExtension } = require('@joplin/lib/path-utils');
const mimeUtils = require('@joplin/lib/mime-utils.js').mime;
import * as mimeUtils from '@joplin/lib/mime-utils';
import ScreenHeader, { MenuOptionType } from '../ScreenHeader';
import NoteTagsDialog from './NoteTagsDialog';
import time from '@joplin/lib/time';

View File

@ -7,7 +7,7 @@ const FsDriverRN = require('./fs-driver/fs-driver-rn').default;
const { Buffer } = require('buffer');
const { Linking, Platform } = require('react-native');
const showMessageBox = require('./showMessageBox.js').default;
const mimeUtils = require('@joplin/lib/mime-utils.js').mime;
const mimeUtils = require('@joplin/lib/mime-utils.js');
const { basename, fileExtension } = require('@joplin/lib/path-utils');
const uuid = require('@joplin/lib/uuid').default;
const Resource = require('@joplin/lib/models/Resource').default;

View File

@ -16,7 +16,7 @@ const { enexXmlToHtml } = require('./import-enex-html-gen.js');
const md5 = require('md5');
const { Base64Decode } = require('base64-stream');
const md5File = require('md5-file');
const { mime } = require('./mime-utils');
import * as mime from './mime-utils';
// const Promise = require('promise');
const fs = require('fs-extra');

View File

@ -1,50 +0,0 @@
const mimeTypes = require('./mime-utils-types');
const mime = {
fromFileExtension(ext) {
ext = ext.toLowerCase();
for (let i = 0; i < mimeTypes.length; i++) {
const t = mimeTypes[i];
if (t.e.indexOf(ext) >= 0) {
return t.t;
}
}
return null;
},
fromFilename(name) {
if (!name) return null;
const splitted = name.trim().split('.');
if (splitted.length <= 1) return null;
return mime.fromFileExtension(splitted[splitted.length - 1]);
},
toFileExtension(mimeType) {
mimeType = mimeType.toLowerCase();
for (let i = 0; i < mimeTypes.length; i++) {
const t = mimeTypes[i];
if (mimeType === t.t) {
// Return the first file extension that is 3 characters long
// If none exist return the first one in the list.
for (let j = 0; j < t.e.length; j++) {
if (t.e[j].length === 3) return t.e[j];
}
return t.e[0];
}
}
return null;
},
fromDataUrl(dataUrl) {
// Example: data:image/jpeg;base64,/9j/4AAQSkZJR.....
const defaultMime = 'text/plain';
const p = dataUrl.substr(0, dataUrl.indexOf(',')).split(';');
let s = p[0];
s = s.split(':');
if (s.length <= 1) return defaultMime;
s = s[1];
return s.indexOf('/') >= 0 ? s : defaultMime; // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
},
};
module.exports = { mime };

View File

@ -0,0 +1,46 @@
const mimeTypes = require('./mime-utils-types');
export const fromFileExtension = (ext: string) => {
ext = ext.toLowerCase();
for (let i = 0; i < mimeTypes.length; i++) {
const t = mimeTypes[i];
if (t.e.indexOf(ext) >= 0) {
return t.t;
}
}
return null;
};
export const fromFilename = (name: string) => {
if (!name) return null;
const splitted = name.trim().split('.');
if (splitted.length <= 1) return null;
return fromFileExtension(splitted[splitted.length - 1]);
};
export const toFileExtension = (mimeType: string) => {
mimeType = mimeType.toLowerCase();
for (let i = 0; i < mimeTypes.length; i++) {
const t = mimeTypes[i];
if (mimeType === t.t) {
// Return the first file extension that is 3 characters long
// If none exist return the first one in the list.
for (let j = 0; j < t.e.length; j++) {
if (t.e[j].length === 3) return t.e[j];
}
return t.e[0];
}
}
return null;
};
export const fromDataUrl = (dataUrl: string) => {
// Example: data:image/jpeg;base64,/9j/4AAQSkZJR.....
const defaultMime = 'text/plain';
const p = dataUrl.substr(0, dataUrl.indexOf(',')).split(';');
let s = p[0];
const result = s.split(':');
if (result.length <= 1) return defaultMime;
s = result[1];
return s.indexOf('/') >= 0 ? s : defaultMime; // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
};

View File

@ -1,6 +1,6 @@
const mimeUtils = require('./mime-utils.js').mime;
const mimeUtils = require('./mime-utils.js');
describe('mimeUils', () => {
describe('mimeUtils', () => {
@ -18,4 +18,15 @@ describe('mimeUils', () => {
expect(mimeUtils.fromFilename('test')).toBe(null);
}));
it.each([
['Not a data URL.', 'text/plain'],
// No encoding type
['data:image/svg+xml,%3csvg%3e%3cpath fill=\'%23fff\' d=\'M1,2,3z\'/%3e%3c/svg%3e', 'image/svg+xml'],
// Shouldn't need a full data URL
['data:image/jpeg;base64,/9j/4AAQSkZJR...', 'image/jpeg'],
['data:image/png;base64,iVBORw0KGgoAAAANSUhEU...', 'image/png'],
])('should get MIME types from data URLs (case %#)', (url, expected) => {
expect(mimeUtils.fromDataUrl(url)).toBe(expected);
});
});

View File

@ -9,7 +9,7 @@ import { ResourceEntity, ResourceLocalStateEntity, ResourceOcrStatus, SqlQuery }
import ResourceLocalState from './ResourceLocalState';
import * as pathUtils from '../path-utils';
import { safeFilename } from '../path-utils';
const { mime } = require('../mime-utils.js');
import * as mime from '../mime-utils';
const { FsDriverDummy } = require('../fs-driver-dummy.js');
import JoplinError from '../JoplinError';
import itemCanBeEncrypted from './utils/itemCanBeEncrypted';

View File

@ -1,5 +1,5 @@
import type { ResourceEntity } from '../../services/database/types';
const { mime } = require('../../mime-utils.js');
import * as mime from '../../mime-utils';
import { filename } from '@joplin/utils/path';
// This file contains resource-related utilities that do not

View File

@ -16,7 +16,7 @@ import Tag from '../../../models/Tag';
import Resource from '../../../models/Resource';
import htmlUtils from '../../../htmlUtils';
import markupLanguageUtils from '../../../markupLanguageUtils';
const mimeUtils = require('../../../mime-utils.js').mime;
import * as mimeUtils from '../../../mime-utils';
const md5 = require('md5');
import HtmlToMd from '../../../HtmlToMd';
const urlUtils = require('../../../urlUtils.js');

View File

@ -14,7 +14,7 @@ import replaceUnsupportedCharacters from './utils/replaceUnsupportedCharacters';
import { FetchBlobOptions } from './types';
import FileApiDriverLocal from './file-api-driver-local';
const mimeUtils = require('./mime-utils.js').mime;
import * as mimeUtils from './mime-utils';
const { _ } = require('./locale');
const http = require('http');
const https = require('https');

View File

@ -16,7 +16,7 @@ import { msleep } from '../utils/time';
import Logger, { LoggerWrapper } from '@joplin/utils/Logger';
import prettyBytes = require('pretty-bytes');
const mimeUtils = require('@joplin/lib/mime-utils.js').mime;
import * as mimeUtils from '@joplin/lib/mime-utils';
// Converts "root:/myfile.txt:" to "myfile.txt"
const extractNameRegex = /^root:\/(.*):$/;

View File

@ -7,7 +7,7 @@ import * as fs from 'fs-extra';
import { AppContext, RouteType } from '../utils/types';
import { localFileFromUrl } from '../utils/joplinUtils';
import { homeUrl, loginUrl } from '../utils/urlUtils';
const { mime } = require('@joplin/lib/mime-utils.js');
import * as mime from '@joplin/lib/mime-utils';
const publicDir = `${dirname(dirname(__dirname))}/public`;

View File

@ -7,7 +7,7 @@ const { patreonOauthToken } = require('./tool-utils');
const HtmlToMd = require('@joplin/lib/HtmlToMd').default;
const { dirname, filename, basename } = require('@joplin/lib/path-utils');
const markdownUtils = require('@joplin/lib/markdownUtils').default;
const mimeUtils = require('@joplin/lib/mime-utils.js').mime;
const mimeUtils = require('@joplin/lib/mime-utils.js');
const { mimeTypeFromHeaders } = require('@joplin/lib/net-utils');
const shim = require('@joplin/lib/shim').default;
const moment = require('moment');