Fix WEBP image copying (#24743)

Fix regression from https://github.com/go-gitea/gitea/pull/23801, where
I forgot that the new module will not throw, so the `catch` handlers
were never triggered and in turn, the WEBP was not converted to PNG.
This commit is contained in:
silverwind 2023-05-16 06:45:36 +02:00 committed by GitHub
parent b926f96da7
commit bdd3007c87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,18 +4,14 @@ import {convertImage} from '../utils.js';
const {i18n} = window.config; const {i18n} = window.config;
async function doCopy(content, btn) {
const success = await clippie(content);
showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error);
}
export function initCopyContent() { export function initCopyContent() {
const btn = document.getElementById('copy-content'); const btn = document.getElementById('copy-content');
if (!btn || btn.classList.contains('disabled')) return; if (!btn || btn.classList.contains('disabled')) return;
btn.addEventListener('click', async () => { btn.addEventListener('click', async () => {
if (btn.classList.contains('is-loading')) return; if (btn.classList.contains('is-loading')) return;
let content, isImage; let content;
let isRasterImage = false;
const link = btn.getAttribute('data-link'); const link = btn.getAttribute('data-link');
// when data-link is present, we perform a fetch. this is either because // when data-link is present, we perform a fetch. this is either because
@ -28,7 +24,7 @@ export function initCopyContent() {
const contentType = res.headers.get('content-type'); const contentType = res.headers.get('content-type');
if (contentType.startsWith('image/') && !contentType.startsWith('image/svg')) { if (contentType.startsWith('image/') && !contentType.startsWith('image/svg')) {
isImage = true; isRasterImage = true;
content = await res.blob(); content = await res.blob();
} else { } else {
content = await res.text(); content = await res.text();
@ -43,15 +39,14 @@ export function initCopyContent() {
content = Array.from(lineEls).map((el) => el.textContent).join(''); content = Array.from(lineEls).map((el) => el.textContent).join('');
} }
try { // try copy original first, if that fails and it's an image, convert it to png
await doCopy(content, btn); const success = await clippie(content);
} catch { if (success) {
if (isImage) { // convert image to png as last-resort as some browser only support png copy showTemporaryTooltip(btn, i18n.copy_success);
try { } else {
await doCopy(await convertImage(content, 'image/png'), btn); if (isRasterImage) {
} catch { const success = await clippie(await convertImage(content, 'image/png'));
showTemporaryTooltip(btn, i18n.copy_error); showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error);
}
} else { } else {
showTemporaryTooltip(btn, i18n.copy_error); showTemporaryTooltip(btn, i18n.copy_error);
} }