|
|
|
|
|
|
|
function uploadImageAndGetFullUrl(uploadEndpoint, hostUrl, file) { |
|
const formData = new FormData(); |
|
|
|
|
|
function compressImage(file) { |
|
const options = { |
|
maxSizeMB: 5, |
|
maxWidthOrHeight: 1920, |
|
useWebWorker: true |
|
}; |
|
|
|
return imageCompression(file, options) |
|
.then(compressedFile => { |
|
console.log(`原始文件大小:${file.size / 1024 / 1024} MB`); |
|
console.log(`压缩后文件大小:${compressedFile.size / 1024 / 1024} MB`); |
|
return compressedFile; |
|
}) |
|
.catch(error => { |
|
console.error('图片压缩失败:', error); |
|
return file; |
|
}); |
|
} |
|
|
|
|
|
return compressImage(file).then(compressedFile => { |
|
formData.append("file", compressedFile); |
|
return fetch(`${hostUrl}${uploadEndpoint}`, { |
|
method: 'POST', |
|
body: formData |
|
}); |
|
}); |
|
} |
|
|
|
|
|
const hostUrl = 'https://upimg.wook.eu.org'; |
|
const uploadEndpoint = '/upload'; |
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
const form = document.querySelector('form'); |
|
const urlTextArea = document.querySelector('textarea[name="imageUrls"]'); |
|
const addButton = document.querySelector('input[name="add"]'); |
|
const realFileInput = document.createElement('input'); |
|
realFileInput.type = 'file'; |
|
realFileInput.multiple = true; |
|
realFileInput.style.display = 'none'; |
|
|
|
|
|
document.body.appendChild(realFileInput); |
|
|
|
|
|
addButton.addEventListener('click', function(event) { |
|
if (urlTextArea.value.trim() === '') { |
|
event.preventDefault(); |
|
realFileInput.click(); |
|
} |
|
}); |
|
|
|
|
|
realFileInput.addEventListener('change', function() { |
|
const uploadPromises = Array.from(realFileInput.files).map(file => { |
|
|
|
return uploadImageAndGetFullUrl(uploadEndpoint, hostUrl, file) |
|
.then(response => { |
|
if (!response.ok) { |
|
throw new Error('网络响应不是OK状态'); |
|
} |
|
return response.json(); |
|
}) |
|
.then(data => { |
|
if (data.error) { |
|
throw new Error(data.error); |
|
} |
|
return `${hostUrl}${data.src}`; |
|
}); |
|
}); |
|
|
|
Promise.all(uploadPromises) |
|
.then(fullUrls => { |
|
|
|
urlTextArea.value = fullUrls.join('\n'); |
|
|
|
|
|
form.submit(); |
|
}) |
|
.catch(error => { |
|
console.error('上传失败:', error); |
|
}); |
|
}); |
|
}); |
|
|
|
function confirmDelete() { |
|
return confirm('确定要删除这张图片吗?'); |
|
} |
|
|