Browse Source

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

develop
Alice Gaudon 7 months ago
parent
commit
02db68a6f7
2 changed files with 41 additions and 12 deletions
  1. +15
    -7
      src/controllers/GiteaRepoLatestReleaseController.ts
  2. +26
    -5
      views/list-files.njk

+ 15
- 7
src/controllers/GiteaRepoLatestReleaseController.ts 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) {
if (asset.name === file) {
log.debug('Redirect to', asset.browser_download_url);
return res.redirect(302, asset.browser_download_url);
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);


+ 26
- 5
views/list-files.njk 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>
{% endfor %}
</ul>
<!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>
</section>
{% endfor %}
</main>
</body>
</html>

Loading…
Cancel
Save