gpt4 book ai didi

node.js - 如何使用他们的 API 将有效图像保存到 Github?

转载 作者:行者123 更新时间:2023-12-02 19:32:42 25 4
gpt4 key购买 nike

我正在构建一个脚本,将图像作为 base64 字符串上传到 GitHub。但是当我去查看提交时,我的图像并没有在 Github 中显示为图像。

由于我正在提交大数据以上传其他文件 (Markdown),我无法使用 req.files 所以我通过 req.body.imageTest 发送图像 (至少在 Postman 中是这样)。我正在从以下链接之一构建图像字符串。

谁能告诉我:

  1. 在 GitHub WebView 上提供的图像采用什么编码?
  2. 我应该使用什么编码将图像发送到 GitHub?

我看到 OctoKit只接受 utf-8base-64

await octokit.git.createBlob({
owner,
repo,
content: Buffer.from(req.body.imageTest).toString("base64"),
encoding: "base64"
})

https://www.base64-image.de/

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAB6VBMVEUzNDMuMDQxMS03ODdFQy8uMTnTsxdyZiQlKTYnKCp8dFL/6AzSsg47Pknoz1v/1Ab/4AKMex8lJiWenJL/5Sn7zwLxygg8OzErLCtXWFr965z+1An/3AOokRm8vcT/6kxSTSwqLTUnKCdqa2r//dTGqBHY3Or/7XSHiIr///7+1RPeuwxEQjDu8f//7qSRfx2jpKP+2jP0zAZTU1L/9cqslBfDw8L+4mJZUypvcG7+++vMrRDd3dv+65R5bCOPkI/mwQtDREP09PP/9MOVghytra1JRi5bXFv/+eW2nBXKy8tmXSd6e3r92Cvk5OT/8ryEdCCSkpL/4lsvMC9LTEudiBuzs7P//fX/6oJkZGS+ohPU1NRtYiaCg4L+4VQ7PDvt7e3/+tybm5vrxQlAPzD/2SQjJze8vbz/8rRNSS1WUSv/30v/99n4zgh0aCT/53zhvQv+1xz/8K2ahhz+3UO0mhf/99RjWihZUSRyc3L/5nT/76lHSEe5nhT/5ml+cCL/3TslKjmijBq3uLf73ls/QD3/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SkpJ2FChrhZVrAq7jjqWivqOkE8lJ/dhfG/uEoiEsl7uA4K7omg7qsGVqF9E+hNgCjK0VIUbTkpKOIOngVeZEp7JeuRAt/w/+fs7ixk84tgAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxOC0wMy0yMlQxMjo0NjoyMyswMDowMACpANoAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTgtMDMtMjJUMTI6NDY6MjMrMDA6MDBx9LhmAAAAAElFTkSuQmCC

上面两个链接是上传同一张图片,为什么字符串不同?

如果有帮助,这里是实际图片:

test image

Github 上的图片(应该显示为图片预览,而不是代码)是:

enter image description here

最佳答案

GitHub API 支持使用图像创建提交,我认为您只需要注意读取字节和发送字节的方式即可。

这是创建新存储库并创建包含示例图像的提交的示例脚本:

const fs = require('fs')
const path = require('path')
const util = require('util')

const readFilePromise = util.promisify(fs.readFile)

// API client for working with GitHub data using promises
const { Octokit } = require("@octokit/rest");

const token = process.env["GITHUB_TOKEN"];

if (token == null) {
console.error("Expected GITHUB_TOKEN environment variable to create repository. Exiting...")
process.exit(-1)
}

async function run() {
const octokit = new Octokit({
auth: token,
log: console,
});

// change these values to create the test repository under your account
const owner = "shiftkey";
const repo = "upload-an-image-test-repo";

await octokit.repos.createForAuthenticatedUser({
name: repo,
description: "testing uploading an image through the GitHub API",
})

// ensure that you are reading the file from disk as binary before converting
// to base64
const imagePath = path.join(__dirname, 'example.png')
const bytes = await readFilePromise(imagePath, 'binary')
const buffer = Buffer.from(bytes, 'binary')
const content = buffer.toString('base64')

// TODO: updating file requires providing the SHA of existing blob
// this is not currently supported
const result = await octokit.repos.createOrUpdateFile({
owner,
repo,
message: "Adding an image to the repository",
path: 'example.png',
content,
})

console.log(`Created commit at ${result.data.commit.html_url}`)
}

run()
.catch(err => {
console.error(err, err.stack)
})

这是它在 GitHub 上的样子:

uploaded image shown in rich preview mode

关于node.js - 如何使用他们的 API 将有效图像保存到 Github?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61553399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com