File size: 12,467 Bytes
216802b
 
 
 
453b505
 
216802b
 
 
 
 
 
 
 
 
 
 
 
0b04ef0
216802b
 
 
 
 
 
 
 
 
 
 
3b15875
0b04ef0
 
 
 
216802b
3b15875
0b04ef0
216802b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b15875
216802b
 
0b04ef0
3b15875
216802b
 
 
 
 
 
 
 
 
0b04ef0
216802b
3b15875
216802b
 
 
 
 
 
 
 
453b505
216802b
 
 
 
 
 
453b505
216802b
 
 
 
 
453b505
 
216802b
 
 
 
 
 
 
 
453b505
216802b
 
 
 
 
 
453b505
216802b
 
453b505
216802b
 
 
 
 
453b505
 
216802b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
453b505
 
 
 
 
 
 
 
 
0b04ef0
453b505
 
 
 
 
 
 
 
 
 
0b04ef0
453b505
 
 
 
 
 
 
 
 
 
 
0b04ef0
 
453b505
0b04ef0
 
 
453b505
0b04ef0
 
 
 
 
 
453b505
0b04ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
453b505
0b04ef0
453b505
0b04ef0
 
c666a95
0b04ef0
 
 
 
 
453b505
c666a95
0b04ef0
 
 
 
 
c666a95
0b04ef0
 
 
 
 
 
 
 
 
 
 
 
 
453b505
0b04ef0
 
c666a95
0b04ef0
 
 
 
 
 
 
 
 
 
453b505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b15875
453b505
 
3b15875
453b505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b15875
453b505
 
 
 
 
 
 
 
3b15875
453b505
 
3b15875
453b505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b15875
453b505
 
 
 
216802b
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
const axios = require('axios')
const FormData = require('form-data')
const crypto = require('crypto')
const accountManager = require('./manager')
const imageProcessor = require('./imageProcessor')
const logger = require('./logger')

class ImageUploader {
  constructor() {
    this.uploadUrl = 'https://mammouth.ai/api/attachments/saveFile'
    this.imageCache = new Map()
  }


  generateHash(buffer) {
    return crypto.createHash('sha256').update(buffer).digest('hex')
  }

  async uploadImage(imageBuffer, imageName) {
    // 确保参数有效
    if (!imageBuffer || !Buffer.isBuffer(imageBuffer)) {
      throw new Error('无效的图片数据')
    }

    if (!imageName) {
      imageName = `image_${Date.now()}.png`
    }

    // 生成图片数据的哈希值作为缓存key
    const imageHash = this.generateHash(imageBuffer)

    // 检查缓存中是否已存在该图片的上传结果
    if (this.imageCache.has(imageHash)) {
      console.log(`图片缓存命中: ${imageHash.substring(0, 8)}...`)
      return this.imageCache.get(imageHash)
    }

    console.log(`图片缓存未命中,开始上传: ${imageHash.substring(0, 8)}...`)

    // 获取一个可用的cookie
    const cookieValue = accountManager.getNextAvailableCookie()
    if (!cookieValue) {
      throw new Error('没有可用的账号')
    }

    // 创建FormData对象
    const form = new FormData()
    form.append('type', 'image')
    form.append('name', imageName)
    form.append('file', imageBuffer, {
      filename: 'blob',
      contentType: 'image/png' // 默认为PNG,可根据实际情况调整
    })

    try {
      // 发送请求
      const response = await axios.post(this.uploadUrl, form, {
        headers: {
          ...form.getHeaders(),
          'Cookie': `auth_session=${cookieValue}`,
          'Origin': 'https://mammouth.ai',
          'Referer': 'https://mammouth.ai/app/a/default'
        }
      })

      // 检查响应
      if (response.data && response.data.location) {
        const imageUrl = response.data.location

        // 将结果保存到缓存
        this.imageCache.set(imageHash, imageUrl)
        console.log(`图片上传成功并缓存: ${imageHash.substring(0, 8)}... -> ${imageUrl}`)

        return imageUrl
      } else {
        throw new Error('上传成功但返回格式不正确')
      }
    } catch (error) {
      // 如果是账号限制问题,尝试其他账号
      if (error.response && error.response.status === 403) {
        accountManager.markAsUnavailable(cookieValue)
        // 递归尝试使用新账号
        return this.uploadImage(imageBuffer, imageName)
      }

      throw new Error(`图片上传失败: ${error.message}`)
    }
  }

  async uploadFromBase64(base64String, imageName) {
    // 处理base64字符串,移除可能的前缀
    const base64Data = base64String.replace(/^data:image\/\w+;base64,/, '')
    const buffer = Buffer.from(base64Data, 'base64')
    
    // 如果提供了前缀,则从中提取图片类型
    let fileExt = 'png'
    const mimeMatch = base64String.match(/^data:image\/(\w+);base64,/)
    if (mimeMatch && mimeMatch[1]) {
      fileExt = mimeMatch[1]
    }
    
    // 如果没有提供文件名,则使用哈希值的前8位和时间戳生成
    if (!imageName) {
      const hash = this.generateHash(buffer).substring(0, 8)
      imageName = `image_${hash}_${Date.now()}.${fileExt}`
    }
    
    return this.uploadImage(buffer, imageName)
  }

  async uploadFromUrl(imageUrl, imageName) {
    try {
      // 下载图片
      const response = await axios.get(imageUrl, {
        responseType: 'arraybuffer'
      })
      
      // 从URL中提取文件扩展名
      let fileExt = 'png'
      const urlMatch = imageUrl.match(/\.([a-zA-Z0-9]+)(?:\?.*)?$/)
      if (urlMatch && urlMatch[1]) {
        fileExt = urlMatch[1].toLowerCase()
      }
      
      // 生成buffer并计算哈希
      const buffer = Buffer.from(response.data)
      
      // 如果没有提供文件名,则使用哈希值的前8位和时间戳生成
      if (!imageName) {
        const hash = this.generateHash(buffer).substring(0, 8)
        imageName = `image_${hash}_${Date.now()}.${fileExt}`
      }
      
      return this.uploadImage(buffer, imageName)
    } catch (error) {
      throw new Error(`从URL获取图片失败: ${error.message}`)
    }
  }
  
  getCacheStats() {
    return {
      size: this.imageCache.size,
      keys: Array.from(this.imageCache.keys()).map(k => k.substring(0, 8) + '...')
    }
  }
  
  clearCache() {
    const oldSize = this.imageCache.size
    this.imageCache.clear()
    return { cleared: oldSize }
  }

  /**

   * 智能上传图片(支持长图处理)

   * @param {Buffer} imageBuffer - 图片缓冲区

   * @param {string} imageName - 图片名称

   * @param {string} requestId - 请求ID(用于日志)

   * @param {number} imageIndex - 图片索引(用于日志)

   * @returns {Promise<Array<string>>} 上传后的URL数组

   */
  async uploadImageSmart(imageBuffer, imageName, requestId = null, imageIndex = 0) {
    try {
      // 检测是否为长图
      const detection = await imageProcessor.detectLongImage(imageBuffer)

      if (requestId) {
        logger.logLongImageDetection(requestId, imageIndex, detection)
      }

      if (!detection.isLongImage) {
        // 不是长图,使用原有方式上传
        const url = await this.uploadImage(imageBuffer, imageName)

        if (requestId) {
          logger.logImageUpload(requestId, imageIndex, 0, true, url)
        }

        return [url]
      }

      // 是长图,进行切割处理
      console.log(`检测到长图 ${detection.width}x${detection.height},开始切割处理`)

      try {
        const segments = await imageProcessor.cropLongImage(imageBuffer)

        if (!segments || segments.length === 0) {
          throw new Error('长图切割失败:未生成任何片段')
        }

        if (requestId) {
          logger.logImageCropping(requestId, imageIndex, {
            segments,
            stats: imageProcessor.getProcessingStats(segments)
          })
        }

        // 并行上传切割后的片段
        const uploadPromises = segments.map(async (segment) => {
          const segmentName = imageProcessor.generateSegmentName(
            imageName,
            segment.metadata.segmentIndex,
            segment.metadata.totalSegments
          )

          try {
            const url = await this.uploadImage(segment.buffer, segmentName)

            if (requestId) {
              logger.logImageUpload(requestId, imageIndex, segment.metadata.segmentIndex, true, url)
            }

            console.log(`片段 ${segment.metadata.segmentIndex + 1}/${segment.metadata.totalSegments} 上传成功: ${url}`)
            return {
              url,
              segmentIndex: segment.metadata.segmentIndex,
              success: true
            }
          } catch (error) {
            if (requestId) {
              logger.logImageUpload(requestId, imageIndex, segment.metadata.segmentIndex, false, null, error.message)
            }

            console.error(`片段 ${segment.metadata.segmentIndex + 1} 上传失败: ${error.message}`)
            return {
              error: error.message,
              segmentIndex: segment.metadata.segmentIndex,
              success: false
            }
          }
        })

        // 等待所有上传完成
        const uploadResults = await Promise.all(uploadPromises)

        // 检查是否有失败的上传
        const failedUploads = uploadResults.filter(result => !result.success)
        if (failedUploads.length > 0) {
          const failedIndexes = failedUploads.map(result => result.segmentIndex + 1).join(', ')
          throw new Error(`片段 ${failedIndexes} 上传失败`)
        }

        // 按照片段顺序排序并提取URL
        const sortedResults = uploadResults
          .filter(result => result.success)
          .sort((a, b) => a.segmentIndex - b.segmentIndex)
          .map(result => result.url)

        console.log(`长图切割并行上传完成,共 ${sortedResults.length} 个片段`)
        return sortedResults

      } catch (longImageError) {
        // 长图处理失败,回退到原图上传
        console.warn(`长图处理失败: ${longImageError.message},回退到原图上传`)

        if (requestId) {
          logger.logError(requestId, 'LONG_IMAGE_FALLBACK', `长图处理失败,回退到原图: ${longImageError.message}`, {
            imageIndex,
            originalSize: `${detection.width}x${detection.height}`
          })
        }

        try {
          const fallbackUrl = await this.uploadImage(imageBuffer, imageName)

          if (requestId) {
            logger.logImageUpload(requestId, imageIndex, 0, true, fallbackUrl, '长图回退上传')
          }

          console.log(`长图回退上传成功: ${fallbackUrl}`)
          return [fallbackUrl]
        } catch (fallbackError) {
          throw new Error(`长图处理和回退上传均失败: ${longImageError.message} | ${fallbackError.message}`)
        }
      }

    } catch (error) {
      if (requestId) {
        logger.logError(requestId, 'IMAGE_UPLOAD_ERROR', error.message, {
          imageIndex,
          imageName
        })
      }
      throw error
    }
  }

  /**

   * 智能上传Base64图片(支持长图处理)

   * @param {string} base64String - Base64字符串

   * @param {string} imageName - 图片名称

   * @param {string} requestId - 请求ID(用于日志)

   * @param {number} imageIndex - 图片索引(用于日志)

   * @param {boolean} preserveOrder - 是否保持顺序(禁用缓存优化)

   * @returns {Promise<Array<string>>} 上传后的URL数组

   */
  async uploadFromBase64Smart(base64String, imageName, requestId = null, imageIndex = 0, preserveOrder = false) {
    // 处理base64字符串,移除可能的前缀
    const base64Data = base64String.replace(/^data:image\/\w+;base64,/, '')
    const buffer = Buffer.from(base64Data, 'base64')

    // 如果提供了前缀,则从中提取图片类型
    let fileExt = 'png'
    const mimeMatch = base64String.match(/^data:image\/(\w+);base64,/)
    if (mimeMatch && mimeMatch[1]) {
      fileExt = mimeMatch[1]
    }

    // 如果没有提供文件名,则使用哈希值的前8位和时间戳生成
    if (!imageName) {
      const hash = this.generateHash(buffer).substring(0, 8)
      imageName = `image_${hash}_${Date.now()}.${fileExt}`
    }

    return this.uploadImageSmart(buffer, imageName, requestId, imageIndex, preserveOrder)
  }

  /**

   * 智能上传URL图片(支持长图处理)

   * @param {string} imageUrl - 图片URL

   * @param {string} imageName - 图片名称

   * @param {string} requestId - 请求ID(用于日志)

   * @param {number} imageIndex - 图片索引(用于日志)

   * @param {boolean} preserveOrder - 是否保持顺序(禁用缓存优化)

   * @returns {Promise<Array<string>>} 上传后的URL数组

   */
  async uploadFromUrlSmart(imageUrl, imageName, requestId = null, imageIndex = 0, preserveOrder = false) {
    try {
      // 下载图片
      const response = await axios.get(imageUrl, {
        responseType: 'arraybuffer'
      })

      // 从URL中提取文件扩展名
      let fileExt = 'png'
      const urlMatch = imageUrl.match(/\.([a-zA-Z0-9]+)(?:\?.*)?$/)
      if (urlMatch && urlMatch[1]) {
        fileExt = urlMatch[1].toLowerCase()
      }

      // 生成buffer并计算哈希
      const buffer = Buffer.from(response.data)

      // 如果没有提供文件名,则使用哈希值的前8位和时间戳生成
      if (!imageName) {
        const hash = this.generateHash(buffer).substring(0, 8)
        imageName = `image_${hash}_${Date.now()}.${fileExt}`
      }

      return this.uploadImageSmart(buffer, imageName, requestId, imageIndex, preserveOrder)
    } catch (error) {
      throw new Error(`从URL获取图片失败: ${error.message}`)
    }
  }
}


module.exports = new ImageUploader()