forked from coracle/flotilla
Compress profile pictures on upload
This commit is contained in:
@@ -135,3 +135,34 @@ export const scrollToEvent = async (id: string, attempts = 3): Promise<boolean>
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
export const stripExifData = async (file, {maxWidth = null, maxHeight = null} = {}) => {
|
||||
if (window.DataTransferItem && file instanceof DataTransferItem) {
|
||||
file = file.getAsFile()
|
||||
}
|
||||
|
||||
if (!file) {
|
||||
return file
|
||||
}
|
||||
|
||||
const {default: Compressor} = await import("compressorjs")
|
||||
|
||||
/* eslint no-new: 0 */
|
||||
|
||||
return new Promise((resolve, _reject) => {
|
||||
new Compressor(file, {
|
||||
maxWidth: maxWidth || 2048,
|
||||
maxHeight: maxHeight || 2048,
|
||||
convertSize: 10 * 1024 * 1024,
|
||||
success: resolve,
|
||||
error: e => {
|
||||
// Non-images break compressor
|
||||
if (e.toString().includes("File or Blob")) {
|
||||
return resolve(file)
|
||||
}
|
||||
|
||||
_reject(e)
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user