fix(image): 修复图片上传 — token 解析与 OSS 文件 Content-Type#5
Open
kay120 wants to merge 1 commit into
Open
Conversation
The /resource/image/upload_token API returns the token object directly under `data`, but the client expected `data.tokens[]`, so the slice was always empty and image saves failed with "no upload token returned". After fixing the parsing, the OSS POST still failed with 403 because multipart.CreateFormFile hardcodes the file part Content-Type to application/octet-stream, violating the upload policy condition (`["eq", "$Content-Type", "image/png"]`). Set the file part Content-Type explicitly to the token's oss_content_type. Verified end-to-end against the live API: image note now uploads and saves.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题 / Problem
getnote save <image>保存图片笔记时,任何图片都失败,报错:复现:
@getnote/cli@1.1.8(npm 最新),getnote save ./any.png。文字/链接笔记正常,唯独图片失败。根因 / Root cause
实测发现是两处独立的 bug,第二处在修复第一处后才会暴露:
Bug 1 — upload token 响应解析
GET /open/api/v1/resource/image/upload_token服务端实际返回的data是单个 token 对象:{"success":true,"data":{"accessid":"LTAI...","host":"https://...oss...","policy":"...","signature":"...","callback":"...","object_key":"...","access_url":"...","oss_content_type":"image/png"}}但客户端把它当成
{ "tokens": [ ... ] }来解析(ImageUploadTokenData.Tokens []ImageUploadToken),导致Tokens永远为空,于是len(...Tokens)==0→no upload token returned。Bug 2 — OSS 文件 part 的 Content-Type
修好 Bug 1 后,上传到 OSS 仍 403:
multipart.CreateFormFile会把文件 part 的Content-Type硬编码成application/octet-stream,违反 OSS 上传策略里["eq","$Content-Type","image/png"]的条件。需把文件 part 的Content-Type显式设为 token 的oss_content_type。修改 / Changes
internal/client/client.go:ImageUploadTokenResponse.Data改为单个ImageUploadToken(移除多余的ImageUploadTokenData包装层)。cmd/save/save.go:相应改用tokenResp.Data,并以AccessID/Policy非空作为有效性判断。internal/client/client.go:ImageUploadToOSS用CreatePart显式设置文件 part 的Content-Type为oss_content_type,替代CreateFormFile。验证 / Verification
本地
go build ./...+go vet ./...通过,并用真实 API key 端到端实测。完整对照(文本复现):