Skip to content

Upload images from the CLI and Qiita Preview - #473

Open
tomoasleep wants to merge 11 commits into
increments:mainfrom
tomoasleep:add-image-upload
Open

tomoasleep wants to merge 11 commits into
increments:mainfrom
tomoasleep:add-image-upload

Conversation

@tomoasleep

Copy link
Copy Markdown
Member

What

Qiita CLI から画像をアップロードできるようにする。

  • qiita upload <file> ...: ローカルの画像をアップロードし、記事やスライドに貼れる URL を 1 ファイル 1 行で表示する
  • Qiita Preview の記事・スライドの表示画面のヘッダーに「画像をアップロード」ボタンを追加する。押すとアップロード画面が開き、ドロップかファイル選択でアップロードした画像の URL を一覧・コピーできる
  • README に画像のアップロード方法を追記する

どちらもスライド機能とは関係なく、全てのユーザーが使える。

How

アップロードの手順

Web 版のエディタと同じく、ファイルは Qiita を通さずストレージに直接送る。

  1. POST /api/v2/upload_policies に name / size / content_type を送る
  2. 返ってきた form_data の全キーをそのままフォームに並べ、最後に file を置いて、bucket に POST する
    • file は必ず最後に置く。S3 の POST policy はフィールドの順に評価される
    • region は form_data["x-amz-credential"] から取り出す
  3. 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 が返したメッセージを表示する
  • それ以外の 403(Team 専用モードのユーザーが qiita.com に向けている、など)は、既存の QiitaForbiddenError の案内のままにする
  • ストレージが返すエラー(XML)は Code と Message を表示する

Qiita Preview

  • アクセストークンをブラウザに渡さないため、ブラウザ → preview サーバー(POST /api/images)→ Qiita の経路にした。画像のバイト列は preview サーバーからストレージに直接送る
  • /api/images は /api/slides と違い、スライド機能が使えないときも有効にする
  • そのために uploadImage を、ファイルパスを受け取る uploadImageFile と、バイト列を受け取る uploadImage に分けた
  • ボタンは既存の「スライドショーを開始」と同じグレーの副ボタンにした。モバイル幅ではアイコンだけにする。スライドの画面ではボタンが 3 つになり、モバイル幅でファイルパスのコピーボタンと重なるため、ヘッダーに 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 を実行し、次を確認した
    • PNG をアップロードし、表示された URL から画像を取得できる(コマンド、記事の画面、スライドの画面のそれぞれ)
    • スライド機能を使えないユーザーでも、記事の画面からアップロードできる
    • .webp はリクエストを送らずに拡張子のエラーになる
    • 10MB を超える PNG で、Qiita が返した 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

tomoasleep and others added 11 commits November 11, 2026 17:47
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant