1
1
mirror of https://github.com/Fabio286/antares.git synced 2025-06-05 21:59:22 +02:00

refactor: add ciaplu for pattern matching in language detection and MIME type resolution

This commit is contained in:
2025-02-05 15:34:01 +01:00
parent 0479e5307c
commit 7af44d4a2c
4 changed files with 47 additions and 56 deletions

12
package-lock.json generated
View File

@@ -22,6 +22,7 @@
"babel-loader": "~8.2.3",
"better-sqlite3": "~10.0.0",
"chalk": "~4.1.2",
"ciaplu": "^2.2.0",
"cpu-features": "^0.0.10",
"cross-env": "~7.0.2",
"css-loader": "~6.5.0",
@@ -5608,6 +5609,12 @@
"node": ">=8"
}
},
"node_modules/ciaplu": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/ciaplu/-/ciaplu-2.2.0.tgz",
"integrity": "sha512-7y8s0GMFpIKqX2kwiOEYbaX3P9tPIbX4x41uw8GZjkZ+y0QZrpY3PFjE2Ed6BOeFxcCWi7b85MYHeiRrVrlSOQ==",
"license": "MIT"
},
"node_modules/clean-css": {
"version": "5.3.2",
"license": "MIT",
@@ -20345,6 +20352,11 @@
"version": "3.9.0",
"dev": true
},
"ciaplu": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/ciaplu/-/ciaplu-2.2.0.tgz",
"integrity": "sha512-7y8s0GMFpIKqX2kwiOEYbaX3P9tPIbX4x41uw8GZjkZ+y0QZrpY3PFjE2Ed6BOeFxcCWi7b85MYHeiRrVrlSOQ=="
},
"clean-css": {
"version": "5.3.2",
"requires": {

View File

@@ -131,6 +131,7 @@
"babel-loader": "~8.2.3",
"better-sqlite3": "~10.0.0",
"chalk": "~4.1.2",
"ciaplu": "^2.2.0",
"cpu-features": "^0.0.10",
"cross-env": "~7.0.2",
"css-loader": "~6.5.0",

View File

@@ -1,3 +1,5 @@
import { match } from 'ciaplu';
function isJSON (str: string) {
try {
if (!['{', '['].includes(str.trim()[0]))
@@ -176,17 +178,13 @@ function isMD (str: string) {
}
export function langDetector (str: string) {
if (!str || !str.trim().length)
return 'text';
if (isJSON(str))
return 'json';
if (isHTML(str))
return 'html';
if (isSVG(str))
return 'svg';
if (isXML(str))
return 'xml';
if (isMD(str))
return 'markdown';
return 'text';
return match(str)
.when(() => !str || !str.trim().length, () => 'text')
.when(isJSON, () => 'json')
.when(isHTML, () => 'html')
.when(isSVG, () => 'svg')
.when(isXML, () => 'xml')
.when(isMD, () => 'markdown')
.otherwise(() => 'text')
.return();
}

View File

@@ -1,45 +1,25 @@
import { match } from 'ciaplu';
export function mimeFromHex (hex: string) {
switch (hex.substring(0, 4)) { // 2 bytes
case '424D':
return { ext: 'bmp', mime: 'image/bmp' };
case '1F8B':
return { ext: 'tar.gz', mime: 'application/gzip' };
case '0B77':
return { ext: 'ac3', mime: 'audio/vnd.dolby.dd-raw' };
case '7801':
return { ext: 'dmg', mime: 'application/x-apple-diskimage' };
case '4D5A':
return { ext: 'exe', mime: 'application/x-msdownload' };
case '1FA0':
case '1F9D':
return { ext: 'Z', mime: 'application/x-compress' };
default:
switch (hex.substring(0, 6)) { // 3 bytes
case 'FFD8FF':
return { ext: 'jpg', mime: 'image/jpeg' };
case '4949BC':
return { ext: 'jxr', mime: 'image/vnd.ms-photo' };
case '425A68':
return { ext: 'bz2', mime: 'application/x-bzip2' };
default:
switch (hex) { // 4 bites
case '89504E47':
return { ext: 'png', mime: 'image/png' };
case '47494638':
return { ext: 'gif', mime: 'image/gif' };
case '25504446':
return { ext: 'pdf', mime: 'application/pdf' };
case '504B0304':
return { ext: 'zip', mime: 'application/zip' };
case '425047FB':
return { ext: 'bpg', mime: 'image/bpg' };
case '4D4D002A':
return { ext: 'tif', mime: 'image/tiff' };
case '00000100':
return { ext: 'ico', mime: 'image/x-icon' };
default:
return { ext: '', mime: 'unknown ' + hex };
}
}
}
return match(hex.substring(0, 4)) // 2 bytes
.with('424D', () => ({ ext: 'bmp', mime: 'image/bmp' }))
.with('1F8B', () => ({ ext: 'tar.gz', mime: 'application/gzip' }))
.with('0B77', () => ({ ext: 'ac3', mime: 'audio/vnd.dolby.dd-raw' }))
.with('7801', () => ({ ext: 'dmg', mime: 'application/x-apple-diskimage' }))
.with('4D5A', () => ({ ext: 'exe', mime: 'application/x-msdownload' }))
.when((val) => ['1FA0', '1F9D'].includes(val), () => ({ ext: 'Z', mime: 'application/x-compress' }))
.extracting(() => hex.substring(0, 6)) // 3 bytes
.with('FFD8FF', () => ({ ext: 'jpg', mime: 'image/jpeg' }))
.with('4949BC', () => ({ ext: 'jxr', mime: 'image/vnd.ms-photo' }))
.with('425A68', () => ({ ext: 'bz2', mime: 'application/x-bzip2' }))
.extracting(() => hex) // 4 bytes
.with('89504E47', () => ({ ext: 'png', mime: 'image/png' }))
.with('47494638', () => ({ ext: 'gif', mime: 'image/gif' }))
.with('25504446', () => ({ ext: 'pdf', mime: 'application/pdf' }))
.with('504B0304', () => ({ ext: 'zip', mime: 'application/zip' }))
.with('425047FB', () => ({ ext: 'bpg', mime: 'image/bpg' }))
.with('4D4D002A', () => ({ ext: 'tif', mime: 'image/tiff' }))
.with('00000100', () => ({ ext: 'ico', mime: 'image/x-icon' }))
.otherwise(() => ({ ext: '', mime: 'unknown ' + hex }))
.return();
}