Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/vs/base/common/mime.ts
13405 views
1
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'
2
3
/*---------------------------------------------------------------------------------------------
4
* Copyright (c) Microsoft Corporation. All rights reserved.
5
* Licensed under the MIT License. See License.txt in the project root for license information.
6
*--------------------------------------------------------------------------------------------*/
7
8
import { extname } from './path';
9
10
export const Mimes = Object.freeze({
11
text: 'text/plain',
12
binary: 'application/octet-stream',
13
unknown: 'application/unknown',
14
markdown: 'text/markdown',
15
latex: 'text/latex',
16
uriList: 'text/uri-list',
17
html: 'text/html',
18
});
19
20
interface MapExtToMediaMimes {
21
[index: string]: string | string[];
22
}
23
24
const mapExtToTextMimes: Record<string, string> = {
25
'.css': 'text/css',
26
'.csv': 'text/csv',
27
'.htm': 'text/html',
28
'.html': 'text/html',
29
'.ics': 'text/calendar',
30
'.js': 'text/javascript',
31
'.mjs': 'text/javascript',
32
'.txt': 'text/plain',
33
'.xml': 'text/xml'
34
};
35
36
// Known media mimes that we can handle
37
const mapExtToMediaMimes: MapExtToMediaMimes = {
38
'.aac': 'audio/x-aac',
39
'.avi': 'video/x-msvideo',
40
'.bmp': 'image/bmp',
41
'.flv': 'video/x-flv',
42
'.gif': 'image/gif',
43
'.ico': 'image/x-icon',
44
'.jpe': ['image/jpg', 'image/jpeg'],
45
'.jpeg': ['image/jpg', 'image/jpeg'],
46
'.jpg': ['image/jpg', 'image/jpeg'],
47
'.m1v': 'video/mpeg',
48
'.m2a': 'audio/mpeg',
49
'.m2v': 'video/mpeg',
50
'.m3a': 'audio/mpeg',
51
'.mid': 'audio/midi',
52
'.midi': 'audio/midi',
53
'.mk3d': 'video/x-matroska',
54
'.mks': 'video/x-matroska',
55
'.mkv': 'video/x-matroska',
56
'.mov': 'video/quicktime',
57
'.movie': 'video/x-sgi-movie',
58
'.mp2': 'audio/mpeg',
59
'.mp2a': 'audio/mpeg',
60
'.mp3': 'audio/mpeg',
61
'.mp4': 'video/mp4',
62
'.mp4a': 'audio/mp4',
63
'.mp4v': 'video/mp4',
64
'.mpe': 'video/mpeg',
65
'.mpeg': 'video/mpeg',
66
'.mpg': 'video/mpeg',
67
'.mpg4': 'video/mp4',
68
'.mpga': 'audio/mpeg',
69
'.oga': 'audio/ogg',
70
'.ogg': 'audio/ogg',
71
'.opus': 'audio/opus',
72
'.ogv': 'video/ogg',
73
'.png': 'image/png',
74
'.psd': 'image/vnd.adobe.photoshop',
75
'.qt': 'video/quicktime',
76
'.spx': 'audio/ogg',
77
'.svg': 'image/svg+xml',
78
'.tga': 'image/x-tga',
79
'.tif': 'image/tiff',
80
'.tiff': 'image/tiff',
81
'.wav': 'audio/x-wav',
82
'.webm': 'video/webm',
83
'.webp': 'image/webp',
84
'.wma': 'audio/x-ms-wma',
85
'.wmv': 'video/x-ms-wmv',
86
'.woff': 'application/font-woff',
87
};
88
89
export function getMediaOrTextMime(path: string): string | undefined {
90
const ext = extname(path);
91
const textMime = mapExtToTextMimes[ext.toLowerCase()];
92
if (textMime !== undefined) {
93
return textMime;
94
} else {
95
return getMediaMime(path);
96
}
97
}
98
99
export function getMediaMime(path: string): string | undefined {
100
const ext = extname(path);
101
const mimeType = mapExtToMediaMimes[ext.toLowerCase()];
102
return Array.isArray(mimeType) ? mimeType[0] : mimeType;
103
}
104
105
export function getExtensionForMimeType(mimeType: string): string | undefined {
106
for (const extension in mapExtToMediaMimes) {
107
const value = mapExtToMediaMimes[extension];
108
if (Array.isArray(value) ? value.includes(mimeType) : value === mimeType) {
109
return extension;
110
}
111
}
112
113
return undefined;
114
}
115
116
const _simplePattern = /^(.+)\/(.+?)(;.+)?$/;
117
118
export function normalizeMimeType(mimeType: string): string;
119
export function normalizeMimeType(mimeType: string, strict: true): string | undefined;
120
export function normalizeMimeType(mimeType: string, strict?: true): string | undefined {
121
122
const match = _simplePattern.exec(mimeType);
123
if (!match) {
124
return strict
125
? undefined
126
: mimeType;
127
}
128
// https://datatracker.ietf.org/doc/html/rfc2045#section-5.1
129
// media and subtype must ALWAYS be lowercase, parameter not
130
return `${match[1].toLowerCase()}/${match[2].toLowerCase()}${match[3] ?? ''}`;
131
}
132
133
/**
134
* Whether the provided mime type is a text stream like `stdout`, `stderr`.
135
*/
136
export function isTextStreamMime(mimeType: string) {
137
return ['application/vnd.code.notebook.stdout', 'application/vnd.code.notebook.stderr'].includes(mimeType);
138
}
139
140