import MD5 from 'crypto-js/md5'
import { FileWithPath } from 'react-dropzone'
import { v1 as uuidv1 } from 'uuid'
export const fileUrl = (file: File | FileWithPath | undefined): string => {
if (file) {
const URL = window.URL || window.webkitURL
return URL.createObjectURL(file)
} else {
return ''
}
}
export const parseUrl = (url: string): string => {
if (!url) {
return ''
}
try {
const urlData = new URL(url)
return urlData.pathname
} catch (e: unknown) {
console.log(e)
return ''
}
}
export const filenameHandle = (
file: File | FileWithPath
): File | FileWithPath => {
const name = MD5(file.name).toString().toLowerCase()
const uuid = uuidv1()
const filename = `${name}.${uuid}${parseFileExtension(file?.name || '.png')}`
return new File([file], filename, {
type: file.type,
})
}
export const parseFilename = (filename: string): string => {
if (!filename) {
return ''
}
return filename.substring(0, filename.lastIndexOf('.'))
}
export const parseFileExtension = (filename: string): string => {
if (!filename) {
return ''
}
return filename.substring(filename.lastIndexOf('.'))
}
export function downloadFile(url: string): void {
if (!url) return
const link = document.createElement('a')
link.href = url
document.body.appendChild(link)
link.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
})
)
document.body.removeChild(link)
}