Lots of restructuring for repo backend

This commit is contained in:
Jef Roosens 2022-01-13 21:47:14 +01:00
parent 5616e7a4e2
commit d4b7a25c06
Signed by untrusted user: Jef Roosens
GPG key ID: 955C0660072F691F
7 changed files with 176 additions and 120 deletions

View file

@ -28,62 +28,67 @@ fn (mut app App) get_root(filename string) web.Result {
mut full_path := ''
if is_pkg_name(filename) {
full_path = os.join_path_single(app.repo.pkg_dir(), filename)
full_path = os.join_path_single(app.repo.pkg_dir, filename)
} else {
full_path = os.join_path_single(app.repo.dir, filename)
full_path = os.join_path_single(app.repo.repo_dir, filename)
}
return app.file(full_path)
}
['/pkgs/:pkg'; put]
fn (mut app App) put_package(pkg string) web.Result {
if !app.is_authorized() {
return app.text('Unauthorized.')
}
// ['/pkgs/:pkg'; put]
// fn (mut app App) put_package(pkg string) web.Result {
// if !app.is_authorized() {
// return app.text('Unauthorized.')
// }
if !is_pkg_name(pkg) {
app.lwarn("Invalid package name '$pkg'.")
// if !is_pkg_name(pkg) {
// app.lwarn("Invalid package name '$pkg'.")
return app.text('Invalid filename.')
}
// return app.text('Invalid filename.')
// }
if app.repo.exists(pkg) {
app.lwarn("Duplicate package '$pkg'")
// if app.repo.exists(pkg) {
// app.lwarn("Duplicate package '$pkg'")
return app.text('File already exists.')
}
// return app.text('File already exists.')
// }
pkg_path := app.repo.pkg_path(pkg)
// pkg_path := app.repo.pkg_path(pkg)
if length := app.req.header.get(.content_length) {
app.ldebug("Uploading $length (${pretty_bytes(length.int())}) bytes to package '$pkg'.")
// if length := app.req.header.get(.content_length) {
// app.ldebug("Uploading $length (${pretty_bytes(length.int())}) bytes to package '$pkg'.")
// This is used to time how long it takes to upload a file
mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true })
// // This is used to time how long it takes to upload a file
// mut sw := time.new_stopwatch(time.StopWatchOptions{ auto_start: true })
reader_to_file(mut app.reader, length.int(), pkg_path) or {
app.lwarn("Failed to upload package '$pkg'")
// reader_to_file(mut app.reader, length.int(), pkg_path) or {
// app.lwarn("Failed to upload package '$pkg'")
return app.text('Failed to upload file.')
}
// return app.text('Failed to upload file.')
// }
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.")
}
// 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.")
// }
app.repo.add_package(pkg_path) or {
app.lwarn("Failed to add package '$pkg' to database.")
// app.repo.add_package(pkg_path) or {
// app.lwarn("Failed to add package '$pkg' to database.")
os.rm(pkg_path) or { println('Failed to remove $pkg_path') }
// os.rm(pkg_path) or { println('Failed to remove $pkg_path') }
return app.text('Failed to add package to repo.')
}
// return app.text('Failed to add package to repo.')
// }
app.linfo("Added '$pkg' to repository.")
// app.linfo("Added '$pkg' to repository.")
return app.text('Package added successfully.')
// return app.text('Package added successfully.')
// }
['/add'; put]
pub fn (mut app App) add_package() web.Result {
return app.text('')
}