Created
December 3, 2022 18:30
-
-
Save dead8309/808d2d70ff0c8e3f7ab5657d7f91501d to your computer and use it in GitHub Desktop.
Downloading Files from a Private Repository in nodejs. It's better to just pipe the octet stream to client BUT in case if you are hosting of platform like vercel which supports only 4mb payload limit its a good workaround for that
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require("express") | |
| const router = express.Router() | |
| const axios = require('axios'); | |
| const fs = require("fs") | |
| const token = process.env.GITHUB_TOKEN | |
| const REPO = // Repo eg: dead8309/Kizzy | |
| const getLatestRelease = async() =>{ | |
| let file = await axios.get(`https://api.github.com/repos/${REPO}/releases/latest`, { | |
| headers: { | |
| 'Authorization': 'token '+token | |
| } | |
| }) | |
| var latestReleaseInfo = await file.data | |
| return latestReleaseInfo | |
| } | |
| const getLatestFile = async(jsonData) =>{ | |
| let assets = await jsonData | |
| assets = assets.assets[0] | |
| let file = await axios.get(assets.url,{ | |
| headers: { | |
| 'Accept': 'application/octet-stream', | |
| 'Authorization': 'token '+token | |
| } | |
| }) | |
| let data = await file.request.res.responseUrl | |
| return data | |
| } | |
| router.get('/',async (req,res) => { | |
| let path = await getLatestFile(getLatestRelease()) | |
| res.status(301).redirect(path) | |
| }) | |
| router.get('/latest', async (req,res)=>{ | |
| let data = await getLatestRelease() | |
| const response = { | |
| name: data.name, | |
| body: data.body | |
| } | |
| res.send(response) | |
| }) | |
| module.exports = router | |
| /* | |
| There are two Routes | |
| GET '/' -> Downloads the file | |
| GET '/latest' -> Gets the latest release body for displaying on webpage | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment