favycon/utils/files.ts
Rui Saraiva efa6b4cc1e
feat: add drag and drop wizard (#39)
* feat(drag-and-drop): add drag and drop wizard

* chore(seo): update seo share image url

* improvement(upload): add download step 3

* improvement(loading): improve loading and add transitions

* improvement: add pwa support, clipboard and animations

* improvement(seo): add html lang and update title

* fix(button): fix button font family
2020-04-19 19:11:20 +01:00

56 lines
1.4 KiB
TypeScript

import { NextApiRequest } from 'next'
export const ONE_MB = 1048576 // 1MB
export const MIN_SIZE = 310
export const MIN_PWA_SIZE = 512
export const SVG_MIME_TYPE = 'image/svg+xml'
export const RECOMMENDED_MIME_TYPES = ['image/png', SVG_MIME_TYPE]
export const ACCEPT_MIME_TYPES = {
'image/jpeg': 'JPEG',
'image/png': 'PNG',
'image/svg+xml': 'SVG',
} as { [key: string]: string }
export const getImageFileSizes = (file: File) =>
new Promise<{ width: number; height: number }>((resolve) => {
const fileReader = new FileReader()
fileReader.onload = () => {
const img = new Image()
img.onload = () => {
resolve({ width: img.width, height: img.height })
}
img.src = fileReader.result as string
}
fileReader.readAsDataURL(file)
})
export const downloadFile = (data: ArrayBuffer, filename: string) => {
if ('msSaveOrOpenBlob' in window.navigator) {
// Blob for IE11
window.navigator.msSaveOrOpenBlob(new Blob([data]), filename)
} else {
// Blob navigator
const url = window.URL.createObjectURL(new Blob([data]))
const link = document.createElement('a')
link.href = url
link.setAttribute('download', filename)
document.body.appendChild(link)
link.click()
link.remove()
}
}
export type MulterApiRequest = NextApiRequest & {
file: {
fieldname: string
originalname: string
encoding: string
mimetype: string
size: number
destination: string
filename: string
path: string
buffer: Buffer
}
}