better content type handling

This commit is contained in:
Luck
2021-03-27 13:09:03 +00:00
parent c8e687d67d
commit e790c54b56
2 changed files with 21 additions and 7 deletions

View File

@@ -17,14 +17,10 @@ async function loadFromBytebin(id) {
const resp = await fetch('https://bytebin.lucko.me/' + id);
if (resp.ok) {
const content = await resp.text();
const { type, subtype: subType } = parseContentType(resp.headers.get('content-type'));
const type = parseLanguageFromContentType(resp.headers.get('content-type'));
document.title = 'paste | ' + id;
if (type === 'text' && languageIds.includes(subType.toLowerCase())) {
return { ok: true, content, type: subType.toLowerCase() };
} else {
return { ok: true, content };
}
return { ok: true, content, type };
} else {
return { ok: false };
}
@@ -33,6 +29,16 @@ async function loadFromBytebin(id) {
}
}
function parseLanguageFromContentType(contentType) {
const { type, subtype: subType } = parseContentType(contentType);
if (type === 'application' && subType === 'json') {
return 'json';
}
if (type === 'text' && languageIds.includes(subType.toLowerCase())) {
return subType.toLowerCase();
}
}
const INITIAL = Symbol();
const LOADING = Symbol();
const LOADED = Symbol();

View File

@@ -147,10 +147,18 @@ const MenuButton = ({ label, ids, value, setValue }) => {
)
}
function langaugeToContentType(language) {
if (language === 'json') {
return 'application/json';
} else {
return 'text/' + language;
}
}
async function saveToBytebin(code, language) {
try {
const compressed = gzip(code);
const contentType = 'text/' + language;
const contentType = langaugeToContentType(language);
const resp = await fetch('https://bytebin.lucko.me/post', {
method: 'POST',