better content type handling
This commit is contained in:
18
src/App.js
18
src/App.js
@@ -17,14 +17,10 @@ async function loadFromBytebin(id) {
|
|||||||
const resp = await fetch('https://bytebin.lucko.me/' + id);
|
const resp = await fetch('https://bytebin.lucko.me/' + id);
|
||||||
if (resp.ok) {
|
if (resp.ok) {
|
||||||
const content = await resp.text();
|
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;
|
document.title = 'paste | ' + id;
|
||||||
if (type === 'text' && languageIds.includes(subType.toLowerCase())) {
|
return { ok: true, content, type };
|
||||||
return { ok: true, content, type: subType.toLowerCase() };
|
|
||||||
} else {
|
|
||||||
return { ok: true, content };
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return { ok: false };
|
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 INITIAL = Symbol();
|
||||||
const LOADING = Symbol();
|
const LOADING = Symbol();
|
||||||
const LOADED = Symbol();
|
const LOADED = Symbol();
|
||||||
|
|||||||
@@ -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) {
|
async function saveToBytebin(code, language) {
|
||||||
try {
|
try {
|
||||||
const compressed = gzip(code);
|
const compressed = gzip(code);
|
||||||
const contentType = 'text/' + language;
|
const contentType = langaugeToContentType(language);
|
||||||
|
|
||||||
const resp = await fetch('https://bytebin.lucko.me/post', {
|
const resp = await fetch('https://bytebin.lucko.me/post', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|||||||
Reference in New Issue
Block a user