Serve and list older versions as well (priority is most recent version)

This commit is contained in:
Alice Gaudon 2020-11-17 13:35:27 +01:00
parent 0a761c2ed0
commit 02db68a6f7
2 changed files with 41 additions and 12 deletions

View File

@ -3,6 +3,7 @@ import {NextFunction, Request, Response} from "express";
import * as https from "https";
import config from "config";
import {log} from "swaf/Logger";
import {NotFoundHttpError} from "swaf/HttpError";
export default class GiteaRepoLatestReleaseController extends Controller {
public routes(): void {
@ -14,7 +15,7 @@ export default class GiteaRepoLatestReleaseController extends Controller {
const {owner, name, file} = req.params;
if (!owner || !name) return next();
https.get(`${config.get('gitea_instance_url')}/api/v1/repos/${owner}/${name}/releases?limit=1`, {
https.get(`${config.get('gitea_instance_url')}/api/v1/repos/${owner}/${name}/releases`, {
headers: {
'Accept': 'application/json',
},
@ -25,18 +26,25 @@ export default class GiteaRepoLatestReleaseController extends Controller {
});
r.on('end', () => {
try {
const assets = JSON.parse(data)[0].assets;
const releases = JSON.parse(data);
if (file) {
for (const asset of assets) {
for (const release of releases) {
for (const asset of release.assets) {
if (asset.name === file) {
log.debug('Redirect to', asset.browser_download_url);
return res.redirect(302, asset.browser_download_url);
}
}
}
throw new NotFoundHttpError('Asset', req.url);
} else {
log.debug('List files');
return res.render('list-files', {assets: assets});
return res.render('list-files', {
owner: owner,
name: name,
releases: releases,
});
}
} catch (e) {
return next(e);

View File

@ -1,5 +1,26 @@
<ul>
{% for asset in assets %}
<li>#{{ asset.id }} - <a href="{{ asset.browser_download_url }}">{{ asset.name }}</a> ({{ asset.size }}, {{ asset.download_count }}, {{ asset.uuid }})</li>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ owner }}/{{ name }} - releases</title>
</head>
<body>
<main>
<h1>{{ owner }}/{{ name }} - releases</h1>
{% for release in releases %}
<section>
<h2>{{ release.name }}</h2>
<ul>
{% for asset in release.assets %}
<li>#{{ asset.id }} - <a href="{{ asset.browser_download_url }}">{{ asset.name }}</a>
({{ asset.size }}, {{ asset.download_count }}, {{ asset.uuid }})
</li>
{% endfor %}
</ul>
</ul>
</section>
{% endfor %}
</main>
</body>
</html>