Skip to content

Commit

Permalink
fix: mimetypes (#572)
Browse files Browse the repository at this point in the history
* fix: mimetypes

* fix: mimetypes

* fix: mimetypes
  • Loading branch information
tsi authored Mar 7, 2024
1 parent 9389b0d commit 4fe8dfc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
32 changes: 27 additions & 5 deletions src/plugins/cloudinary/models/video-source/video-source.const.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,36 @@ export const VIDEO_SUFFIX_REMOVAL_PATTERN = RegExp(`\\.(${COMMON_VIDEO_EXTENSION
// eslint-disable-next-line no-control-regex
export const URL_PATTERN = RegExp('https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\+.~#?&/=]*)');


export const CONTAINER_MIME_TYPES = {
dash: ['application/dash+xml'],
hls: ['application/x-mpegURL'],
mpd: ['application/dash+xml'],
m3u8: ['application/x-mpegURL']
hls: 'application/x-mpegURL',
dash: 'application/dash+xml',

// See: https://docs.videojs.com/utils_mimetypes.js.html
opus: 'video/ogg',
ogv: 'video/ogg',
mp4: 'video/mp4',
mov: 'video/mp4',
m4v: 'video/mp4',
mkv: 'video/x-matroska',
m4a: 'audio/mp4',
mp3: 'audio/mpeg',
aac: 'audio/aac',
caf: 'audio/x-caf',
flac: 'audio/flac',
oga: 'audio/ogg',
wav: 'audio/wav',
m3u8: 'application/x-mpegURL',
mpd: 'application/dash+xml',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/gif',
png: 'image/png',
svg: 'image/svg+xml',
webp: 'image/webp'
};

export const ADAPTIVE_SOURCETYPES = ['hls', 'dash', 'mpd', 'm3u8'];

export const FORMAT_MAPPINGS = {
hls: 'm3u8',
dash: 'mpd'
Expand Down
13 changes: 8 additions & 5 deletions src/plugins/cloudinary/models/video-source/video-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { castArray } from 'utils/array';
import { SOURCE_TYPE } from 'utils/consts';
import {
CONTAINER_MIME_TYPES,
ADAPTIVE_SOURCETYPES,
DEFAULT_POSTER_PARAMS,
DEFAULT_VIDEO_PARAMS,
VIDEO_SUFFIX_REMOVAL_PATTERN
Expand Down Expand Up @@ -193,7 +194,7 @@ class VideoSource extends BaseSource {
const srcs = this.sourceTypes().map(sourceType => {
const srcTransformation = this.sourceTransformation()[sourceType] || this.transformation();
const format = normalizeFormat(sourceType);
const isAdaptive = ['mpd', 'm3u8'].indexOf(format) !== -1;
const isAdaptive = ADAPTIVE_SOURCETYPES.includes(format);
const opts = {};

if (srcTransformation) {
Expand Down Expand Up @@ -244,10 +245,12 @@ class VideoSource extends BaseSource {
}

generateRawSource(url, type) {
const t = type || url.split('.').pop();
const isAdaptive = !!CONTAINER_MIME_TYPES[t];
if (isAdaptive) {
type = CONTAINER_MIME_TYPES[t][0];
type = type || url.split('.').pop();

const isAdaptive = ADAPTIVE_SOURCETYPES.includes(type);

if (CONTAINER_MIME_TYPES[type]) {
type = CONTAINER_MIME_TYPES[type];
} else {
type = type ? `video/${type}` : null;
}
Expand Down
12 changes: 4 additions & 8 deletions src/plugins/cloudinary/models/video-source/video-source.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import { some } from '../../../../utils/array';

export function formatToMimeTypeAndTransformation(format) {
const [container, codec] = format.toLowerCase().split('\/');
let result = CONTAINER_MIME_TYPES[container];
let transformation = null;

if (!result) {
result = [`video/${container}`, transformation];
}
const mimetype = CONTAINER_MIME_TYPES[container] || `video/${container}`;
let result = [mimetype];

if (codec) {
transformation = codecToSrcTransformation(codec);
result = [`${result[0]}; codecs="${codecShorthandTrans(codec)}"`, transformation];
const transformation = codecToSrcTransformation(codec);
result = [`${mimetype}; codecs="${codecShorthandTrans(codec)}"`, transformation];
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cloudinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const setError = (that, res) => {
};

const setVideoSrc = (that, srcs) => {
console.log('Trying urls: ' + JSON.stringify(srcs));
console.log('Trying sources: ', srcs);
srcs.forEach(s => {
s.try = true;
});
Expand Down
4 changes: 2 additions & 2 deletions test/unit/videoSource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('Raw url tests', () => {
let source = new VideoSource(url, sourceDef);
let srcs = source.generateSources();
expect(srcs[0].src).toEqual(url);
expect(srcs[0].type).toEqual(null);
expect(srcs[0].type).toEqual('video/mp4');
expect(srcs[0].isAdaptive).toEqual(false);
});
it('Test raw url with transformations', () => {
Expand All @@ -169,7 +169,7 @@ describe('Raw url tests', () => {
let source = new VideoSource(url, sourceDef);
let srcs = source.generateSources();
expect(srcs[0].src).toEqual(url);
expect(srcs[0].type).toEqual(null);
expect(srcs[0].type).toEqual('video/mp4');
expect(srcs[0].isAdaptive).toEqual(false);
});
it('Test raw url without extension', () => {
Expand Down

0 comments on commit 4fe8dfc

Please sign in to comment.