Skip to content

Commit 779d10c

Browse files
committed
implement mime type normalization
Fixes #2
1 parent 46cd02f commit 779d10c

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ There are a few additional things that I want to point out in particular:
8686
- Pay attention to the naming system. If your tool is called `dummy`, then the class should be called `dummyHandler`, and the file should be called `dummy.ts`.
8787
- The handler is responsible for setting the output file's name. This is done to allow for flexibility in rare cases where the _full_ file name matters. Of course, in most cases, you'll only have to swap the file extension.
8888
- The handler is also responsible for ensuring that any byte buffers that enter or exit the handler _do not get mutated_. If necessary, clone the buffer by wrapping it in `new Uint8Array()`.
89+
- When handling MIME types, run them through [normalizeMimeType](src/normalizeMimeType.ts) first. One file can have multiple valid MIME types, which isn't great when you're trying to match them algorithmically.
8990

9091
### Adding dependencies
9192

src/handlers/FFmpeg.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
22

33
import { FFmpeg } from "@ffmpeg/ffmpeg";
44
import type { LogEvent } from "@ffmpeg/ffmpeg";
5+
56
import mime from "mime";
7+
import normalizeMimeType from "../normalizeMimeType.ts";
68

79
class FFmpegHandler implements FormatHandler {
810

@@ -130,6 +132,7 @@ class FFmpegHandler implements FormatHandler {
130132
extension = format;
131133
mimeType = mime.getType(format) || ("video/" + format);
132134
}
135+
mimeType = normalizeMimeType(mimeType);
133136

134137
this.supportedFormats.push({
135138
name: description + (formats.length > 1 ? (" / " + format) : ""),

src/handlers/ImageMagick.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "@imagemagick/magick-wasm";
88

99
import mime from "mime";
10+
import normalizeMimeType from "../normalizeMimeType.ts";
1011

1112
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
1213

@@ -37,7 +38,7 @@ class ImageMagickHandler implements FormatHandler {
3738
name: format.description,
3839
format: formatName,
3940
extension: formatName,
40-
mime: mimeType,
41+
mime: normalizeMimeType(mimeType),
4142
from: format.supportsReading,
4243
to: format.supportsWriting,
4344
internal: format.format

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { FileFormat, FileData, FormatHandler, ConvertPathNode } from "./FormatHandler.js";
2+
import normalizeMimeType from "./normalizeMimeType.js";
23
import handlers from "./handlers";
34

45
/** Files currently selected for conversion */
@@ -97,9 +98,8 @@ const fileSelectHandler = (event: Event) => {
9798
${files.length > 1 ? `<br>... and ${files.length - 1} more` : ""}
9899
</h2>`;
99100

100-
let mimeType = files[0].type;
101101
// Common MIME type adjustments (to match "mime" library)
102-
if (mimeType === "image/x-icon") mimeType = "image/vnd.microsoft.icon";
102+
let mimeType = normalizeMimeType(files[0].type);
103103

104104
// Search input formats by MIME type, or fall back to file extension.
105105
const fileExtension = files[0].name.split(".").pop();

src/normalizeMimeType.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function normalizeMimeType (mime: string) {
2+
switch (mime) {
3+
case "audio/x-wav": return "audio/wav";
4+
case "audio/vnd.wave": return "audio/wav";
5+
case "image/x-icon": return "image/vnd.microsoft.icon";
6+
}
7+
return mime;
8+
}
9+
10+
export default normalizeMimeType;

0 commit comments

Comments
 (0)