Skip to content

Commit

Permalink
feat: guess file extenstion from content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
seasick committed Feb 7, 2024
1 parent aafc19f commit 90cb411
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/hooks/useImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ export default function useImport(url?: string, autoImport = false) {
files.map(async (file) => {
const response = await fetch(file.url);
const content = await response.arrayBuffer();
const name = file.name.split('?')[0]; // Strip possible query string from file name
let name = file.name.split('?')[0]; // Strip possible query string from file name

// Check if the file extension is missing from the file name. If so,
// try to guess it from the content type.
// TODO
if (!name.includes('.')) {
const contentType = response.headers.get('Content-Type');

if (contentType.startsWith('application/zip')) {
name += '.zip';
} else if (contentType.startsWith('text/plain')) {
name += '.scad';
} else {
// Everything else is SCAD too, for now.
name += '.scad';
}
}

// If it is a scad file we can try to look if there are any libraries to import
if (autoImport && name.endsWith('.scad')) {
Expand Down

0 comments on commit 90cb411

Please sign in to comment.