Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85949336
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/changelog.d/compress-unreadable.fix b/changelog.d/compress-unreadable.fix
new file mode 100644
index 0000000000..f2c284402e
--- /dev/null
+++ b/changelog.d/compress-unreadable.fix
@@ -0,0 +1 @@
+Uploading a picture the browser cannot read no longer hangs with image compression on
diff --git a/src/components/media_upload/media_upload.js b/src/components/media_upload/media_upload.js
index 0e07626800..42c47964d5 100644
--- a/src/components/media_upload/media_upload.js
+++ b/src/components/media_upload/media_upload.js
@@ -1,188 +1,194 @@
import fileSizeFormatService from '../../services/file_size_format/file_size_format.js'
import statusPosterService from '../../services/status_poster/status_poster.service.js'
import { useInstanceStore } from 'src/stores/instance.js'
import { useMergedConfigStore } from 'src/stores/merged_config.js'
import { isAnimated } from 'src/services/animated_image/animated_image.js'
import { library } from '@fortawesome/fontawesome-svg-core'
import { faCircleNotch, faUpload } from '@fortawesome/free-solid-svg-icons'
library.add(faUpload, faCircleNotch)
const mediaUpload = {
data() {
return {
uploadCount: 0,
uploadReady: true,
}
},
computed: {
uploading() {
return this.uploadCount > 0
},
},
methods: {
onClick() {
if (this.uploadReady) {
this.$refs.input.click()
}
},
async resizeImage(file) {
// Skip if not an image, a GIF or an SVG
if (
!file.type.startsWith('image/') ||
file.type === 'image/gif' ||
file.type === 'image/svg+xml'
) {
return file
}
// Skip if image compression is disabled
if (!useMergedConfigStore().mergedConfig.imageCompression) {
return file
}
// Animated pictures would lose their animation on the canvas
if (await isAnimated(file)) {
return file
}
return new Promise((resolve) => {
const img = new Image()
+ // The browser cannot read it (e.g. HEIC in Chrome): upload it as it is
+ img.onerror = () => {
+ URL.revokeObjectURL(img.src)
+ resolve(file)
+ }
img.onload = () => {
+ URL.revokeObjectURL(img.src)
// Calculate new dimensions
let width = img.width
let height = img.height
const maxSize = 2048
if (width > maxSize || height > maxSize) {
if (width > height) {
height = Math.round((height * maxSize) / width)
width = maxSize
} else {
width = Math.round((width * maxSize) / height)
height = maxSize
}
}
// Create canvas and resize
const canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
const ctx = canvas.getContext('2d')
ctx.drawImage(img, 0, 0, width, height)
// Check WebP support by trying to create a WebP canvas
const testCanvas = document.createElement('canvas')
const supportsWebP = testCanvas
.toDataURL('image/webp')
.startsWith('data:image/webp')
// Convert to WebP if supported and alwaysUseJpeg is false, otherwise JPEG
const type =
!useMergedConfigStore().mergedConfig.alwaysUseJpeg && supportsWebP
? 'image/webp'
: 'image/jpeg'
const extension = type === 'image/webp' ? '.webp' : '.jpg'
// Remove the original extension and add new one
const newFileName = file.name.replace(/\.[^/.]+$/, '') + extension
canvas.toBlob(
(blob) => {
- // Keep the original if compressing did not make it smaller
- if (blob.size >= file.size) {
+ // Keep the original if compressing failed or did not make it smaller
+ if (!blob || blob.size >= file.size) {
resolve(file)
return
}
resolve(
new File([blob], newFileName, {
type,
lastModified: Date.now(),
}),
)
},
type,
0.85,
)
}
img.src = URL.createObjectURL(file)
})
},
async uploadFile(file) {
if (file.size > useInstanceStore().uploadlimit) {
const filesize = fileSizeFormatService.fileSizeFormat(file.size)
const allowedsize = fileSizeFormatService.fileSizeFormat(
useInstanceStore().uploadlimit,
)
this.$emit('upload-failed', 'file_too_big', {
filesize: filesize.num,
filesizeunit: filesize.unit,
allowedsize: allowedsize.num,
allowedsizeunit: allowedsize.unit,
})
return
}
// Resize image if needed
const processedFile = await this.resizeImage(file)
const formData = new FormData()
formData.append('file', processedFile)
this.$emit('uploading')
this.uploadCount++
statusPosterService.uploadMedia({ formData }).then(
(fileData) => {
this.$emit('uploaded', fileData)
this.decreaseUploadCount()
},
(error) => {
console.error('Error uploading file', error)
this.$emit('upload-failed', 'default')
this.decreaseUploadCount()
},
)
},
decreaseUploadCount() {
this.uploadCount--
if (this.uploadCount === 0) {
this.$emit('all-uploaded')
}
},
clearFile() {
this.uploadReady = false
this.$nextTick(() => {
this.uploadReady = true
})
},
multiUpload(files) {
for (const file of files) {
this.uploadFile(file)
}
},
change({ target }) {
this.multiUpload(target.files)
},
},
props: {
dropFiles: Object,
disabled: Boolean,
normalButton: Boolean,
acceptTypes: {
type: String,
default: '*/*',
},
},
watch: {
dropFiles: function (fileInfos) {
if (!this.uploading) {
this.multiUpload(fileInfos)
}
},
},
}
export default mediaUpload
diff --git a/test/unit/specs/components/media_upload.spec.js b/test/unit/specs/components/media_upload.spec.js
index e34a693d0c..5ecf131d30 100644
--- a/test/unit/specs/components/media_upload.spec.js
+++ b/test/unit/specs/components/media_upload.spec.js
@@ -1,37 +1,44 @@
import { createTestingPinia } from '@pinia/testing'
import { mount } from '@vue/test-utils'
import { setActivePinia } from 'pinia'
import { flat, photo } from 'test/fixtures/photo.js'
import { mountOpts } from 'test/fixtures/setup_test.js'
import MediaUpload from 'src/components/media_upload/media_upload.vue'
import { useMergedConfigStore } from 'src/stores/merged_config.js'
const resize = (file) => {
setActivePinia(createTestingPinia())
useMergedConfigStore().mergedConfig = { imageCompression: true }
const wrapper = mount(MediaUpload, mountOpts())
return wrapper.vm.resizeImage(file)
}
describe('MediaUpload image compression', () => {
it('compresses a picture', async () => {
const out = await resize(await photo(100, 100))
expect(out.name).to.match(/^photo\.(webp|jpg)$/)
})
it('uploads the original when the compressed picture is not smaller', async () => {
const picture = await flat(4, 4)
expect(await resize(picture)).to.equal(picture)
})
it('does not rasterize SVG', async () => {
const svg = new File(
['<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"/>'],
'logo.svg',
{ type: 'image/svg+xml' },
)
expect(await resize(svg)).to.equal(svg)
})
+
+ it('uploads the original when the browser cannot read the picture', async () => {
+ const broken = new File(['not a picture'], 'photo.heic', {
+ type: 'image/heic',
+ })
+ expect(await resize(broken)).to.equal(broken)
+ })
})
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Nov 17, 11:30 PM (19 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1839884
Default Alt Text
(8 KB)
Attached To
Mode
rPUFE pleroma-fe-upstream
Attached
Detach File
Event Timeline