MCPcopy
hub / github.com/claude-code-best/claude-code / uploadFile

Function uploadFile

packages/weixin/src/media.ts:80–133  ·  view source on GitHub ↗
(params: {
  filePath: string
  toUserId: string
  mediaType: number
  apiBaseUrl: string
  token: string
  cdnBaseUrl: string
})

Source from the content-addressed store, hash-verified

78}
79
80export async function uploadFile(params: {
81 filePath: string
82 toUserId: string
83 mediaType: number
84 apiBaseUrl: string
85 token: string
86 cdnBaseUrl: string
87}): Promise<UploadedFileInfo> {
88 const plaintext = readFileSync(params.filePath)
89 const rawSize = plaintext.length
90 const rawMd5 = createHash('md5').update(plaintext).digest('hex')
91 const aesKey = randomBytes(16)
92 const filekey = randomBytes(16).toString('hex')
93 const ciphertext = encryptAesEcb(plaintext, aesKey)
94 const fileSize = ciphertext.length
95
96 const uploadResp = await getUploadUrl(params.apiBaseUrl, params.token, {
97 filekey,
98 media_type: params.mediaType,
99 to_user_id: params.toUserId,
100 rawsize: rawSize,
101 rawfilemd5: rawMd5,
102 filesize: fileSize,
103 no_need_thumb: true,
104 aeskey: aesKey.toString('hex'),
105 })
106
107 if (!uploadResp.upload_param) {
108 throw new Error('No upload_param in response')
109 }
110
111 const uploadUrl = buildCdnUploadUrl(
112 params.cdnBaseUrl,
113 uploadResp.upload_param,
114 filekey,
115 )
116 const uploadResult = await fetch(uploadUrl, {
117 method: 'POST',
118 headers: { 'Content-Type': 'application/octet-stream' },
119 body: new Uint8Array(ciphertext),
120 })
121
122 if (!uploadResult.ok) {
123 throw new Error(`CDN upload failed: HTTP ${uploadResult.status}`)
124 }
125
126 return {
127 encryptQueryParam: uploadResult.headers.get('x-encrypted-param') || '',
128 aesKey: Buffer.from(aesKey.toString('hex')).toString('base64'),
129 fileSize,
130 rawSize,
131 fileName: basename(params.filePath),
132 }
133}
134
135export function guessMediaType(filePath: string): number {
136 const ext = extname(filePath).toLowerCase()

Callers 1

sendMediaFileFunction · 0.70

Calls 7

encryptAesEcbFunction · 0.85
buildCdnUploadUrlFunction · 0.85
fetchFunction · 0.85
getUploadUrlFunction · 0.70
toStringMethod · 0.65
getMethod · 0.65
readFileSyncFunction · 0.50

Tested by

no test coverage detected