| 1 | module main |
| 2 | |
| 3 | import veb |
| 4 | import os |
| 5 | |
| 6 | @['/:username/:repo_name/tag/:tag/:format'] |
| 7 | pub fn (mut app App) handle_download_tag_archive(username string, repo_name string, tag string, format string) veb.Result { |
| 8 | // access checking will be implemented in another module |
| 9 | user := app.get_user_by_username(username) or { return ctx.not_found() } |
| 10 | repo := app.find_repo_by_name_and_user_id(repo_name, user.id) or { return ctx.not_found() } |
| 11 | |
| 12 | if !app.can_read_repo(ctx, repo) { |
| 13 | return ctx.not_found() |
| 14 | } |
| 15 | |
| 16 | archive_abs_path := os.abs_path(app.config.archive_path) |
| 17 | snapshot_format := if format == 'zip' { 'zip' } else { 'tar.gz' } |
| 18 | snapshot_name := '${username}_${repo_name}_${tag}.${snapshot_format}' |
| 19 | archive_path := '${archive_abs_path}/${snapshot_name}' |
| 20 | |
| 21 | if format == 'zip' { |
| 22 | repo.archive_tag(tag, archive_path, .zip) |
| 23 | } else { |
| 24 | repo.archive_tag(tag, archive_path, .tar) |
| 25 | } |
| 26 | |
| 27 | archive_content := os.read_file(archive_path) or { return ctx.not_found() } |
| 28 | |
| 29 | return app.send_file(mut ctx, snapshot_name, archive_content) |
| 30 | } |
| 31 | |