Skip to content

Commit

Permalink
refactor audio and video files according to better understanding of c…
Browse files Browse the repository at this point in the history
…ontainer formats
  • Loading branch information
thescientist13 committed Mar 2, 2023
1 parent 01c5bf0 commit ae7ac1a
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 25 deletions.
5 changes: 0 additions & 5 deletions packages/cli/src/lib/resource-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,8 @@ async function resolveForRelativeUrl(url, rootUrl) {
return reducedUrl;
}

function isGenericMediaContainer(extension) {
return ['ogg', 'mp4', '3gp'].includes(extension);
}

export {
checkResourceExists,
isGenericMediaContainer,
mergeResponse,
modelResource,
normalizePathnameForWindows,
Expand Down
10 changes: 3 additions & 7 deletions packages/cli/src/plugins/resource/plugin-standard-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*
*/
import fs from 'fs/promises';
import { isGenericMediaContainer } from '../../lib/resource-utils.js';
import { ResourceInterface } from '../../lib/resource-interface.js';

class StandardAudioResource extends ResourceInterface {
Expand All @@ -14,16 +13,13 @@ class StandardAudioResource extends ResourceInterface {

// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Audio_codecs
// https://www.thoughtco.com/audio-file-mime-types-3469485
// TODO add support for .ts
this.extensions = ['3gp', 'mid', 'mp3', 'mp4', 'm3u', 'oga', 'ogg', 'ra', 'wav'];
this.extensions = ['mid', 'mp3', 'm3u', 'oga', 'ra', 'wav'];
}

async shouldServe(url, request) {
async shouldServe(url) {
const extension = url.pathname.split('.').pop();
const isAudioFile = request.headers.get('Sec-Fetch-Dest') === 'audio';
const isGenericAudioResource = isGenericMediaContainer(extension) && isAudioFile;

return url.protocol === 'file:' && (isGenericAudioResource || (!isGenericMediaContainer(extension) && this.extensions.includes(extension)));
return url.protocol === 'file:' && this.extensions.includes(extension);
}

async serve(url) {
Expand Down
9 changes: 4 additions & 5 deletions packages/cli/src/plugins/resource/plugin-standard-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*
*/
import fs from 'fs/promises';
import { isGenericMediaContainer } from '../../lib/resource-utils.js';
import { ResourceInterface } from '../../lib/resource-interface.js';

class StandardVideoResource extends ResourceInterface {
Expand All @@ -18,12 +17,10 @@ class StandardVideoResource extends ResourceInterface {
this.extensions = ['3gp', 'avi', 'flv', 'm3u8', 'mp4', 'mov', 'ogg', 'ogv', 'wmv'];
}

async shouldServe(url, request) {
async shouldServe(url) {
const extension = url.pathname.split('.').pop();
const isVideoFile = request.headers.get('Sec-Fetch-Dest') === 'video';
const isGenericVideoResource = isGenericMediaContainer(extension) && isVideoFile;

return url.protocol === 'file:' && (isGenericVideoResource || !isGenericMediaContainer(extension) && this.extensions.includes(extension));
return url.protocol === 'file:' && this.extensions.includes(extension);
}

async serve(url) {
Expand Down Expand Up @@ -52,6 +49,8 @@ class StandardVideoResource extends ResourceInterface {
contentType = 'video/quicktime';
break;
case 'ogg':
contentType = `application/${extension}`;
break;
case 'ogv':
contentType = `video/${extension}`;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,10 +902,7 @@ describe('Develop Greenwood With: ', function() {
before(async function() {
return new Promise((resolve, reject) => {
request.get({
url: `${hostname}:${port}/assets/splash-clip.mp4`,
headers: {
'Sec-Fetch-Dest': 'video'
}
url: `${hostname}:${port}/assets/splash-clip.mp4`
}, (err, res, body) => {
if (err) {
reject();
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/test/cases/serve.default/serve.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,7 @@ describe('Serve Greenwood With: ', function() {
before(async function() {
return new Promise((resolve, reject) => {
request.get({
url: `${hostname}/assets/splash-clip.mp4`,
headers: {
'Sec-Fetch-Dest': 'video'
}
url: `${hostname}/assets/splash-clip.mp4`
}, (err, res, body) => {
if (err) {
reject();
Expand Down

0 comments on commit ae7ac1a

Please sign in to comment.