vieter/src/routes.v

95 lines
2.1 KiB
Coq
Raw Normal View History

2022-01-11 14:24:29 +01:00
module main
import web
import os
import repo
import time
const prefixes = ['B', 'KB', 'MB', 'GB']
fn pretty_bytes(bytes int) string {
mut i := 0
mut n := f32(bytes)
for n >= 1024 {
i++
n /= 1024
}
return '${n:.2}${prefixes[i]}'
}
2022-01-11 14:24:29 +01:00
fn is_pkg_name(s string) bool {
return s.contains('.pkg')
}
['/:filename'; get]
fn (mut app App) get_root(filename string) web.Result {
mut full_path := ''
if is_pkg_name(filename) {
2022-01-13 21:47:14 +01:00
full_path = os.join_path_single(app.repo.pkg_dir, filename)
} else {
2022-01-13 21:47:14 +01:00
full_path = os.join_path_single(app.repo.repo_dir, filename)
}
return app.file(full_path)
}
2022-01-13 21:47:14 +01:00
// ['/pkgs/:pkg'; put]
// fn (mut app App) put_package(pkg string) web.Result {
// if !app.is_authorized() {
// return app.text('Unauthorized.')
// }
2022-01-11 21:18:33 +01:00
2022-01-13 21:47:14 +01:00
// if !is_pkg_name(pkg) {
// app.lwarn("Invalid package name '$pkg'.")
2022-01-13 21:47:14 +01:00
// return app.text('Invalid filename.')
// }
2022-01-13 21:47:14 +01:00
// if app.repo.exists(pkg) {
// app.lwarn("Duplicate package '$pkg'")
2022-01-11 14:24:29 +01:00
2022-01-13 21:47:14 +01:00
// return app.text('File already exists.')
// }
2022-01-11 14:24:29 +01:00
2022-01-13 21:47:14 +01:00
// pkg_path := app.repo.pkg_path(pkg)
2022-01-13 21:47:14 +01:00
// if length := app.req.header.get(.content_length) {
// app.ldebug("Uploading $length (${pretty_bytes(length.int())}) bytes to package '$pkg'.")
2022-01-13 21:47:14 +01:00
// // This is used to time how long it takes to upload a file
// mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true })
2022-01-13 21:47:14 +01:00
// reader_to_file(mut app.reader, length.int(), pkg_path) or {
// app.lwarn("Failed to upload package '$pkg'")
2022-01-11 14:24:29 +01:00
2022-01-13 21:47:14 +01:00
// return app.text('Failed to upload file.')
// }
2022-01-13 21:47:14 +01:00
// sw.stop()
// app.ldebug("Upload of package '$pkg' completed in ${sw.elapsed().seconds():.3}s.")
// } else {
// app.lwarn("Tried to upload package '$pkg' without specifying a Content-Length.")
// return app.text("Content-Type header isn't set.")
// }
2022-01-11 14:24:29 +01:00
2022-01-13 21:47:14 +01:00
// app.repo.add_package(pkg_path) or {
// app.lwarn("Failed to add package '$pkg' to database.")
2022-01-11 14:24:29 +01:00
2022-01-13 21:47:14 +01:00
// os.rm(pkg_path) or { println('Failed to remove $pkg_path') }
2022-01-11 14:40:25 +01:00
2022-01-13 21:47:14 +01:00
// return app.text('Failed to add package to repo.')
// }
// app.linfo("Added '$pkg' to repository.")
2022-01-11 14:24:29 +01:00
2022-01-13 21:47:14 +01:00
// return app.text('Package added successfully.')
// }
2022-01-11 14:24:29 +01:00
2022-01-13 21:47:14 +01:00
['/add'; put]
pub fn (mut app App) add_package() web.Result {
return app.text('')
2022-01-11 14:24:29 +01:00
}