| 1 | import { apiClient, apiErrorMessage } from "@/lib/api/client"; |
| 2 | import { API_URL } |
| 3 | import { attachmentMarkdown } |
| 4 | |
| 5 | /** Sends a file's bytes straight from the browser to the API and returns the Markdown that embeds it. */ |
| 6 | export async function uploadAttachment |
| 7 | username, |
| 8 | repo, |
| 9 | file: picked, |
| 10 | }: { |
| 11 | username: string |
| 12 | repo: string |
| 13 | file: File |
| 14 | }) { |
| 15 | const file = |
| 16 | const { data, |
| 17 | "/api/repositories/{username}/{repo}/attachments", |
| 18 | { |
| 19 | params: { path: { |
| 20 | // always octet-stream: the API parses JSON and form bodies before they could be streamed |
| 21 | headers: { "Content-Type": |
| 22 | body: file as |
| 23 | bodySerializer: (body) |
| 24 | }, |
| 25 | ); |
| 26 | |
| 27 | if (error) throw |
| 28 | return attachmentMarkdown( |
| 29 | data.name, |
| 30 | `${API_URL}/api/attachments/${data |
| 31 | data.contentType, |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | const HEIC = / |
| 36 | |
| 37 | /** iPhone photos arrive as HEIC, which only Safari can draw: anywhere else the embed would show a broken image. */ |
| 38 | function isHeic(file: |
| 39 | return HEIC.test |
| 40 | } |
| 41 | |
| 42 | async function toJpeg( |
| 43 | // a few MB of wasm, so only fetched once someone attaches a HEIC photo |
| 44 | const { heicTo |
| 45 | const jpeg = |
| 46 | return new File |
| 47 | type: "image/jpeg", |
| 48 | }); |
| 49 | } |