Created
November 6, 2023 16:22
-
-
Save zoharbabin/8217604a36cf3a38bb9dc2b53c8f9da8 to your computer and use it in GitHub Desktop.
a sample html showing how to map various input file types to the correct entry and media types in Kaltura
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>File Type Tester</title> | |
</head> | |
<body> | |
<input type="file" id="media-file" multiple> | |
<div id="results"></div> | |
<script> | |
// Polyfill for startsWith, for IE compatibility | |
if (!String.prototype.startsWith) { | |
String.prototype.startsWith = function(search, pos) { | |
return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search; | |
}; | |
} | |
// Polyfill for Array.prototype.forEach for older browsers | |
if (!Array.prototype.forEach) { | |
Array.prototype.forEach = function(callback) { | |
for (var i = 0; i < this.length; i++) { | |
callback(this[i], i, this); | |
} | |
}; | |
} | |
// Kaltura Enum Definitions for Entry and Media Types | |
var KalturaEntryType = { MEDIA_CLIP: '1', DATA: '6', DOCUMENT: '10' }; | |
var KalturaMediaType = { VIDEO: 1, IMAGE: 2, AUDIO: 5 }; | |
// MIME type categories | |
var MEDIA_MIME_PREFIXES = ['image/', 'video/', 'audio/']; | |
var DATA_MIME_TYPES = ['application/json', 'text/plain', 'text/csv', 'application/xml', 'application/x-www-form-urlencoded', 'text/html', 'application/x-har', 'text/calendar', 'application/shellscript', 'application/zip', 'application/x-subrip', 'application/octet-stream', 'text/css']; | |
var DOCUMENT_MIME_TYPES = ['application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/pdf', 'application/vnd.openxmlformats-officedocument.presentationml.template']; | |
function resolveSpecialMimeTypes(fileExtension) { | |
var specialMimeMapping = { | |
'srt': 'application/x-subrip', | |
'mo': 'application/octet-stream', | |
'po': 'text/x-gettext-translation' | |
}; | |
return specialMimeMapping[fileExtension] || ''; | |
} | |
function classifyFile(file) { | |
var fileExtension = file.name.split('.').pop().toLowerCase(); | |
var mimeType = file.type || resolveSpecialMimeTypes(fileExtension); | |
var result = { entryType: null, mediaType: null, mimeType: mimeType }; | |
if (MEDIA_MIME_PREFIXES.some(function(prefix) { return mimeType.startsWith(prefix); })) { | |
result.entryType = KalturaEntryType.MEDIA_CLIP; | |
result.mediaType = KalturaMediaType[mimeType.split('/')[0].toUpperCase()]; | |
} else if (DATA_MIME_TYPES.indexOf(mimeType) !== -1 || DOCUMENT_MIME_TYPES.indexOf(mimeType) !== -1) { | |
result.entryType = DATA_MIME_TYPES.indexOf(mimeType) !== -1 ? KalturaEntryType.DATA : KalturaEntryType.DOCUMENT; | |
} else if (['xml', 'html', 'har', 'jar', 'ics', 'sh', 'c', 'ked'].indexOf(fileExtension) !== -1) { | |
result.entryType = KalturaEntryType.DATA; | |
} | |
return result; | |
} | |
// Waiting for the DOM to be loaded | |
document.addEventListener('DOMContentLoaded', function() { | |
var inputElement = document.getElementById('media-file'); | |
if (inputElement) { | |
inputElement.addEventListener('change', function() { | |
var resultsDiv = document.getElementById('results'); | |
if (resultsDiv) { | |
resultsDiv.innerHTML = ''; | |
// Loop through each file and classify | |
Array.prototype.forEach.call(this.files, function(file) { | |
var result = classifyFile(file); | |
if (result.entryType) { | |
console.log("File: " + file.name + ", Entry Type: " + result.entryType + ", Media Type: " + result.mediaType + ", MimeType: " + result.mimeType); | |
} else { | |
resultsDiv.innerHTML += '<p style="color:red;">File: ' + file.name + ', Unknown type, MimeType: ' + result.mimeType + '</p>'; | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment