Adds support for .step extension, disables .stl (#51)

* New structure to map multiple extensions to type, and disable STL

* Clean up
This commit is contained in:
Pierre Jacquier
2023-03-30 04:32:29 -04:00
committed by GitHub
parent e8d4798c17
commit f491739193
3 changed files with 24 additions and 23 deletions

View File

@ -4,7 +4,10 @@ import { downloadFile, isFilenameSupported } from './diff'
it('checks if the filename has a supported extension', () => {
expect(isFilenameSupported('noextension')).toBe(false)
expect(isFilenameSupported('unsupported.txt')).toBe(false)
expect(isFilenameSupported('unsupported.stl')).toBe(false)
expect(isFilenameSupported('supported.obj')).toBe(true)
expect(isFilenameSupported('supported.stp')).toBe(true)
expect(isFilenameSupported('supported.step')).toBe(true)
})
describe('Function downloadFile', () => {

View File

@ -6,24 +6,22 @@ import {
FileImportFormat_type,
} from '@kittycad/lib/dist/types/src/models'
// TODO: check if we could get that from the library
export const supportedSrcFormats = new Set([
'dae',
'dxf',
'fbx',
'obj',
'step',
'stl',
'svg',
])
export const extensionToSrcFormat: {
[extension: string]: FileImportFormat_type
} = {
'dae': 'dae',
'dxf': 'dxf',
'fbx': 'fbx',
'obj': 'obj',
'stp': 'step',
'step': 'step',
'svg': 'svg',
// stl disabled for now as there's some GitHub support for them already, see #40
}
export function isFilenameSupported(filename: string) {
const parts = filename.split('.')
if (parts.length <= 1) {
return false
}
return supportedSrcFormats.has(parts.pop()!)
export function isFilenameSupported(filename: string): boolean{
const extension = filename.split('.').pop()
return !!(extension && extensionToSrcFormat[extension])
}
export async function downloadFile(
@ -57,14 +55,14 @@ export async function downloadFile(
async function convert(
client: Client,
body: string,
srcFormat: string,
extension: string,
outputFormat = 'stl'
) {
// TODO: think about the best output format for visual diff injection, now defaults to STL
const response = await file.create_file_conversion({
client,
body,
src_format: srcFormat as FileImportFormat_type,
src_format: extensionToSrcFormat[extension],
output_format: outputFormat as FileExportFormat_type,
})
if ('error_code' in response) throw response
@ -86,9 +84,9 @@ export async function getFileDiff(
): Promise<FileDiff> {
const { filename, status } = file
const extension = filename.split('.').pop()
if (!extension || !supportedSrcFormats.has(extension)) {
if (!extension || !extensionToSrcFormat[extension]) {
throw Error(
`Unsupported extension. Given ${extension}, was expecting ${supportedSrcFormats.values()}`
`Unsupported extension. Given ${extension}, was expecting ${Object.keys(extensionToSrcFormat)}`
)
}

View File

@ -1,5 +1,5 @@
import { createRoot, Root } from 'react-dom/client'
import { isFilenameSupported, supportedSrcFormats } from './diff'
import { isFilenameSupported, extensionToSrcFormat } from './diff'
import { DiffEntry } from './types'
export type GithubPullUrlParams = {
@ -45,7 +45,7 @@ export function getGithubCommitUrlParams(
}
export function getSupportedWebDiffElements(document: Document): HTMLElement[] {
const fileTypeSelectors = Array.from(supportedSrcFormats).map(
const fileTypeSelectors = Object.keys(extensionToSrcFormat).map(
t => `.file[data-file-type=".${t}"]`
)
const selector = fileTypeSelectors.join(', ')