( ui, draft, slug, branchName, defaultLocale, getSha, )
| 13 | * @return {Promise<void>} |
| 14 | */ |
| 15 | export async function uploadDraftImages( |
| 16 | ui, |
| 17 | draft, |
| 18 | slug, |
| 19 | branchName, |
| 20 | defaultLocale, |
| 21 | getSha, |
| 22 | ) { |
| 23 | for (const img of draft.imageFiles || []) { |
| 24 | const data = await getImage(img.id); |
| 25 | if (!data) continue; |
| 26 | |
| 27 | const content = await bufferToBase64(data); |
| 28 | const imgPath = `content/${defaultLocale}/blog/${slug}/${img.name}`; |
| 29 | const existingImgSha = await getSha(imgPath); |
| 30 | |
| 31 | await ghFetch(ui, `/contents/${imgPath}`, { |
| 32 | method: 'PUT', |
| 33 | body: JSON.stringify({ |
| 34 | message: `${existingImgSha ? 'Update' : 'Add'} image ${img.name}`, |
| 35 | content, |
| 36 | branch: branchName, |
| 37 | sha: existingImgSha, |
| 38 | }), |
| 39 | }); |
| 40 | |
| 41 | // Redundant copies for each locale |
| 42 | if (draft.translations) { |
| 43 | for (const locale of Object.keys(draft.translations)) { |
| 44 | const localeImgPath = `content/${locale}/blog/${slug}/${img.name}`; |
| 45 | const existingLocaleImgSha = await getSha(localeImgPath); |
| 46 | await ghFetch(ui, `/contents/${localeImgPath}`, { |
| 47 | method: 'PUT', |
| 48 | body: JSON.stringify({ |
| 49 | message: `${existingLocaleImgSha ? 'Update' : 'Add'} localized image ${img.name} for ${locale}`, |
| 50 | content, |
| 51 | branch: branchName, |
| 52 | sha: existingLocaleImgSha, |
| 53 | }), |
| 54 | }); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
no test coverage detected