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,27 +82,17 @@ 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
.get_object(format!("{}", path.display()))
.await
.map_err(|_| Error::NotFound("Object not found".into()));
if let Ok(contents) = data {
let bytes = contents.bytes().to_vec();
return Ok(FileView::File(bytes));
}
}
let objects = s3_fileview(&path).await?; let objects = s3_fileview(&path).await?;
let rendered = Template::render( let rendered = Template::render(
"index", "index",
@ -113,6 +103,25 @@ async fn index(path: PathBuf) -> Result<FileView, Error> {
); );
Ok(FileView::Folder(rendered)) Ok(FileView::Folder(rendered))
} }
}
async fn s3_serve_file(path: &PathBuf) -> Result<FileView, Error> {
let is_root_prefix = path.as_os_str().is_empty();
if is_root_prefix {
return Err(Error::NotFound("Root prefix is not a file".into()));
}
// FIXME: this can be big, we should use streaming,
// not loading in memory!
BUCKET
.get_object(format!("{}", path.display()))
.await
.map(|contents| {
let bytes = contents.bytes().to_vec();
FileView::File(bytes)
})
.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> {
/* /*