Skip to content

Commit

Permalink
Improve getMimeType to remove trailing dot in the extension if preset
Browse files Browse the repository at this point in the history
  • Loading branch information
jelveh committed Jul 9, 2024
1 parent 073b501 commit 535475b
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions src/dev-center/js/dev-center.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ if(domain === 'puter.localhost'){
static_hosting_domain = 'site.puter.localhost';
}

// port
if (URLParams.has('puter.port') && URLParams.get('puter.port')) {
static_hosting_domain = static_hosting_domain + `:` + URLParams.get('puter.port');
}
Expand All @@ -81,7 +82,6 @@ if (URLParams.has('puter.port') && URLParams.get('puter.port')) {
$(document).ready(function () {
$('#loading').show();

// get dev profile
setTimeout(async function () {
puter.ui.onLaunchedWithItems(async function (items) {
source_path = items[0].path;
Expand All @@ -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'){
puter.apps.getDeveloperProfile(async function (dev_profile) {
developer = dev_profile;
Expand All @@ -119,7 +120,7 @@ $(document).ready(function () {
}
})
}
// get apps
// Get apps
puter.apps.list().then((resp) => {
apps = resp;

Expand Down Expand Up @@ -176,16 +177,16 @@ $(document).on('click', '.tab-btn', function (e) {
$(this).addClass('active');
$('section[data-tab="' + $(this).attr('data-tab') + '"]').show();

// ------------------------------
// ---------------------------------------------------------------
// Apps tab
// ------------------------------
// ---------------------------------------------------------------
if ($(this).attr('data-tab') === 'apps') {
refresh_app_list();
activeTab = 'apps';
}
// ------------------------------
// ---------------------------------------------------------------
// Payout Method tab
// ------------------------------
// ---------------------------------------------------------------
else if ($(this).attr('data-tab') === 'payout-method') {
activeTab = 'payout-method';
$('#loading').show();
Expand Down Expand Up @@ -318,6 +319,7 @@ async function create_app(title, source_path = null, items = null) {
})
}


$(document).on('click', '.deploy-btn', function (e) {
deploy(currently_editing_app, dropped_items);
})
Expand Down Expand Up @@ -856,8 +858,6 @@ $('#earn-money::backdrop').click(async function (e) {
puter.kv.set('earn-money-c2a-closed', 'true')
})



$(document).on('click', '.edit-app-open-app-btn', async function (e) {
puter.ui.launchApp($(this).attr('data-app-name'))
})
Expand Down Expand Up @@ -983,8 +983,10 @@ $(document).on('click', '#edit-app-icon', async function (e) {
let image = reader.result;
// Get file extension
let fileExtension = res2.name.split('.').pop();

// Get MIME type
let mimeType = getMimeType(fileExtension);

// Replace MIME type in the data URL
image = image.replace('data:application/octet-stream;base64', `data:image/${mimeType};base64`);

Expand Down Expand Up @@ -1066,10 +1068,7 @@ function generate_app_card(app) {
h += `</td>`;

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"
if (app.approved_for_incentive_program)
h += `<span style="float:right;
Expand All @@ -1084,7 +1083,6 @@ function generate_app_card(app) {
return h;
}


/**
* Formats a binary-byte integer into the human-readable form with units.
*
Expand All @@ -1098,6 +1096,9 @@ window.byte_format = (bytes) => {
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
};

/**
* check if a string is a valid email address
*/
function validateEmail(email) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
Expand Down Expand Up @@ -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) {
const mimeTypes = {
jpg: 'jpeg',
jpeg: 'jpeg',
png: 'png',
gif: 'gif',
bmp: 'bmp',
webp: 'webp',
svg: 'svg+xml',
tiff: 'tiff',
ico: 'vnd.microsoft.icon'
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
bmp: 'image/bmp',
webp: 'image/webp',
svg: 'image/svg+xml',
tiff: 'image/tiff',
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';
}

0 comments on commit 535475b

Please sign in to comment.