Skip to content

Commit 535475b

Browse files
committed
Improve getMimeType to remove trailing dot in the extension if preset
1 parent 073b501 commit 535475b

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/dev-center/js/dev-center.js

+34-24
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ if(domain === 'puter.localhost'){
6262
static_hosting_domain = 'site.puter.localhost';
6363
}
6464

65+
// port
6566
if (URLParams.has('puter.port') && URLParams.get('puter.port')) {
6667
static_hosting_domain = static_hosting_domain + `:` + URLParams.get('puter.port');
6768
}
@@ -81,7 +82,6 @@ if (URLParams.has('puter.port') && URLParams.get('puter.port')) {
8182
$(document).ready(function () {
8283
$('#loading').show();
8384

84-
// get dev profile
8585
setTimeout(async function () {
8686
puter.ui.onLaunchedWithItems(async function (items) {
8787
source_path = items[0].path;
@@ -95,6 +95,7 @@ $(document).ready(function () {
9595
}
9696
})
9797

98+
// Get dev profile. This is only for puter.com for now as we don't have dev profiles in self-hosted Puter
9899
if(domain === 'puter.com'){
99100
puter.apps.getDeveloperProfile(async function (dev_profile) {
100101
developer = dev_profile;
@@ -119,7 +120,7 @@ $(document).ready(function () {
119120
}
120121
})
121122
}
122-
// get apps
123+
// Get apps
123124
puter.apps.list().then((resp) => {
124125
apps = resp;
125126

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

179-
// ------------------------------
180+
// ---------------------------------------------------------------
180181
// Apps tab
181-
// ------------------------------
182+
// ---------------------------------------------------------------
182183
if ($(this).attr('data-tab') === 'apps') {
183184
refresh_app_list();
184185
activeTab = 'apps';
185186
}
186-
// ------------------------------
187+
// ---------------------------------------------------------------
187188
// Payout Method tab
188-
// ------------------------------
189+
// ---------------------------------------------------------------
189190
else if ($(this).attr('data-tab') === 'payout-method') {
190191
activeTab = 'payout-method';
191192
$('#loading').show();
@@ -318,6 +319,7 @@ async function create_app(title, source_path = null, items = null) {
318319
})
319320
}
320321

322+
321323
$(document).on('click', '.deploy-btn', function (e) {
322324
deploy(currently_editing_app, dropped_items);
323325
})
@@ -856,8 +858,6 @@ $('#earn-money::backdrop').click(async function (e) {
856858
puter.kv.set('earn-money-c2a-closed', 'true')
857859
})
858860

859-
860-
861861
$(document).on('click', '.edit-app-open-app-btn', async function (e) {
862862
puter.ui.launchApp($(this).attr('data-app-name'))
863863
})
@@ -983,8 +983,10 @@ $(document).on('click', '#edit-app-icon', async function (e) {
983983
let image = reader.result;
984984
// Get file extension
985985
let fileExtension = res2.name.split('.').pop();
986+
986987
// Get MIME type
987988
let mimeType = getMimeType(fileExtension);
989+
988990
// Replace MIME type in the data URL
989991
image = image.replace('data:application/octet-stream;base64', `data:image/${mimeType};base64`);
990992

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

10681070
h += `<td style="vertical-align:middle; min-width:200px;">`;
1069-
// // Open App
1070-
// h += `<button class="open-app-btn button button-small" data-app-name="${html_encode(app.name)}">Open App</button>`;
1071-
// // Settings
1072-
// 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>`;
1071+
10731072
// "Approved for incentive program"
10741073
if (app.approved_for_incentive_program)
10751074
h += `<span style="float:right;
@@ -1084,7 +1083,6 @@ function generate_app_card(app) {
10841083
return h;
10851084
}
10861085

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

1099+
/**
1100+
* check if a string is a valid email address
1101+
*/
11011102
function validateEmail(email) {
11021103
var re = /\S+@\S+\.\S+/;
11031104
return re.test(email);
@@ -1852,19 +1853,28 @@ $(document).on('change', '.select-all-apps', function (e) {
18521853
}
18531854
})
18541855

1855-
// Function to map file extensions to MIME types
1856+
/**
1857+
* Get the MIME type for a given file extension.
1858+
*
1859+
* @param {string} extension - The file extension (with or without leading dot).
1860+
* @returns {string} The corresponding MIME type, or 'application/octet-stream' if not found.
1861+
*/
18561862
function getMimeType(extension) {
18571863
const mimeTypes = {
1858-
jpg: 'jpeg',
1859-
jpeg: 'jpeg',
1860-
png: 'png',
1861-
gif: 'gif',
1862-
bmp: 'bmp',
1863-
webp: 'webp',
1864-
svg: 'svg+xml',
1865-
tiff: 'tiff',
1866-
ico: 'vnd.microsoft.icon'
1864+
jpg: 'image/jpeg',
1865+
jpeg: 'image/jpeg',
1866+
png: 'image/png',
1867+
gif: 'image/gif',
1868+
bmp: 'image/bmp',
1869+
webp: 'image/webp',
1870+
svg: 'image/svg+xml',
1871+
tiff: 'image/tiff',
1872+
ico: 'image/x-icon'
18671873
};
18681874

1869-
return mimeTypes[extension.toLowerCase()] || 'octet-stream';
1875+
// Remove leading dot if present and convert to lowercase
1876+
const cleanExtension = extension.replace(/^\./, '').toLowerCase();
1877+
1878+
// Return the MIME type if found, otherwise return 'application/octet-stream'
1879+
return mimeTypes[cleanExtension] || 'application/octet-stream';
18701880
}

0 commit comments

Comments
 (0)