|
|
|
|
|
|
|
function uploadImageAndGetFullUrl(uploadEndpoint, hostUrl, file) { |
|
const formData = new FormData(); |
|
|
|
|
|
function handleCompressFile(file) { |
|
const maxFileSize = 5 * 1024 * 1024; |
|
return new Promise((resolve) => { |
|
if (file.size <= maxFileSize || !file.type.startsWith("image")) { |
|
resolve(file); |
|
} else { |
|
imageCompression(file, { maxSizeMB: 5 }) |
|
.then((compressedFile) => { |
|
resolve(compressedFile); |
|
}) |
|
.catch((error) => { |
|
console.error(">> imageCompression error", error); |
|
resolve(file); |
|
}); |
|
} |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
const uphostUrl = 'https://testupimg.wook.eu.org'; |
|
|
|
return handleCompressFile(file).then(compressedFile => { |
|
formData.append("file", compressedFile); |
|
return fetch(`${uphostUrl}${uploadEndpoint}`, { |
|
method: 'POST', |
|
body: formData |
|
}); |
|
}); |
|
} |
|
|
|
|
|
const hostUrl = 'https://imghost.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); |
|
} |
|
|
|
const imageUrl = `${hostUrl}${data[0].src}`; |
|
|
|
console.log(imageUrl); |
|
|
|
return imageUrl; |
|
}); |
|
}); |
|
|
|
Promise.all(uploadPromises) |
|
.then(fullUrls => { |
|
|
|
urlTextArea.value = fullUrls.join('\n'); |
|
|
|
|
|
|
|
}) |
|
.catch(error => { |
|
console.error('上传失败:', error); |
|
}); |
|
}); |
|
}); |
|
|
|
function confirmDelete() { |
|
return confirm('确定要删除这张图片吗?'); |
|
} |
|
|