Fix listing of S3 prefixes not terminated by a slash #3

Merged
matteo merged 5 commits from eay/serves3:hackathon_slash_fix_erik_and_eren into main 2024-06-12 12:05:38 +02:00
2 changed files with 9 additions and 8 deletions
Showing only changes of commit ed3a1fbfe9 - Show all commits

View File

@ -78,7 +78,6 @@ enum FileView {
#[derive(Serialize)] #[derive(Serialize)]
struct FileViewItem { struct FileViewItem {
parent: String,
path: String, path: String,
size: String, size: String,
size_bytes: u64, size_bytes: u64,
@ -107,7 +106,7 @@ async fn index(path: PathBuf) -> Result<FileView, Error> {
We try first to retrieve list an object as a file. If we fail, We try first to retrieve list an object as a file. If we fail,
we fallback to retrieving the equivalent folder. we fallback to retrieving the equivalent folder.
*/ */
if let Ok(result) = s3_serve_file(&path).await { if let Ok(result) = s3_serve_file(&path).await {
Ok(result) Ok(result)
} else { } else {
@ -176,7 +175,6 @@ async fn s3_fileview(path: &PathBuf) -> Result<Vec<FileViewItem>, Error> {
let folders = list.common_prefixes.iter().flatten().map(|dir| { let folders = list.common_prefixes.iter().flatten().map(|dir| {
let path = dir.prefix.strip_prefix(&prefix); let path = dir.prefix.strip_prefix(&prefix);
path.map(|path| FileViewItem { path.map(|path| FileViewItem {
parent:s3_folder_path.clone(),
path: path.to_owned(), path: path.to_owned(),
size_bytes: 0, size_bytes: 0,
size: "[DIR]".to_owned(), size: "[DIR]".to_owned(),
@ -187,7 +185,6 @@ async fn s3_fileview(path: &PathBuf) -> Result<Vec<FileViewItem>, Error> {
let files = list.contents.iter().map(|obj| { let files = list.contents.iter().map(|obj| {
let path = obj.key.strip_prefix(&prefix); let path = obj.key.strip_prefix(&prefix);
path.map(|path| FileViewItem { path.map(|path| FileViewItem {
parent:s3_folder_path.clone(),
path: path.to_owned(), path: path.to_owned(),
size_bytes: obj.size, size_bytes: obj.size,
size: size_bytes_to_human(obj.size), size: size_bytes_to_human(obj.size),
@ -246,9 +243,8 @@ fn rocket() -> _ {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use rstest::rstest;
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*; use super::*;
use rstest::rstest;
#[rstest] #[rstest]
#[case(1024, "1.024 kB")] #[case(1024, "1.024 kB")]
@ -260,7 +256,7 @@ mod tests {
#[case(u64::MIN, format!("{:.3} B",u64::MIN as f64))] #[case(u64::MIN, format!("{:.3} B",u64::MIN as f64))]
fn test_size_bytes_to_human(#[case] bytes: u64, #[case] expected: String) { fn test_size_bytes_to_human(#[case] bytes: u64, #[case] expected: String) {
println!("{}",size_bytes_to_human(bytes)); println!("{}", size_bytes_to_human(bytes));
assert_eq!(size_bytes_to_human(bytes), expected); assert_eq!(size_bytes_to_human(bytes), expected);
} }
} }

View File

@ -5,6 +5,11 @@
--> -->
<html> <html>
<head> <head>
<script type="text/javascript">
if (window.location.pathname.endsWith('/') === false) {
window.location.href = window.location + "/";
}
</script>
<style> <style>
body { body {
font-family: monospace; font-family: monospace;
@ -44,7 +49,7 @@
{% for object in objects %} {% for object in objects %}
<tr> <tr>
<td><a href="/{{ object.parent | urlencode }}{{ object.path | urlencode }}" data-size-bytes="{{ object.size_bytes }}">{{ object.path }}</a></td> <td><a href="{{ object.path | urlencode }}" data-size-bytes="{{ object.size_bytes }}">{{ object.path }}</a></td>
<td>{{ object.size }}</td> <td>{{ object.size }}</td>
<td>{{ object.last_modification}}</td> <td>{{ object.last_modification}}</td>
</tr> </tr>