45 lines
1.4 KiB
JavaScript
45 lines
1.4 KiB
JavaScript
'use strict'
|
|
|
|
const prettyBytes = require('pretty-bytes')
|
|
const LegacyService = require('../legacy-service')
|
|
const {
|
|
makeBadgeData: getBadgeData,
|
|
makeLogo: getLogo,
|
|
} = require('../../lib/badge-data')
|
|
const {
|
|
checkErrorResponse: githubCheckErrorResponse,
|
|
} = require('./github-helpers')
|
|
|
|
module.exports = class GithubRepoSize extends LegacyService {
|
|
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
|
|
camp.route(
|
|
/^\/github\/repo-size\/([^/]+)\/([^/]+)\.(svg|png|gif|jpg|json)$/,
|
|
cache((data, match, sendBadge, request) => {
|
|
const user = match[1]
|
|
const repo = match[2]
|
|
const format = match[3]
|
|
const apiUrl = `/repos/${user}/${repo}`
|
|
const badgeData = getBadgeData('repo size', data)
|
|
if (badgeData.template === 'social') {
|
|
badgeData.logo = getLogo('github', data)
|
|
}
|
|
githubApiProvider.request(request, apiUrl, {}, (err, res, buffer) => {
|
|
if (githubCheckErrorResponse(badgeData, err, res)) {
|
|
sendBadge(format, badgeData)
|
|
return
|
|
}
|
|
try {
|
|
const parsedData = JSON.parse(buffer)
|
|
badgeData.text[1] = prettyBytes(parseInt(parsedData.size) * 1024)
|
|
badgeData.colorscheme = 'blue'
|
|
sendBadge(format, badgeData)
|
|
} catch (e) {
|
|
badgeData.text[1] = 'invalid'
|
|
sendBadge(format, badgeData)
|
|
}
|
|
})
|
|
})
|
|
)
|
|
}
|
|
}
|