forked from matteo/serves3
1
0
Fork 0

Minor refactorings

This commit is contained in:
Matteo Settenvini 2023-07-02 12:11:13 +02:00
parent 9fa09de8df
commit 74274dc076
3 changed files with 48 additions and 22 deletions

12
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: Public domain.
// SPDX-License-Identifier: CC0-1.0
{
"recommendations": [
"bungcip.better-toml",
"ritwickdey.LiveServer",
"rust-lang.rust-analyzer",
"serayuzgur.crates",
"vadimcn.vscode-lldb",
"zaaack.markdown-editor",
]
}

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
// SPDX-FileCopyrightText: Public domain.
// SPDX-License-Identifier: CC0-1.0
{
"liveServer.settings.port": 8000,
}

View File

@ -82,36 +82,45 @@ async fn index(path: PathBuf) -> Result<FileView, Error> {
The way things work in S3, the following holds for us: The way things work in S3, the following holds for us:
- we need to use a slash as separator - we need to use a slash as separator
- folders need to be queried ending with a slash - folders need to be queried ending with a slash
- getting the bucket address will return an XML file - getting the bucket address (empty prefix) will
with all properties; we don't want that. return an XML file with all properties; we don't
want that.
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.
*/ */
// FIXME: this can be big, we should use streaming, if let Ok(result) = s3_serve_file(&path).await {
// not loading in memory! Ok(result)
if !path.as_os_str().is_empty() { } else {
let data = BUCKET let objects = s3_fileview(&path).await?;
.get_object(format!("{}", path.display())) let rendered = Template::render(
.await "index",
.map_err(|_| Error::NotFound("Object not found".into())); context! {
path: format!("{}/", path.display()),
objects
},
);
Ok(FileView::Folder(rendered))
}
}
if let Ok(contents) = data { async fn s3_serve_file(path: &PathBuf) -> Result<FileView, Error> {
let bytes = contents.bytes().to_vec(); let is_root_prefix = path.as_os_str().is_empty();
return Ok(FileView::File(bytes)); if is_root_prefix {
} return Err(Error::NotFound("Root prefix is not a file".into()));
} }
let objects = s3_fileview(&path).await?; // FIXME: this can be big, we should use streaming,
let rendered = Template::render( // not loading in memory!
"index", BUCKET
context! { .get_object(format!("{}", path.display()))
path: format!("{}/", path.display()), .await
objects .map(|contents| {
}, let bytes = contents.bytes().to_vec();
); FileView::File(bytes)
Ok(FileView::Folder(rendered)) })
.map_err(|_| Error::NotFound("Object not found".into()))
} }
async fn s3_fileview(path: &PathBuf) -> Result<Vec<String>, Error> { async fn s3_fileview(path: &PathBuf) -> Result<Vec<String>, Error> {