forked from Hubs-Foundation/hubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject-types.js
111 lines (104 loc) · 3.63 KB
/
object-types.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Enumeration of spawned object content origins. URL means the content is
// fetched from a URL, FILE means it was an uploaded/dropped file, and
// CLIPBOARD means it was raw data pasted in from the clipboard, SPAWNER means
// it was copied over from a spawner.
export const ObjectContentOrigins = {
URL: 1,
FILE: 2,
CLIPBOARD: 3,
SPAWNER: 4
};
// Enumeration of spawnable object types, used for telemetry, which encapsulates
// both the origin of the content for the object and also the type of content
// contained in the object.
export const ObjectTypes = {
URL_IMAGE: 0,
URL_VIDEO: 1,
URL_MODEL: 2,
URL_PDF: 3,
URL_AUDIO: 4,
//URL_TEXT: 5,
//URL_TELEPORTER: 6,
FILE_IMAGE: 8,
FILE_VIDEO: 9,
FILE_MODEL: 10,
FILE_PDF: 11,
FILE_AUDIO: 12,
//FILE_TEXT: 13,
CLIPBOARD_IMAGE: 16,
CLIPBOARD_VIDEO: 17,
CLIPBOARD_MODEL: 18,
CLIPBOARD_PDF: 19,
CLIPBOARD_AUDIO: 20,
//CLIPBOARD_TEXT: 21,
SPAWNER_IMAGE: 24,
SPAWNER_VIDEO: 25,
SPAWNER_MODEL: 26,
SPAWNER_PDF: 27,
SPAWNER_AUDIO: 28,
//SPAWNER_TEXT: 29,
CAMERA: 30,
UNKNOWN: 31
};
// Given an origin and three object type values for URL, FILE, and CLIPBOARD
// origins respectively, return the appropriate one
function objectTypeForOrigin(contentOrigin, urlType, fileType, clipboardType, spawnerType) {
if (contentOrigin === ObjectContentOrigins.URL) {
return urlType;
} else if (contentOrigin === ObjectContentOrigins.FILE) {
return fileType;
} else if (contentOrigin === ObjectContentOrigins.CLIPBOARD) {
return clipboardType;
} else {
return spawnerType;
}
}
// Lookup table of mime-type prefixes to the set of object types that we should use
// for objects spawned matching their underlying Content-Type.
const objectTypeMimePrefixLookupMap = {
"image/": [ObjectTypes.URL_IMAGE, ObjectTypes.FILE_IMAGE, ObjectTypes.CLIPBOARD_IMAGE, ObjectTypes.SPAWNER_IMAGE],
"model/": [ObjectTypes.URL_MODEL, ObjectTypes.FILE_MODEL, ObjectTypes.CLIPBOARD_MODEL, ObjectTypes.SPAWNER_MODEL],
"application/x-zip-compressed": [
ObjectTypes.URL_MODEL,
ObjectTypes.FILE_MODEL,
ObjectTypes.CLIPBOARD_MODEL,
ObjectTypes.SPAWNER_MODEL
],
"video/": [ObjectTypes.URL_VIDEO, ObjectTypes.FILE_VIDEO, ObjectTypes.CLIPBOARD_VIDEO, ObjectTypes.SPAWNER_VIDEO],
"application/vnd.apple.mpegurl": [
ObjectTypes.URL_VIDEO,
ObjectTypes.FILE_VIDEO,
ObjectTypes.CLIPBOARD_VIDEO,
ObjectTypes.SPAWNER_VIDEO
],
"application/x-mpegurl": [
ObjectTypes.URL_VIDEO,
ObjectTypes.FILE_VIDEO,
ObjectTypes.CLIPBOARD_VIDEO,
ObjectTypes.SPAWNER_VIDEO
],
"audio/": [ObjectTypes.URL_AUDIO, ObjectTypes.FILE_AUDIO, ObjectTypes.CLIPBOARD_AUDIO, ObjectTypes.SPAWNER_AUDIO],
"application/pdf": [ObjectTypes.URL_PDF, ObjectTypes.FILE_PDF, ObjectTypes.CLIPBOARD_PDF, ObjectTypes.SPAWNER_PDF]
};
const srcSuffixLookupMap = {
".m3u8": [ObjectTypes.URL_VIDEO, ObjectTypes.FILE_VIDEO, ObjectTypes.CLIPBOARD_VIDEO, ObjectTypes.SPAWNER_VIDEO]
};
// Given an content origin and the resolved mime type of a piece of content, return
// the ObjectType, if any, for that content.
export function objectTypeForOriginAndContentType(contentOrigin, contentType, src) {
for (const prefix in objectTypeMimePrefixLookupMap) {
if (contentType.toLowerCase().startsWith(prefix)) {
const types = objectTypeMimePrefixLookupMap[prefix];
return objectTypeForOrigin(contentOrigin, ...types);
}
}
if (src) {
for (const suffix in srcSuffixLookupMap) {
if (src.toLowerCase().endsWith(suffix)) {
const types = srcSuffixLookupMap[suffix];
return objectTypeForOrigin(contentOrigin, ...types);
}
}
}
return ObjectTypes.UNKNOWN;
}