Upload uploader.js
Browse files- src/lib/uploader.js +30 -19
src/lib/uploader.js
CHANGED
|
@@ -16,7 +16,7 @@ class ImageUploader {
|
|
| 16 |
return crypto.createHash('sha256').update(buffer).digest('hex')
|
| 17 |
}
|
| 18 |
|
| 19 |
-
async uploadImage(imageBuffer, imageName) {
|
| 20 |
// 确保参数有效
|
| 21 |
if (!imageBuffer || !Buffer.isBuffer(imageBuffer)) {
|
| 22 |
throw new Error('无效的图片数据')
|
|
@@ -28,14 +28,16 @@ class ImageUploader {
|
|
| 28 |
|
| 29 |
// 生成图片数据的哈希值作为缓存key
|
| 30 |
const imageHash = this.generateHash(imageBuffer)
|
| 31 |
-
|
| 32 |
-
// 检查缓存中是否已存在该图片的上传结果
|
| 33 |
-
if (this.imageCache.has(imageHash)) {
|
| 34 |
-
console.log(`
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
-
|
| 38 |
-
console.log(`
|
| 39 |
|
| 40 |
// 获取一个可用的cookie
|
| 41 |
const cookieValue = accountManager.getNextAvailableCookie()
|
|
@@ -66,11 +68,11 @@ class ImageUploader {
|
|
| 66 |
// 检查响应
|
| 67 |
if (response.data && response.data.location) {
|
| 68 |
const imageUrl = response.data.location
|
| 69 |
-
|
| 70 |
// 将结果保存到缓存
|
| 71 |
this.imageCache.set(imageHash, imageUrl)
|
| 72 |
-
console.log(`
|
| 73 |
-
|
| 74 |
return imageUrl
|
| 75 |
} else {
|
| 76 |
throw new Error('上传成功但返回格式不正确')
|
|
@@ -80,9 +82,9 @@ class ImageUploader {
|
|
| 80 |
if (error.response && error.response.status === 403) {
|
| 81 |
accountManager.markAsUnavailable(cookieValue)
|
| 82 |
// 递归尝试使用新账号
|
| 83 |
-
return this.uploadImage(imageBuffer, imageName)
|
| 84 |
}
|
| 85 |
-
|
| 86 |
throw new Error(`图片上传失败: ${error.message}`)
|
| 87 |
}
|
| 88 |
}
|
|
@@ -156,10 +158,13 @@ class ImageUploader {
|
|
| 156 |
* @param {string} imageName - 图片名称
|
| 157 |
* @param {string} requestId - 请求ID(用于日志)
|
| 158 |
* @param {number} imageIndex - 图片索引(用于日志)
|
|
|
|
| 159 |
* @returns {Promise<Array<string>>} 上传后的URL数组
|
| 160 |
*/
|
| 161 |
-
async uploadImageSmart(imageBuffer, imageName, requestId = null, imageIndex = 0) {
|
| 162 |
try {
|
|
|
|
|
|
|
| 163 |
// 检测是否为长图
|
| 164 |
const detection = await imageProcessor.detectLongImage(imageBuffer)
|
| 165 |
|
|
@@ -169,12 +174,16 @@ class ImageUploader {
|
|
| 169 |
|
| 170 |
if (!detection.isLongImage) {
|
| 171 |
// 不是长图,使用原有方式上传
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
if (requestId) {
|
| 175 |
logger.logImageUpload(requestId, imageIndex, 0, true, url)
|
| 176 |
}
|
| 177 |
|
|
|
|
| 178 |
return [url]
|
| 179 |
}
|
| 180 |
|
|
@@ -262,9 +271,10 @@ class ImageUploader {
|
|
| 262 |
* @param {string} imageName - 图片名称
|
| 263 |
* @param {string} requestId - 请求ID(用于日志)
|
| 264 |
* @param {number} imageIndex - 图片索引(用于日志)
|
|
|
|
| 265 |
* @returns {Promise<Array<string>>} 上传后的URL数组
|
| 266 |
*/
|
| 267 |
-
async uploadFromBase64Smart(base64String, imageName, requestId = null, imageIndex = 0) {
|
| 268 |
// 处理base64字符串,移除可能的前缀
|
| 269 |
const base64Data = base64String.replace(/^data:image\/\w+;base64,/, '')
|
| 270 |
const buffer = Buffer.from(base64Data, 'base64')
|
|
@@ -282,7 +292,7 @@ class ImageUploader {
|
|
| 282 |
imageName = `image_${hash}_${Date.now()}.${fileExt}`
|
| 283 |
}
|
| 284 |
|
| 285 |
-
return this.uploadImageSmart(buffer, imageName, requestId, imageIndex)
|
| 286 |
}
|
| 287 |
|
| 288 |
/**
|
|
@@ -291,9 +301,10 @@ class ImageUploader {
|
|
| 291 |
* @param {string} imageName - 图片名称
|
| 292 |
* @param {string} requestId - 请求ID(用于日志)
|
| 293 |
* @param {number} imageIndex - 图片索引(用于日志)
|
|
|
|
| 294 |
* @returns {Promise<Array<string>>} 上传后的URL数组
|
| 295 |
*/
|
| 296 |
-
async uploadFromUrlSmart(imageUrl, imageName, requestId = null, imageIndex = 0) {
|
| 297 |
try {
|
| 298 |
// 下载图片
|
| 299 |
const response = await axios.get(imageUrl, {
|
|
@@ -316,7 +327,7 @@ class ImageUploader {
|
|
| 316 |
imageName = `image_${hash}_${Date.now()}.${fileExt}`
|
| 317 |
}
|
| 318 |
|
| 319 |
-
return this.uploadImageSmart(buffer, imageName, requestId, imageIndex)
|
| 320 |
} catch (error) {
|
| 321 |
throw new Error(`从URL获取图片失败: ${error.message}`)
|
| 322 |
}
|
|
|
|
| 16 |
return crypto.createHash('sha256').update(buffer).digest('hex')
|
| 17 |
}
|
| 18 |
|
| 19 |
+
async uploadImage(imageBuffer, imageName, forceUpload = false) {
|
| 20 |
// 确保参数有效
|
| 21 |
if (!imageBuffer || !Buffer.isBuffer(imageBuffer)) {
|
| 22 |
throw new Error('无效的图片数据')
|
|
|
|
| 28 |
|
| 29 |
// 生成图片数据的哈希值作为缓存key
|
| 30 |
const imageHash = this.generateHash(imageBuffer)
|
| 31 |
+
|
| 32 |
+
// 检查缓存中是否已存在该图片的上传结果(除非强制上传)
|
| 33 |
+
if (!forceUpload && this.imageCache.has(imageHash)) {
|
| 34 |
+
console.log(`[缓存命中] 图片哈希: ${imageHash.substring(0, 8)}...`)
|
| 35 |
+
const cachedUrl = this.imageCache.get(imageHash)
|
| 36 |
+
console.log(`[缓存返回] URL: ${cachedUrl}`)
|
| 37 |
+
return cachedUrl
|
| 38 |
}
|
| 39 |
+
|
| 40 |
+
console.log(`[开始上传] 图片哈希: ${imageHash.substring(0, 8)}... ${forceUpload ? '(强制上传)' : '(缓存未命中)'}`)
|
| 41 |
|
| 42 |
// 获取一个可用的cookie
|
| 43 |
const cookieValue = accountManager.getNextAvailableCookie()
|
|
|
|
| 68 |
// 检查响应
|
| 69 |
if (response.data && response.data.location) {
|
| 70 |
const imageUrl = response.data.location
|
| 71 |
+
|
| 72 |
// 将结果保存到缓存
|
| 73 |
this.imageCache.set(imageHash, imageUrl)
|
| 74 |
+
console.log(`[上传成功] 图片哈希: ${imageHash.substring(0, 8)}... -> ${imageUrl}`)
|
| 75 |
+
|
| 76 |
return imageUrl
|
| 77 |
} else {
|
| 78 |
throw new Error('上传成功但返回格式不正确')
|
|
|
|
| 82 |
if (error.response && error.response.status === 403) {
|
| 83 |
accountManager.markAsUnavailable(cookieValue)
|
| 84 |
// 递归尝试使用新账号
|
| 85 |
+
return this.uploadImage(imageBuffer, imageName, forceUpload)
|
| 86 |
}
|
| 87 |
+
|
| 88 |
throw new Error(`图片上传失败: ${error.message}`)
|
| 89 |
}
|
| 90 |
}
|
|
|
|
| 158 |
* @param {string} imageName - 图片名称
|
| 159 |
* @param {string} requestId - 请求ID(用于日志)
|
| 160 |
* @param {number} imageIndex - 图片索引(用于日志)
|
| 161 |
+
* @param {boolean} preserveOrder - 是否保持顺序(禁用缓存优化)
|
| 162 |
* @returns {Promise<Array<string>>} 上传后的URL数组
|
| 163 |
*/
|
| 164 |
+
async uploadImageSmart(imageBuffer, imageName, requestId = null, imageIndex = 0, preserveOrder = false) {
|
| 165 |
try {
|
| 166 |
+
console.log(`[智能上传] 开始处理图片${imageIndex + 1},保持顺序: ${preserveOrder}`)
|
| 167 |
+
|
| 168 |
// 检测是否为长图
|
| 169 |
const detection = await imageProcessor.detectLongImage(imageBuffer)
|
| 170 |
|
|
|
|
| 174 |
|
| 175 |
if (!detection.isLongImage) {
|
| 176 |
// 不是长图,使用原有方式上传
|
| 177 |
+
console.log(`[智能上传] 图片${imageIndex + 1}为普通图片,开始上传`)
|
| 178 |
+
|
| 179 |
+
// 如果需要保持顺序,则禁用缓存优化
|
| 180 |
+
const url = await this.uploadImage(imageBuffer, imageName, preserveOrder)
|
| 181 |
|
| 182 |
if (requestId) {
|
| 183 |
logger.logImageUpload(requestId, imageIndex, 0, true, url)
|
| 184 |
}
|
| 185 |
|
| 186 |
+
console.log(`[智能上传] 图片${imageIndex + 1}上传完成: ${url}`)
|
| 187 |
return [url]
|
| 188 |
}
|
| 189 |
|
|
|
|
| 271 |
* @param {string} imageName - 图片名称
|
| 272 |
* @param {string} requestId - 请求ID(用于日志)
|
| 273 |
* @param {number} imageIndex - 图片索引(用于日志)
|
| 274 |
+
* @param {boolean} preserveOrder - 是否保持顺序(禁用缓存优化)
|
| 275 |
* @returns {Promise<Array<string>>} 上传后的URL数组
|
| 276 |
*/
|
| 277 |
+
async uploadFromBase64Smart(base64String, imageName, requestId = null, imageIndex = 0, preserveOrder = false) {
|
| 278 |
// 处理base64字符串,移除可能的前缀
|
| 279 |
const base64Data = base64String.replace(/^data:image\/\w+;base64,/, '')
|
| 280 |
const buffer = Buffer.from(base64Data, 'base64')
|
|
|
|
| 292 |
imageName = `image_${hash}_${Date.now()}.${fileExt}`
|
| 293 |
}
|
| 294 |
|
| 295 |
+
return this.uploadImageSmart(buffer, imageName, requestId, imageIndex, preserveOrder)
|
| 296 |
}
|
| 297 |
|
| 298 |
/**
|
|
|
|
| 301 |
* @param {string} imageName - 图片名称
|
| 302 |
* @param {string} requestId - 请求ID(用于日志)
|
| 303 |
* @param {number} imageIndex - 图片索引(用于日志)
|
| 304 |
+
* @param {boolean} preserveOrder - 是否保持顺序(禁用缓存优化)
|
| 305 |
* @returns {Promise<Array<string>>} 上传后的URL数组
|
| 306 |
*/
|
| 307 |
+
async uploadFromUrlSmart(imageUrl, imageName, requestId = null, imageIndex = 0, preserveOrder = false) {
|
| 308 |
try {
|
| 309 |
// 下载图片
|
| 310 |
const response = await axios.get(imageUrl, {
|
|
|
|
| 327 |
imageName = `image_${hash}_${Date.now()}.${fileExt}`
|
| 328 |
}
|
| 329 |
|
| 330 |
+
return this.uploadImageSmart(buffer, imageName, requestId, imageIndex, preserveOrder)
|
| 331 |
} catch (error) {
|
| 332 |
throw new Error(`从URL获取图片失败: ${error.message}`)
|
| 333 |
}
|