mirror of
https://github.com/scratchfoundation/scratch-asset-types.git
synced 2025-08-28 21:38:44 -04:00
Add support for detecting mp3 audio
This commit is contained in:
parent
1abe1d18f6
commit
3addb0e183
4 changed files with 19 additions and 2 deletions
18
index.js
18
index.js
|
@ -16,7 +16,13 @@ module.exports.bufferCheck = input => {
|
|||
}, options);
|
||||
|
||||
for (let i = 0; i < header.length; i++) {
|
||||
if (header[i] !== buf[i + options.offset]) {
|
||||
// If a bitmask is set
|
||||
if (options.mask) {
|
||||
// If header doesn't equal `buf` with bits masked off
|
||||
if (header[i] !== (options.mask[i] & buf[i + options.offset])) {
|
||||
return false;
|
||||
}
|
||||
} else if (header[i] !== buf[i + options.offset]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +57,16 @@ module.exports.bufferCheck = input => {
|
|||
return typesList.wav;
|
||||
}
|
||||
|
||||
// Check for MPEG header at different starting offsets
|
||||
for (let start = 0; start < 2 && start < (buf.length - 16); start++) {
|
||||
if (
|
||||
check([0x49, 0x44, 0x33], {offset: start}) || // ID3 header
|
||||
check([0xFF, 0xE2], {offset: start, mask: [0xFF, 0xE2]}) // MPEG 1 or 2 Layer 3 header
|
||||
) {
|
||||
return typesList.mp3;
|
||||
}
|
||||
}
|
||||
|
||||
if (check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20,
|
||||
0x20, 0x0D, 0x0A, 0x87, 0x0A])) {
|
||||
// JPEG-2000 family
|
||||
|
|
|
@ -6,6 +6,7 @@ module.exports = {
|
|||
jp2: {ext: 'jpg', mime: 'image/jpg'},
|
||||
json: {ext: 'json', mime: 'application/json'},
|
||||
mj2: {ext: 'jpg', mime: 'image/jpg'},
|
||||
mp3: {ext: 'mp3', mime: 'audio/mpeg'},
|
||||
png: {ext: 'png', mime: 'image/png'},
|
||||
webp: {ext: 'webp', mime: 'image/webp'},
|
||||
wav: {ext: 'wav', mime: 'audio/x-wav'},
|
||||
|
|
BIN
test/fixtures/test.mp3
vendored
Normal file
BIN
test/fixtures/test.mp3
vendored
Normal file
Binary file not shown.
|
@ -3,7 +3,7 @@ const fileType = require('../../index');
|
|||
const typesList = require('../../lib/typeslist');
|
||||
|
||||
const checkList = [
|
||||
'gif', 'jpg', 'json', 'png', 'wav', 'webp', 'zip'];
|
||||
'gif', 'jpg', 'json', 'mp3', 'png', 'wav', 'webp', 'zip'];
|
||||
|
||||
tap.test('check-types', t => {
|
||||
checkList.forEach(thisType => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue