2022-01-11 14:24:29 +01:00
|
|
|
module main
|
|
|
|
|
|
|
|
import web
|
|
|
|
import os
|
|
|
|
import repo
|
2022-01-11 18:19:41 +01:00
|
|
|
import time
|
2022-01-19 21:24:46 +01:00
|
|
|
import rand
|
2022-01-11 18:19:41 +01:00
|
|
|
|
|
|
|
const prefixes = ['B', 'KB', 'MB', 'GB']
|
|
|
|
|
2022-01-14 22:46:04 +01:00
|
|
|
// pretty_bytes converts a byte count to human-readable version
|
2022-01-11 18:19:41 +01:00
|
|
|
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
|
|
|
|
2022-01-11 21:01:09 +01:00
|
|
|
fn is_pkg_name(s string) bool {
|
|
|
|
return s.contains('.pkg')
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:46:04 +01:00
|
|
|
// get_root handles a GET request for a file on the root
|
2022-01-11 21:01:09 +01:00
|
|
|
['/: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)
|
2022-01-11 21:01:09 +01:00
|
|
|
} else {
|
2022-01-13 21:47:14 +01:00
|
|
|
full_path = os.join_path_single(app.repo.repo_dir, filename)
|
2022-01-11 21:01:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return app.file(full_path)
|
|
|
|
}
|
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
['/publish'; post]
|
|
|
|
fn (mut app App) put_package() web.Result {
|
|
|
|
if !app.is_authorized() {
|
|
|
|
return app.text('Unauthorized.')
|
|
|
|
}
|
2022-01-11 21:01:09 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
mut pkg_path := ''
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
if length := app.req.header.get(.content_length) {
|
|
|
|
// Generate a random filename for the temp file
|
|
|
|
pkg_path = os.join_path_single(app.dl_dir, rand.uuid_v4())
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
for os.exists(pkg_path) {
|
|
|
|
pkg_path = os.join_path_single(app.dl_dir, rand.uuid_v4())
|
|
|
|
}
|
2022-01-11 16:08:11 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
app.ldebug("Uploading $length (${pretty_bytes(length.int())}) bytes to '$pkg_path'.")
|
2022-01-11 18:19:41 +01:00
|
|
|
|
2022-01-19 21:24:46 +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-11 18:19:41 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
reader_to_file(mut app.reader, length.int(), pkg_path) or {
|
|
|
|
app.lwarn("Failed to upload '$pkg_path'")
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
return app.text('Failed to upload file.')
|
|
|
|
}
|
2022-01-11 18:19:41 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
sw.stop()
|
|
|
|
app.ldebug("Upload of '$pkg_path' completed in ${sw.elapsed().seconds():.3}s.")
|
|
|
|
} else {
|
|
|
|
app.lwarn('Tried to upload package without specifying a Content-Length.')
|
|
|
|
return app.text("Content-Type header isn't set.")
|
|
|
|
}
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
added := app.repo.add_from_path(pkg_path) or {
|
|
|
|
app.lerror('Error while adding package.')
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
return app.text('Failed to add package.')
|
|
|
|
}
|
|
|
|
if !added {
|
|
|
|
app.lwarn('Duplicate package.')
|
2022-01-11 14:40:25 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
return app.text('File already exists.')
|
|
|
|
}
|
2022-01-13 21:47:14 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
app.linfo("Added '$pkg_path' to repository.")
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
return app.text('Package added successfully.')
|
|
|
|
}
|
2022-01-11 14:24:29 +01:00
|
|
|
|
2022-01-14 22:46:04 +01:00
|
|
|
// add_package PUT a new package to the server
|
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
|
|
|
}
|