-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmedia.ts
159 lines (137 loc) · 6.42 KB
/
media.ts
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
import { fields } from './fields';
const mediaMapperTwitterImage = (item: any[]) => ({
url: item[0],
width: item[1],
height: item[2],
alt: item[3],
});
const mediaMapperTwitterPlayer = (item: any[]) => ({
url: item[0],
width: item[1],
height: item[2],
stream: item[3],
});
const mediaMapperMusicSong = (item: any[]) => ({
url: item[0],
track: item[1],
disc: item[2],
});
const mediaMapper = (item: any[]) => ({
url: item[0],
width: item[1],
height: item[2],
type: item[3],
});
const mediaSorter = (a: { url: string; width: number; height: number }, b: { url: string; width: number; height: number }) => {
if (!(a.url && b.url)) {
return 0;
}
const aRes = a.url.match(/\.(\w{2,5})$/);
const aExt = (aRes && aRes[1].toLowerCase()) || null;
const bRes = b.url.match(/\.(\w{2,5})$/);
const bExt = (bRes && bRes[1].toLowerCase()) || null;
if (aExt === 'gif' && bExt !== 'gif') {
return -1;
}
if (aExt !== 'gif' && bExt === 'gif') {
return 1;
}
return Math.max(b.width, b.height) - Math.max(a.width, a.height);
};
const mediaSorterMusicSong = (a: { track: number; disc: number }, b: { track: number; disc: number }) => {
if (!(a.track && b.track)) {
return 0;
}
if (a.disc > b.disc) {
return 1;
}
if (a.disc < b.disc) {
return -1;
}
return a.track - b.track;
};
// lodash zip replacement
const zip = (array: any[], ...args: any[]) => {
if (array === undefined) return [];
return array.map((value, idx) => [value, ...args.map((arr) => arr[idx])]);
};
/*
* media setup
* @param string ogObject - return open graph info
* @param string options - options the user has set
* @param function callback
*/
export function mediaSetup(
ogObject: any,
options: {
allMedia?: boolean;
customMetaTags?: ConcatArray<{ multiple: boolean; property: string; fieldName: string }>;
onlyGetOpenGraphInfo?: any;
},
) {
// sets ogImage image/width/height/type to null if one This exists
if (ogObject.ogImage || ogObject.ogImageWidth || ogObject.twitterImageHeight || ogObject.ogImageType) {
ogObject.ogImage = ogObject.ogImage ? ogObject.ogImage : [null];
ogObject.ogImageWidth = ogObject.ogImageWidth ? ogObject.ogImageWidth : [null];
ogObject.ogImageHeight = ogObject.ogImageHeight ? ogObject.ogImageHeight : [null];
ogObject.ogImageType = ogObject.ogImageType ? ogObject.ogImageType : [null];
}
// format images
const ogImages = zip(ogObject.ogImage, ogObject.ogImageWidth, ogObject.ogImageHeight, ogObject.ogImageType).map(mediaMapper).sort(mediaSorter);
// sets ogVideo video/width/height/type to null if one these exists
if (ogObject.ogVideo || ogObject.ogVideoWidth || ogObject.ogVideoHeight || ogObject.ogVideoType) {
ogObject.ogVideo = ogObject.ogVideo ? ogObject.ogVideo : [null];
ogObject.ogVideoWidth = ogObject.ogVideoWidth ? ogObject.ogVideoWidth : [null];
ogObject.ogVideoHeight = ogObject.ogVideoHeight ? ogObject.ogVideoHeight : [null];
ogObject.ogVideoType = ogObject.ogVideoType ? ogObject.ogVideoType : [null];
}
// format videos
const ogVideos = zip(ogObject.ogVideo, ogObject.ogVideoWidth, ogObject.ogVideoHeight, ogObject.ogVideoType).map(mediaMapper).sort(mediaSorter);
// sets twitter image/width/height/type to null if one these exists
if (ogObject.twitterImageSrc || ogObject.twitterImage || ogObject.twitterImageWidth || ogObject.twitterImageHeight || ogObject.twitterImageAlt) {
ogObject.twitterImageSrc = ogObject.twitterImageSrc ? ogObject.twitterImageSrc : [null];
ogObject.twitterImage = ogObject.twitterImage ? ogObject.twitterImage : ogObject.twitterImageSrc; // deafult to twitterImageSrc
ogObject.twitterImageWidth = ogObject.twitterImageWidth ? ogObject.twitterImageWidth : [null];
ogObject.twitterImageHeight = ogObject.twitterImageHeight ? ogObject.twitterImageHeight : [null];
ogObject.twitterImageAlt = ogObject.twitterImageAlt ? ogObject.twitterImageAlt : [null];
}
// format twitter images
const twitterImages = zip(ogObject.twitterImage, ogObject.twitterImageWidth, ogObject.twitterImageHeight, ogObject.twitterImageAlt).map(mediaMapperTwitterImage).sort(mediaSorter);
// sets twitter player/width/height/stream to null if one these exists
if (ogObject.twitterPlayer || ogObject.twitterPlayerWidth || ogObject.twitterPlayerHeight || ogObject.twitterPlayerStream) {
ogObject.twitterPlayer = ogObject.twitterPlayer ? ogObject.twitterPlayer : [null];
ogObject.twitterPlayerWidth = ogObject.twitterPlayerWidth ? ogObject.twitterPlayerWidth : [null];
ogObject.twitterPlayerHeight = ogObject.twitterPlayerHeight ? ogObject.twitterPlayerHeight : [null];
ogObject.twitterPlayerStream = ogObject.twitterPlayerStream ? ogObject.twitterPlayerStream : [null];
}
// format twitter player
const twitterPlayers = zip(ogObject.twitterPlayer, ogObject.twitterPlayerWidth, ogObject.twitterPlayerHeight, ogObject.twitterPlayerStream).map(mediaMapperTwitterPlayer).sort(mediaSorter);
// sets music song/songTrack/songDisc to null if one This exists
if (ogObject.musicSong || ogObject.musicSongTrack || ogObject.musicSongDisc) {
ogObject.musicSong = ogObject.musicSong ? ogObject.musicSong : [null];
ogObject.musicSongTrack = ogObject.musicSongTrack ? ogObject.musicSongTrack : [null];
ogObject.musicSongDisc = ogObject.musicSongDisc ? ogObject.musicSongDisc : [null];
}
// format music songs
const musicSongs = zip(ogObject.musicSong, ogObject.musicSongTrack, ogObject.musicSongDisc).map(mediaMapperMusicSong).sort(mediaSorterMusicSong);
// remove old values since everything will live under the main property
fields
.filter((item) => item.multiple && item.fieldName && item.fieldName.match('(ogImage|ogVideo|twitter|musicSong).*'))
.forEach((item) => {
delete ogObject[item.fieldName];
});
if (options?.allMedia) {
if (ogImages.length) ogObject.ogImage = ogImages;
if (ogVideos.length) ogObject.ogVideo = ogVideos;
if (twitterImages.length) ogObject.twitterImage = twitterImages;
if (twitterPlayers.length) ogObject.twitterPlayer = twitterPlayers;
if (musicSongs.length) ogObject.musicSong = musicSongs;
} else {
if (ogImages.length) [ogObject.ogImage] = ogImages;
if (ogVideos.length) [ogObject.ogVideo] = ogVideos;
if (twitterImages.length) [ogObject.twitterImage] = twitterImages;
if (twitterPlayers.length) [ogObject.twitterPlayer] = twitterPlayers;
if (musicSongs.length) [ogObject.musicSong] = musicSongs;
}
return ogObject;
}