Improve getMimeType to remove trailing dot in the extension if preset

This commit is contained in:
Nariman Jelveh 2024-07-08 17:33:09 -07:00
parent 073b501e41
commit 535475b3c3

View File

@ -62,6 +62,7 @@ if(domain === 'puter.localhost'){
static_hosting_domain = 'site.puter.localhost'; static_hosting_domain = 'site.puter.localhost';
} }
// port
if (URLParams.has('puter.port') && URLParams.get('puter.port')) { if (URLParams.has('puter.port') && URLParams.get('puter.port')) {
static_hosting_domain = static_hosting_domain + `:` + URLParams.get('puter.port'); static_hosting_domain = static_hosting_domain + `:` + URLParams.get('puter.port');
} }
@ -81,7 +82,6 @@ if (URLParams.has('puter.port') && URLParams.get('puter.port')) {
$(document).ready(function () { $(document).ready(function () {
$('#loading').show(); $('#loading').show();
// get dev profile
setTimeout(async function () { setTimeout(async function () {
puter.ui.onLaunchedWithItems(async function (items) { puter.ui.onLaunchedWithItems(async function (items) {
source_path = items[0].path; source_path = items[0].path;
@ -95,6 +95,7 @@ $(document).ready(function () {
} }
}) })
// Get dev profile. This is only for puter.com for now as we don't have dev profiles in self-hosted Puter
if(domain === 'puter.com'){ if(domain === 'puter.com'){
puter.apps.getDeveloperProfile(async function (dev_profile) { puter.apps.getDeveloperProfile(async function (dev_profile) {
developer = dev_profile; developer = dev_profile;
@ -119,7 +120,7 @@ $(document).ready(function () {
} }
}) })
} }
// get apps // Get apps
puter.apps.list().then((resp) => { puter.apps.list().then((resp) => {
apps = resp; apps = resp;
@ -176,16 +177,16 @@ $(document).on('click', '.tab-btn', function (e) {
$(this).addClass('active'); $(this).addClass('active');
$('section[data-tab="' + $(this).attr('data-tab') + '"]').show(); $('section[data-tab="' + $(this).attr('data-tab') + '"]').show();
// ------------------------------ // ---------------------------------------------------------------
// Apps tab // Apps tab
// ------------------------------ // ---------------------------------------------------------------
if ($(this).attr('data-tab') === 'apps') { if ($(this).attr('data-tab') === 'apps') {
refresh_app_list(); refresh_app_list();
activeTab = 'apps'; activeTab = 'apps';
} }
// ------------------------------ // ---------------------------------------------------------------
// Payout Method tab // Payout Method tab
// ------------------------------ // ---------------------------------------------------------------
else if ($(this).attr('data-tab') === 'payout-method') { else if ($(this).attr('data-tab') === 'payout-method') {
activeTab = 'payout-method'; activeTab = 'payout-method';
$('#loading').show(); $('#loading').show();
@ -318,6 +319,7 @@ async function create_app(title, source_path = null, items = null) {
}) })
} }
$(document).on('click', '.deploy-btn', function (e) { $(document).on('click', '.deploy-btn', function (e) {
deploy(currently_editing_app, dropped_items); deploy(currently_editing_app, dropped_items);
}) })
@ -856,8 +858,6 @@ $('#earn-money::backdrop').click(async function (e) {
puter.kv.set('earn-money-c2a-closed', 'true') puter.kv.set('earn-money-c2a-closed', 'true')
}) })
$(document).on('click', '.edit-app-open-app-btn', async function (e) { $(document).on('click', '.edit-app-open-app-btn', async function (e) {
puter.ui.launchApp($(this).attr('data-app-name')) puter.ui.launchApp($(this).attr('data-app-name'))
}) })
@ -983,8 +983,10 @@ $(document).on('click', '#edit-app-icon', async function (e) {
let image = reader.result; let image = reader.result;
// Get file extension // Get file extension
let fileExtension = res2.name.split('.').pop(); let fileExtension = res2.name.split('.').pop();
// Get MIME type // Get MIME type
let mimeType = getMimeType(fileExtension); let mimeType = getMimeType(fileExtension);
// Replace MIME type in the data URL // Replace MIME type in the data URL
image = image.replace('data:application/octet-stream;base64', `data:image/${mimeType};base64`); image = image.replace('data:application/octet-stream;base64', `data:image/${mimeType};base64`);
@ -1066,10 +1068,7 @@ function generate_app_card(app) {
h += `</td>`; h += `</td>`;
h += `<td style="vertical-align:middle; min-width:200px;">`; h += `<td style="vertical-align:middle; min-width:200px;">`;
// // Open App
// h += `<button class="open-app-btn button button-small" data-app-name="${html_encode(app.name)}">Open App</button>`;
// // Settings
// h += `<span class="edit-app" data-app-name="${html_encode(app.name)}" data-app-title="${html_encode(app.title)}" data-app-uid="${html_encode(app.uid)}"><img src="./img/settings.svg"></span>`;
// "Approved for incentive program" // "Approved for incentive program"
if (app.approved_for_incentive_program) if (app.approved_for_incentive_program)
h += `<span style="float:right; h += `<span style="float:right;
@ -1084,7 +1083,6 @@ function generate_app_card(app) {
return h; return h;
} }
/** /**
* Formats a binary-byte integer into the human-readable form with units. * Formats a binary-byte integer into the human-readable form with units.
* *
@ -1098,6 +1096,9 @@ window.byte_format = (bytes) => {
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}; };
/**
* check if a string is a valid email address
*/
function validateEmail(email) { function validateEmail(email) {
var re = /\S+@\S+\.\S+/; var re = /\S+@\S+\.\S+/;
return re.test(email); return re.test(email);
@ -1852,19 +1853,28 @@ $(document).on('change', '.select-all-apps', function (e) {
} }
}) })
// Function to map file extensions to MIME types /**
* Get the MIME type for a given file extension.
*
* @param {string} extension - The file extension (with or without leading dot).
* @returns {string} The corresponding MIME type, or 'application/octet-stream' if not found.
*/
function getMimeType(extension) { function getMimeType(extension) {
const mimeTypes = { const mimeTypes = {
jpg: 'jpeg', jpg: 'image/jpeg',
jpeg: 'jpeg', jpeg: 'image/jpeg',
png: 'png', png: 'image/png',
gif: 'gif', gif: 'image/gif',
bmp: 'bmp', bmp: 'image/bmp',
webp: 'webp', webp: 'image/webp',
svg: 'svg+xml', svg: 'image/svg+xml',
tiff: 'tiff', tiff: 'image/tiff',
ico: 'vnd.microsoft.icon' ico: 'image/x-icon'
}; };
return mimeTypes[extension.toLowerCase()] || 'octet-stream'; // Remove leading dot if present and convert to lowercase
const cleanExtension = extension.replace(/^\./, '').toLowerCase();
// Return the MIME type if found, otherwise return 'application/octet-stream'
return mimeTypes[cleanExtension] || 'application/octet-stream';
} }