fix: github image save

This commit is contained in:
DomWane 2024-09-16 11:10:41 +02:00
parent 241776eaf1
commit 67660b0972
6 changed files with 67 additions and 16 deletions

View file

@ -149,6 +149,7 @@ export const useData = defineStore('data', () => {
function useProject() { function useProject() {
const project = ref<Partial<Project>>() const project = ref<Partial<Project>>()
const projectImage = ref<File>() const projectImage = ref<File>()
const isPublishing = ref(false)
function setProject(id: string) { function setProject(id: string) {
project.value = getProjectById(id, { shallow: false }) as Project project.value = getProjectById(id, { shallow: false }) as Project
@ -170,18 +171,25 @@ export const useData = defineStore('data', () => {
} }
async function publishProject() { async function publishProject() {
isPublishing.value = true
try { try {
const imageArrayBuffer = await projectImage.value?.arrayBuffer() const imageArrayBuffer = await projectImage.value?.arrayBuffer()
let imageBuffer: Buffer | undefined let imageBuffer: Buffer | undefined
if (imageArrayBuffer) let base64Image: string | undefined
if (imageArrayBuffer) {
imageBuffer = Buffer.from(imageArrayBuffer) imageBuffer = Buffer.from(imageArrayBuffer)
const base64String = imageBuffer.toString('base64')
base64Image = base64String
}
await $fetch(`/api/data`, { await $fetch(`/api/data`, {
method: 'POST', method: 'POST',
body: { body: {
project: project.value, project: project.value,
image: { image: {
type: projectImage.value?.type, type: projectImage.value?.type,
data: imageBuffer?.toString('base64'), data: base64Image,
}, },
}, },
}) })
@ -189,10 +197,14 @@ export const useData = defineStore('data', () => {
catch (e) { catch (e) {
console.error(e) console.error(e)
} }
finally {
isPublishing.value = false
}
} }
return { return {
project, project,
isPublishing,
setProject, setProject,
clearProject, clearProject,
saveProject, saveProject,

View file

@ -19,6 +19,7 @@
"@formkit/auto-animate": "^0.8.2", "@formkit/auto-animate": "^0.8.2",
"@iconify-json/heroicons-outline": "^1.2.0", "@iconify-json/heroicons-outline": "^1.2.0",
"@iconify-json/heroicons-solid": "^1.2.0", "@iconify-json/heroicons-solid": "^1.2.0",
"@iconify-json/eos-icons": "^1.1.10",
"@nuxt/devtools": "^1.4.1", "@nuxt/devtools": "^1.4.1",
"@nuxt/eslint": "0.5.5", "@nuxt/eslint": "0.5.5",
"@nuxt/image": "^1.8.0", "@nuxt/image": "^1.8.0",

View file

@ -14,7 +14,7 @@ definePageMeta({
}) })
const { useProject, projects } = useData() const { useProject, projects } = useData()
const { saveProject, setProject, project, publishProject, saveProjectImage } = useProject() const { saveProject, setProject, project, publishProject, saveProjectImage, isPublishing } = useProject()
const route = useRoute() const route = useRoute()
await until(projects).toMatch(p => p?.length > 0) await until(projects).toMatch(p => p?.length > 0)
@ -323,9 +323,19 @@ function next() {
w-full w-full
lg="w-fit" lg="w-fit"
inverted-color inverted-color
@click="publishProject()" @click="isPublishing ? () => null : publishProject()"
> >
<span px-24px>PUBLISH</span> <UnoIcon
v-if="isPublishing"
w-108px
i-eos-icons-loading
text-black
text-18px
/>
<span
v-else
px-24px
>PUBLISH</span>
</Button> </Button>
</div> </div>
</div> </div>

View file

@ -76,7 +76,7 @@ function useProjectName() {
const { isEditing, name, toggleEdit } = useProjectName() const { isEditing, name, toggleEdit } = useProjectName()
const { useProject } = useData() const { useProject } = useData()
const { saveProject, publishProject, saveProjectImage } = useProject() const { saveProject, publishProject, saveProjectImage, isPublishing } = useProject()
function save() { function save() {
saveProject({ saveProject({
@ -309,9 +309,19 @@ function next() {
w-full w-full
lg="w-fit" lg="w-fit"
inverted-color inverted-color
@click="publishProject()" @click="isPublishing ? () => null : publishProject()"
> >
<span px-24px>PUBLISH</span> <UnoIcon
v-if="isPublishing"
w-108px
i-eos-icons-loading
text-black
text-18px
/>
<span
v-else
px-24px
>PUBLISH</span>
</Button> </Button>
</div> </div>
</div> </div>

View file

@ -11,6 +11,9 @@ importers:
'@formkit/auto-animate': '@formkit/auto-animate':
specifier: ^0.8.2 specifier: ^0.8.2
version: 0.8.2 version: 0.8.2
'@iconify-json/eos-icons':
specifier: ^1.1.10
version: 1.2.0
'@iconify-json/heroicons-outline': '@iconify-json/heroicons-outline':
specifier: ^1.2.0 specifier: ^1.2.0
version: 1.2.0 version: 1.2.0
@ -1100,6 +1103,9 @@ packages:
resolution: {integrity: sha512-d2CGZR2o7fS6sWB7DG/3a95bGKQyHMACZ5aW8qGkkqQpUoZV6C0X7Pc7l4ZNMZkfNBf4VWNe9E1jRsf0G146Ew==} resolution: {integrity: sha512-d2CGZR2o7fS6sWB7DG/3a95bGKQyHMACZ5aW8qGkkqQpUoZV6C0X7Pc7l4ZNMZkfNBf4VWNe9E1jRsf0G146Ew==}
engines: {node: '>=18.18'} engines: {node: '>=18.18'}
'@iconify-json/eos-icons@1.2.0':
resolution: {integrity: sha512-grdfoS20Z4gWAzNPza7ytguNBWeTOkx4Y6aZHs149t2Z6AhW7zG3VWkkq6M+YuL2G8ugHnBw7ZxgazZ6oiMnIQ==}
'@iconify-json/heroicons-outline@1.2.0': '@iconify-json/heroicons-outline@1.2.0':
resolution: {integrity: sha512-Qy1sRmQYqih6xRxwCtnX0hXJ4252t83C0CnNWAP3gF0fH0Qmp9RY66LMB0moYGxQxUhsTFIl2nNceSVSBUo8Tg==} resolution: {integrity: sha512-Qy1sRmQYqih6xRxwCtnX0hXJ4252t83C0CnNWAP3gF0fH0Qmp9RY66LMB0moYGxQxUhsTFIl2nNceSVSBUo8Tg==}
@ -6576,6 +6582,10 @@ snapshots:
'@humanwhocodes/retry@0.3.0': {} '@humanwhocodes/retry@0.3.0': {}
'@iconify-json/eos-icons@1.2.0':
dependencies:
'@iconify/types': 2.0.0
'@iconify-json/heroicons-outline@1.2.0': '@iconify-json/heroicons-outline@1.2.0':
dependencies: dependencies:
'@iconify/types': 2.0.0 '@iconify/types': 2.0.0

View file

@ -19,7 +19,7 @@ export default defineEventHandler(async (event) => {
const owner = 'develit-io' const owner = 'develit-io'
const repo = 'test-repo' const repo = 'test-repo'
const baseBranch = 'main' const baseBranch = 'main'
const newBranchName = `${id}-project-update` const newBranchName = `${id}-project-update-${Date.now()}`
const commitMessage = `${body.project.id ? `Updating the project: ${body.project.name}` : `Initiating the creation of project: ${body.project.name}`}` const commitMessage = `${body.project.id ? `Updating the project: ${body.project.name}` : `Initiating the creation of project: ${body.project.name}`}`
const files = [ const files = [
@ -55,7 +55,7 @@ export default defineEventHandler(async (event) => {
}) })
} }
async function commitChangesToNewBranch(owner: string, repo: string, newBranch: string, message: string, files: { path: string, content: string }[]) { async function commitChangesToNewBranch(owner: string, repo: string, newBranch: string, message: string, files: { path: string, content: string, encoding: string }[]) {
const { data: latestCommit } = await octokit.rest.repos.getCommit({ const { data: latestCommit } = await octokit.rest.repos.getCommit({
owner, owner,
repo, repo,
@ -68,18 +68,26 @@ export default defineEventHandler(async (event) => {
tree_sha: latestCommit.commit.tree.sha, tree_sha: latestCommit.commit.tree.sha,
}) })
const tree = files.map(file => ({ const blobs = await Promise.all(files.map(async (file) => {
path: file.path, const { data: blob } = await octokit.rest.git.createBlob({
mode: '100644' as const, owner,
type: 'blob' as const, repo,
content: file.content, content: file.content,
encoding: file.encoding,
})
return {
path: file.path,
mode: '100644' as const,
type: 'blob' as const,
sha: blob.sha,
}
})) }))
const { data: newTree } = await octokit.rest.git.createTree({ const { data: newTree } = await octokit.rest.git.createTree({
owner, owner,
repo, repo,
base_tree: baseTree.sha, base_tree: baseTree.sha,
tree, tree: blobs,
}) })
const { data: newCommit } = await octokit.rest.git.createCommit({ const { data: newCommit } = await octokit.rest.git.createCommit({