universal-data-tool
universal-data-tool copied to clipboard
[REFACTORING] Centralize the logic of the extension management
Description The current function is written multiple times in different part of code due to the collective work. It cause the app to control the extension in certain places and not to others.
The places where I did find a use : src/components/ImportPage/prompt-and-get-samples-from-local-directory.js
const convertToSamplesObject = (fp) => {
const ext = fp.split(".").slice(-1)[0].toLowerCase()
if (["png", "jpg", "jpeg"].includes(ext)) {
return { imageUrl: `file://${fp}` }
}
if (["pdf"].includes(ext)) {
return { pdfUrl: `file://${fp}` }
}
if (["mp4", "webm", "mkv"].includes(ext)) {
return { videoUrl: `file://${fp}` }
}
return null
}
src/components/PasteUrlsDialog/get-sample-from-url.js
switch (extension.toLowerCase()) {
case "png":
case "jpg":
case "gif":
case "jpeg":
case "bmp": {
return { imageUrl: s }
}
case "pdf": {
return { pdfUrl: s }
}
case "mp3":
case "wav": {
return { audioUrl: s }
}
default: {
if (opts.returnNulls) return null
throw new Error(`extension not recognized: "${extension}" in "${s}"`)
// TODO if the user doesn't care, return null (this
// behavior could be enabled with textfield option)
}
src/utils/RecognizeFileExtension.js
var typeOfFile = "File"
var fileExtension = UrlOfAFile.match(
`\\/?([^\\/\\\\&\\?]*\\.([a-zA-Z0-9]*))(\\?|$)`
)
if (!isEmpty(fileExtension)) {
fileExtension = fileExtension[2].toLowerCase()
}
if (
fileExtension === "jpg" ||
fileExtension === "jpeg" ||
fileExtension === "png" ||
fileExtension === "ico" ||
fileExtension === "jpe" ||
fileExtension === "gif" ||
fileExtension === "webp" ||
fileExtension === "jfif"
)
typeOfFile = "Image"
if (fileExtension === "mp4" || fileExtension === "mkv") typeOfFile = "Video"
if (fileExtension === "mp3") typeOfFile = "Audio"
if (fileExtension === "pdf") typeOfFile = "PDF"
if (fileExtension === "txt") typeOfFile = "Texte"
return typeOfFile
Benefits Set of extension allowed and supported unique Reduce the risk of error not managed Reduce the duplicity
Proposed solution All these place have a function who tell which extension is allowed and return the type of extension. Just create a new function who centralize all these for a uniform translation of the file extension
Other solution Stay as is.
We should generally use mime types: https://www.npmjs.com/package/mime-types