summaryrefslogtreecommitdiff
path: root/includes/external/addressbook/node_modules/open-graph-scraper/dist/lib/media.js
blob: c2dc8858a4b213e4f6247826e4323d032afbfc1e (plain)
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mediaSetup = void 0;
const fields_1 = require("./fields");
const mediaMapperTwitterImage = (item) => ({
    alt: item[3],
    height: item[2],
    url: item[0],
    width: item[1],
});
const mediaMapperTwitterPlayer = (item) => ({
    height: item[2],
    stream: item[3],
    url: item[0],
    width: item[1],
});
const mediaMapperMusicSong = (item) => ({
    disc: item[2],
    track: item[1],
    url: item[0],
});
const mediaMapper = (item) => ({
    height: item[2],
    type: item[3],
    url: item[0],
    width: item[1],
});
const mediaSorter = (a, b) => {
    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, b) => {
    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, ...args) => {
    if (array === undefined)
        return [];
    return array
        .map((value, idx) => [value, ...args.map((arr) => arr[idx])]);
};
/**
 * formats the multiple media values
 *
 * @param {object} ogObject - the current ogObject
 * @param {object} options - options for ogs
 * @return {object} object with ogs results with updated media values
 *
 */
function mediaSetup(ogObject, options) {
    // sets ogImage image/width/height/type to null if one these exists
    if (ogObject.ogImage || ogObject.ogImageWidth || ogObject.ogImageHeight || 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 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 these 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_1.default.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;
}
exports.mediaSetup = mediaSetup;
exports.default = mediaSetup;