Typora 中使用自己的图片上传接口

Coding 文件上传

通过 Coding 的开放 API 实现文件上传。
源码:https://gitee.com/r4c12/coding.net-uploader

从 Coding 本身的 开放 API 介绍 无法知道想要的接口,只能通过分析网页加载过程中的 URL 调用才能得到。

获取 token

  1. 在个人设置中的 访问令牌 标签页点击 新建令牌 获取 token。
    获取token-1
  2. 选中 project:file 读/写 授权读取与操作文件 即可
    获取token-2
  3. 输入密码验证后,复制生成的 token
    获取token-3

依赖

  • requests
  • Python >=3.6

调用

from uploader import Uploader

uploader = Uploader("{token}", "{team_name}", "{user_name}")
uploader.upload_file(
            "{file}",
            "{project_name}",
            "{repo_name}",
            "{branch}"
        )

或者直接执行:

python test_uploader.py --config-file config.json --upload-files "文件1" "文件2" ...

上传的文件的名称重命名为了文件的 md5 值,即默认 md5 值相同的两个文件是同一个文件。执行后会输出文件的 URL。

在 Typora 中使用接口

Typora 是一个方便的并且跨平台的 Markdown 写作工作,在最近的版本中可以进行 图片上传。官方文档中介绍了一些工具,但是也可以使用自定义的命令进行上传。

You could config a custom command to upload images, using tools that is not listed in above options, or event write your own tools / scripts. Typora will append all images that needs to be uploaded after the custom command you filled.

Then, Typora will fetch image urls from the last N lines of the standard output of your custom command. (N is the number of images to upload).

For example, if you write a tool upload-image.sh, then you can input [some path]/upload-image.sh in the command filed. Typora will call [some path]/upload-image.sh “image-path-1” “image-path-2” to upload two images located in image-path-1 and image-path-2. Then the command may return something like:

Upload Success:
http://remote-image-1.png
http://remote-image-2.png

Then Typora will get the two remote image url from the output, and replace the original local images used in the Markdown document.

You could click the “Test Uploader” button to verify your custom commands.

可以作为图片的 Git 平台中,有国外的 Github 和 Gitlab,以及国内的 Gitee 和 Coding。

Github 虽然可以通过 https://www.jsdelivr.com/ 加快图片下载速度,但是上传的接口很受限,国内不仅上传慢,频率还不能过快,还有次数限制,Gitlab 速度也差不多。

国内的 Gitee OpenAPI 文档写的很详细,但是图片不能过大,好像是超过 1M 的大小就必须要登录。

所以相较而言比较理想的方案就是使用 Coding

使用方法

上传的图片的名称重命名为了图片的 md5 值,即默认 md5 值相同的图片是同一张图片。执行后会输出文件(图片)的 URL。

  1. 创建一个 config.json 文件,输入相关信息

    {
      "token": "获取的token",
      "team": "团队名称",
      "user": "用户名",
      "project": "项目名",
      "repo": "仓库名",
      "branch": "仓库分支"
    }
    
  2. 创建虚拟环境

    python -m venv venv
    
  3. 激活虚拟环境

    • Linux
      source venv/bin/activate
      
    • Windows
      .\ven\Scripts\activate
      
  4. 安装依赖

    pip install -r requirements.txt
    
  5. 生成在 Typora 中使用的上传命令

    python generate_cmd.py
    
  6. 通过File->Preferences 或者快捷键 Ctrl+, 切换到 Image 标签,Image Uploader 选择 Custom Command,然后上一步中的输出结果复制到 Command 位置,点击测试
    typora图片上传测试-1
    typora图片上传测试-2

    如果测试成功就可以在将图片复制到 Typora 后,选中图片,右键选择 Upload Image 即可将图片上传到仓库。
    typora图片上传测试-3
    typora图片上传测试-4

接口获取

创建项目

  1. 点击 创建项目
    创建仓库-1
  2. 选择 代码托管项目
    创建仓库-2
  3. 填写项目相关信息
    创建仓库-3

创建仓库

  1. 在新建好的项目中点击 新建代码仓库
    创建仓库-1
  2. 填写仓库相关信息(选择启用 README.md 文件初始化项目,这样才会创建 master 分支)
    创建仓库-2

上传文件

  1. 点击上传文件
    文件上传-1
  2. 选择文件进行上传,同时打开开发调试工具,在 URL 过滤中输入 api 查看 api 调用。
    文件上传-2

分析接口

  1. 观察 Network 中的 URL 调用
    分析接口-1

  2. 首先的 URL 获取了仓库的一些信息,是一个 GET 请求,
    分析接口-2
    分析接口-3
    Preview 面板中,查看获取到的数据,有一项是:

    { "commitId": "92a442e68679e4592499121985f9b09b19e781cd" }
    

    这个值在之后会作为文件上传的 POST 参数之一。

  3. 查看第二个请求 URL
    分析接口-4
    我们看到它使用了上述参数,

    { "lastCommitSha": "92a442e68679e4592499121985f9b09b19e781cd" }
    

文件上传接口调用步骤总结

  1. 获取仓库信息接口
    接口 URL:

    {
      "url": "https://<team_name>.coding.net/api/user/<user_name>/project/<project_name>/depot/<repo_name>/git/tree/<branch>/"
    }
    
  2. 上传文件接口
    接口 URL:

    {
      "url": "https://<team_name>.coding.net/api/user/<user_name>/project/<project_name>/depot/<repo_name>/git/upload/master/"
    }
    

    POST 参数:

    "message": message,  // "commit信息"
    "lastCommitSha": "", // 上一步获取的值
    "newRef": ""         // 直接为空就行
    

    个人使用的话,一般 team_nameuser_name 一样,project_namerepo_name 设置也一样就行。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值