Repository navigation
Upload images from the CLI and Qiita Preview - #473
Open
tomoasleep wants to merge 11 commits into
Open
tomoasleep wants to merge 11 commits into
tomoasleep wants to merge 11 commits into
Conversation
Requests POST /api/v2/upload_policies and posts the file to the S3 bucket with the returned form data, as the web client does. The file field must come last because the S3 POST policy is evaluated in field order, and the policy expires in a minute, so each file gets its own policy right before its upload. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Prints one download URL per line so the URL can be pasted into a slide or piped into another command. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
A 403 from POST /api/v2/upload_policies means the file exceeds the per file size limit or the monthly upload limit, but handleError answered it with the generic advice to check the access token. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Qiita answers 403 both when it rejects the image itself and when the caller may not upload at all, so converting every 403 into ImageUploadRejectedError hid the guidance for the latter. A team-only user running `qiita upload` against qiita.com only saw "Forbidden in team only mode" without a hint of what to do. Convert only the two rejections that describe the image, and let every other 403 reach the existing QiitaForbiddenError handler. Reading the type also drops the raw-body fallback, so a response that is not the JSON we expect no longer leaks a truncated fragment to the user. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Validation messages printed directly by commands are in English, and only the messages that go through handleError are in Japanese. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The preview server needs to upload the bytes it receives from the browser, which never touch the local file system. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Uploading an image does not depend on the slide feature, so the preview server accepts uploads for every user, as the upload command does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Uploading an image is available to every user, but the button was only on the slide preview, which users without the slide feature cannot open. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
What
Qiita CLI から画像をアップロードできるようにする。
qiita upload <file> ...: ローカルの画像をアップロードし、記事やスライドに貼れる URL を 1 ファイル 1 行で表示するどちらもスライド機能とは関係なく、全てのユーザーが使える。
How
アップロードの手順
Web 版のエディタと同じく、ファイルは Qiita を通さずストレージに直接送る。
POST /api/v2/upload_policiesにname/size/content_typeを送るform_dataの全キーをそのままフォームに並べ、最後にfileを置いて、bucketに POST するfileは必ず最後に置く。S3 の POST policy はフィールドの順に評価されるform_data["x-amz-credential"]から取り出すdownload_urlを画像の URL として返すpolicy は 1 分で失効するため、複数のファイルをまとめて policy を先に取らず、ファイルごとにアップロードの直前に取る。
対応する拡張子は avif / gif / jpeg / jpg / png / tif / tiff。それ以外はリクエストを送る前にエラーにする。
エラーの表示
upload_policiesの 403 のうち、画像そのものが理由の 2 種類(asset_size_too_large/monthly_upload_size_limit)はImageUploadRejectedErrorにして、Qiita が返したメッセージを表示するQiitaForbiddenErrorの案内のままにするCodeとMessageを表示するQiita Preview
POST /api/images)→ Qiita の経路にした。画像のバイト列は preview サーバーからストレージに直接送る/api/imagesは/api/slidesと違い、スライド機能が使えないときも有効にするuploadImageを、ファイルパスを受け取るuploadImageFileと、バイト列を受け取るuploadImageに分けたgapを足した動作確認
pnpm test: 272 件パス(この PR での追加は 21 件)pnpm run lint:typecheck/pnpm run lint:eslint/pnpm run lint:prettier/pnpm run buildが通るqiita uploadとqiita previewを実行し、次を確認した.webpはリクエストを送らずに拡張子のエラーになるAsset size too largeが表示されるスクリーンショット
ファイルパスと画像の URL はぼかしています。
記事の画面のヘッダー
画像をアップロードしたあとのアップロード画面
スライドの画面のヘッダー
スライドの画面のヘッダー(モバイル幅)
Why
記事やスライドに画像を入れるとき、Qiita の Web 画面に移らずに、CLI と Qiita Preview から画像の URL を得られるようにするため。
Qiita Preview にはエディタが無いため、Web 版のエディタのように本文へ Markdown を挿入するのではなく、URL を表示してコピーさせる形にした。
qiita publishのときに本文中のローカル画像を自動でアップロードする形にはしなかった。ローカルのパスとアップロード後の URL の対応を持たないと、publish のたびに同じ画像をアップロードし直して、月間のアップロード容量を消費してしまうため。Refs
🤖 Generated with Claude Code