2022-01-11 14:24:29 +01:00
|
|
|
module main
|
|
|
|
|
|
|
|
import web
|
|
|
|
import os
|
|
|
|
import repo
|
|
|
|
|
|
|
|
['/pkgs/:pkg'; put]
|
|
|
|
fn (mut app App) put_package(pkg string) web.Result {
|
2022-01-11 16:08:11 +01:00
|
|
|
if app.repo.exists(pkg) {
|
2022-01-11 14:24:29 +01:00
|
|
|
app.lwarn("Tried to upload duplicate package '$pkg'")
|
|
|
|
|
|
|
|
return app.text('File already exists.')
|
|
|
|
}
|
|
|
|
|
2022-01-11 16:08:11 +01:00
|
|
|
pkg_path := app.repo.pkg_path(pkg)
|
|
|
|
|
2022-01-11 14:24:29 +01:00
|
|
|
if length := app.req.header.get(.content_length) {
|
|
|
|
app.ldebug("Uploading $length bytes to package '$pkg'")
|
2022-01-11 16:08:11 +01:00
|
|
|
println(pkg_path)
|
|
|
|
reader_to_file(mut app.reader, length.int(), pkg_path) or {
|
2022-01-11 14:24:29 +01:00
|
|
|
app.lwarn("Failed to upload package '$pkg'")
|
|
|
|
|
|
|
|
return app.text('Failed to upload file.')
|
|
|
|
}
|
|
|
|
} 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 16:08:11 +01:00
|
|
|
app.repo.add_package(pkg_path) or {
|
|
|
|
app.linfo("Failed to add package '$pkg' to database.")
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-01-11 16:08:11 +01:00
|
|
|
os.rm(pkg_path) or { println('Failed to remove $pkg_path') }
|
2022-01-11 14:40:25 +01:00
|
|
|
|
2022-01-11 16:08:11 +01:00
|
|
|
return app.text('Failed to add package to repo.')
|
2022-01-11 14:24:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-11 16:08:11 +01:00
|
|
|
app.linfo("Added '$pkg' to repository.")
|
2022-01-11 14:24:29 +01:00
|
|
|
app.linfo("Uploaded package '$pkg'.")
|
|
|
|
|
|
|
|
return app.text('Package added successfully.')
|
|
|
|
}
|