bsky-app/src/lib/media/util.ts
Ollie H 072682dd9f
Rework scaled dimensions and compression (#737)
* Rework scaled dimensions and compression

* Unbreak image / banner uploads

---------

Co-authored-by: Paul Frazee <pfrazee@gmail.com>
2023-05-30 19:23:55 -05:00

28 lines
799 B
TypeScript

export function extractDataUriMime(uri: string): string {
return uri.substring(uri.indexOf(':') + 1, uri.indexOf(';'))
}
// Fairly accurate estimate that is more performant
// than decoding and checking length of URI
export function getDataUriSize(uri: string): number {
return Math.round((uri.length * 3) / 4)
}
export function isUriImage(uri: string) {
return /\.(jpg|jpeg|png).*$/.test(uri)
}
export function blobToDataUri(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => {
if (typeof reader.result === 'string') {
resolve(reader.result)
} else {
reject(new Error('Failed to read blob'))
}
}
reader.onerror = reject
reader.readAsDataURL(blob)
})
}