-
Notifications
You must be signed in to change notification settings - Fork 643
Expand file tree
/
Copy pathMediaType.js
More file actions
244 lines (238 loc) · 8.33 KB
/
MediaType.js
File metadata and controls
244 lines (238 loc) · 8.33 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Determines the type of media the url string is.
returns an object with .type and .id
You can add new media types by adding a regex
to match and the media class name to use to
render the media
The image_only parameter indicates that the
call only wants an image-based media type
that can be resolved to an image URL.
================================================== */
import Image from "./types/Image"
import YouTube from "./types/YouTube"
import GoogleMap from "./types/GoogleMap"
import Blockquote from "./types/Blockquote"
import Wikipedia from "./types/Wikipedia"
import SoundCloud from "./types/SoundCloud"
import Vimeo from "./types/Vimeo"
import DailyMotion from "./types/DailyMotion"
import Vine from "./types/Vine"
import Twitter from "./types/Twitter"
import TwitterEmbed from "./types/TwitterEmbed"
import Flickr from "./types/Flickr"
import DocumentCloud from "./types/DocumentCloud"
import Instagram from "./types/Instagram"
import Profile from "./types/Profile"
import GoogleDoc from "./types/GoogleDoc"
import Spotify from "./types/Spotify"
import IFrame from "./types/IFrame"
import Imgur from "./types/Imgur"
import PDF from "./types/PDF"
import Audio from "./types/Audio"
import Video from "./types/Video"
import Wistia from "./types/Wistia"
import TikTok from "./types/TikTok"
/**
* Given a JavaScript Object for an event from a TimelineConfig,
* determine the appropriate subclass of Media which can handle creating and showing an
* embed in the "media" section of that event's slide.
*
* When the `image_only` argument is true, the input `url_or_text` will only be
* tested against patterns which are known to return images suitable for use as
* thumbnails and backgrounds. Media classes returned when image_only is true should
* implement the getImageURL() function
*
* @param {Object} m
* @param {Boolean} image_only
*
* @returns {Object} a JS object which represents the match, including a `type`, `name`,
* `match_str`, and `cls`. These are all string values, except `cls`, which
* is a JavaScript class which can be used to instantiate a media embed
* or thumbnail.
*/
export function lookupMediaType(m, image_only) {
var media = {},
media_types = [{
type: "youtube",
name: "YouTube",
match_str: "^(https?:)?\/*(www.)?youtube|youtu\.be",
cls: YouTube
},
{
type: "vimeo",
name: "Vimeo",
match_str: "^(https?:)?\/*(player.)?vimeo\.com",
cls: Vimeo
},
{
type: "dailymotion",
name: "DailyMotion",
match_str: "^(https?:)?\/*(www.)?dailymotion\.com",
cls: DailyMotion
},
{
type: "vine",
name: "Vine",
match_str: "^(https?:)?\/*(www.)?vine\.co",
cls: Vine
},
{
type: "soundcloud",
name: "SoundCloud",
match_str: "^(https?:)?\/*(player.)?soundcloud\.com",
cls: SoundCloud
},
{
type: "twitter",
name: "Twitter",
match_str: "^(https?:)?\/*(www.)?twitter\.com",
cls: Twitter
},
{
type: "twitterembed",
name: "TwitterEmbed",
match_str: "<blockquote class=['\"]twitter-tweet['\"]",
cls: TwitterEmbed
},
{
type: "googlemaps",
name: "Google Map",
match_str: /google.+?\/maps\/@([-\d.]+),([-\d.]+),((?:[-\d.]+[zmayht],?)*)|google.+?\/maps\/search\/([\w\W]+)\/@([-\d.]+),([-\d.]+),((?:[-\d.]+[zmayht],?)*)|google.+?\/maps\/place\/([\w\W]+)\/@([-\d.]+),([-\d.]+),((?:[-\d.]+[zmayht],?)*)|google.+?\/maps\/dir\/([\w\W]+)\/([\w\W]+)\/@([-\d.]+),([-\d.]+),((?:[-\d.]+[zmayht],?)*)/,
cls: GoogleMap
},
{
type: "flickr",
name: "Flickr",
match_str: "^(https?:)?\/*(www.)?flickr.com\/photos",
cls: Flickr
},
{
type: "flickr",
name: "Flickr",
match_str: "^(https?:\/\/)?flic.kr\/.*",
cls: Flickr
},
{
type: "instagram",
name: "Instagram",
match_str: /^(https?:)?\/*(www.)?(instagr.am|^(https?:)?\/*(www.)?instagram.com)\/p\//,
cls: Instagram
},
{
type: "profile",
name: "Profile",
match_str: /^(https?:)?\/*(www.)?instagr.am\/[a-zA-Z0-9]{2,}|^(https?:)?\/*(www.)?instagram.com\/[a-zA-Z0-9]{2,}/,
cls: Profile
},
{
type: "documentcloud",
name: "Document Cloud",
match_str: /documentcloud.org\//,
cls: DocumentCloud
},
{
type: "image",
name: "Image",
match_str: /(jpg|jpeg|png|gif|svg|webp)(\?.*)?$/i,
cls: Image
},
{
type: "imgur",
name: "Imgur",
match_str: /^.*imgur.com\/.+$|<blockquote class=['\"]imgur-embed-pub['\"]/i,
cls: Imgur
},
{
type: "googledocs",
name: "Google Doc",
match_str: "^(https?:)?\/*[^.]*.google.com\/[^\/]*\/d\/[^\/]*\/[^\/]*\?usp=sharing|^(https?:)?\/*drive.google.com\/open\?id=[^\&]*\&authuser=0|^(https?:)?\/\/*drive.google.com\/open\\?id=[^\&]*|^(https?:)?\/*[^.]*.googledrive.com\/host\/[^\/]*\/",
cls: GoogleDoc
},
{
type: "pdf",
name: "PDF",
match_str: /^.*\.pdf(\?.*)?(\#.*)?/,
cls: PDF
},
{
type: "wikipedia",
name: "Wikipedia",
match_str: "^(https?:)?\/*(www.)?wikipedia\.org|^(https?:)?\/*([a-z][a-z].)?wikipedia\.org",
cls: Wikipedia
},
{
type: "spotify",
name: "spotify",
match_str: "spotify",
cls: Spotify
},
{
type: "iframe",
name: "iFrame",
match_str: "iframe",
cls: IFrame
},
{
type: "blockquote",
name: "Quote",
match_str: "blockquote",
cls: Blockquote
},
{
type: "video",
name: "Video",
match_str: /(mp4|webm)(\?.*)?$/i,
cls: Video
},
{
type: "wistia",
name: "Wistia",
match_str: /https?:\/\/(.+)?(wistia\.com|wi\.st)\/.*/i,
cls: Wistia
},
{
type: "audio",
name: "Audio",
match_str: /(mp3|wav|m4a)(\?.*)?$/i,
cls: Audio
},
{
type: "tiktok",
name: "TikTok",
match_str: /https:\/\/www.tiktok.com\/@\S+?\/video\//,
cls: TikTok
},
{
type: "imageblank",
name: "Imageblank",
match_str: "",
cls: Image
}
]
if (image_only) {
if (m instanceof Array) {
return false;
}
for (var i = 0; i < media_types.length; i++) {
switch (media_types[i].type) {
case "flickr":
case "image":
case "instagram":
if (m.url.match(media_types[i].match_str)) {
media = media_types[i];
return media;
}
break;
default:
break;
}
}
} else {
for (var i = 0; i < media_types.length; i++) {
if (m.url.match(media_types[i].match_str)) {
return media_types[i];
}
}
}
return false;
}