Github + JSDelivr 开发白嫖图床

JSDelivr 是一个免费、开源的加速CDN公共服务,也就是反向代理了众多代码托管平台,如:github、npm, 可以通过jsDelivr的域名加速访问,所以珍惜资源,不要滥用。

1、创建个Github仓库存储静态资源
2、JSDelivr 加速加载 Github 资源
  • 链接规则

https://cdn.jsdelivr.net/gh/{Github用户名}/{Github仓库名}@{版本名}/{仓库下资源路径}

3、获取 Github Token
4、使用 Github Api 上传文件
npm install @octokit/core --save

1) Created gitup.js


const fs = require('fs');
const path = require("path");
const {Octokit} = require("@octokit/core");

class Gitup {

    octokit

    constructor(options) {

        this.octokit = new Octokit({
            baseUrl: 'https://api.github.com/repos/' + options.owner + '/' + options.repo + '/contents',
            auth: options.auth,
            userAgent: 'Octokit',
            Accept: 'application/vnd.github.v3+json',

        });

    }

    async uploadFile(file, path) {
        return await this.octokit.request('PUT /{path}', {
            path: path,
            message: 'message',
            content: base64_encode(file)
        })
    }

    async deleteRepoFile(path, sha) {
        return await this.octokit.request('DELETE /{path}', {
            path: path,
            message: 'message',
            sha: sha
        })
    }

    async uploadDir(srcDir, targetDir, fn = (e, data) => {}) {
        // 遍历文件夹
        let files = readDirFiles(srcDir)
        let tempFile
        for (let i = 0; i < files.length; i++) {
            tempFile = files[i]
            let gitFilePath = targetDir + '/' + path.basename(tempFile)
            await this.uploadFile(tempFile, gitFilePath).catch((e) => {
                fn(e, null)
            })
            fn(null, gitFilePath)
        }
    }
}

function readDirFiles(dir) {
    let files = [];
    (function dir(path) {
        let $files = fs.readdirSync(path)
        for (let i = 0; i < $files.length; i++) {
            let file = $files[i]
            let filePath = path + file
            let info = fs.statSync(filePath);
            if (info.isDirectory()) {
                dir(filePath + '/');
            } else {
                files.push(filePath)
            }
        }
    })(dir);
    return files
}

function base64_encode(file) {
    let bitmap = fs.readFileSync(file);
    return Buffer.from(bitmap).toString('base64');
}

module.exports = Gitup

2) Created main.js

const Gitup = require('./gitup')

let owner = '{Github用户名}'
let repo = '{仓库名称}'
let auth = '{Token}'

let gitup = new Gitup({
    owner: owner,
    repo: repo,
    auth: auth
})

async function uploadImage(uploadFile) {

    let path = 'images/' + new Date().getTime() + '.jpg'
    console.log(uploadFile)
    return await gitup.uploadFile(uploadFile, path).then((resp) => {
        if (resp && resp.status === 201) {
            console.log('https://cdn.jsdelivr.net/gh/' + owner + '/' + repo + '/' + path)
        }
    }).catch(function (e){
        //console.log(e)
    })
}

const fs = require('fs')
fs.readdirSync('temp').forEach(function (item, index) {
    //上传目录下的全部图片
    uploadImage('temp/' + item)
})

Demo仅供参考,自行使用 gitup.uploadFile 上传文件。