2022-04-02 21:00:38 +02:00
|
|
|
use std::{collections::HashSet, io::ErrorKind, path::Path};
|
|
|
|
|
2022-04-02 20:56:51 +02:00
|
|
|
use axum::{
|
2022-04-05 09:58:02 +02:00
|
|
|
extract::{BodyStream, Extension, Query},
|
2022-04-02 20:56:51 +02:00
|
|
|
http::StatusCode,
|
|
|
|
response::IntoResponse,
|
|
|
|
};
|
|
|
|
use flate2::read::GzDecoder;
|
|
|
|
use futures_util::TryStreamExt;
|
2022-04-05 09:58:02 +02:00
|
|
|
use serde::Deserialize;
|
2022-04-02 20:56:51 +02:00
|
|
|
use tar::Archive;
|
|
|
|
use tokio_util::io::StreamReader;
|
|
|
|
|
2022-04-05 10:45:42 +02:00
|
|
|
use crate::{DEFAULT_STATIC_SITE, STATIC_DIR_NAME};
|
2022-04-05 09:58:02 +02:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2022-04-28 10:56:30 +02:00
|
|
|
pub struct StaticDirParams {
|
2022-04-05 09:58:02 +02:00
|
|
|
dir: Option<String>,
|
|
|
|
}
|
2022-04-02 21:00:38 +02:00
|
|
|
|
2022-04-02 20:56:51 +02:00
|
|
|
pub async fn post_deploy(
|
|
|
|
Extension(data_dir): Extension<String>,
|
2022-04-05 09:58:02 +02:00
|
|
|
Query(params): Query<StaticDirParams>,
|
2022-04-02 20:56:51 +02:00
|
|
|
res: BodyStream,
|
2022-04-28 10:56:30 +02:00
|
|
|
) -> impl IntoResponse {
|
2022-04-02 20:56:51 +02:00
|
|
|
// This converts a stream into something that implements AsyncRead, which we can then use to
|
|
|
|
// asynchronously write the file to disk
|
|
|
|
let mut read =
|
|
|
|
StreamReader::new(res.map_err(|axum_err| std::io::Error::new(ErrorKind::Other, axum_err)));
|
|
|
|
let uuid = uuid::Uuid::new_v4();
|
|
|
|
let file_path = Path::new(&data_dir).join(uuid.as_hyphenated().to_string());
|
|
|
|
let mut file = tokio::fs::File::create(&file_path).await.unwrap();
|
|
|
|
tokio::io::copy(&mut read, &mut file).await;
|
|
|
|
|
2022-04-05 10:45:42 +02:00
|
|
|
// If no dir is provided, we use the default one. Otherwise, use the provided one.
|
|
|
|
let static_path = Path::new(&data_dir)
|
|
|
|
.join(STATIC_DIR_NAME)
|
|
|
|
.join(params.dir.unwrap_or(DEFAULT_STATIC_SITE.to_string()));
|
2022-04-05 09:58:02 +02:00
|
|
|
|
|
|
|
// Make sure the static directory exists
|
|
|
|
tokio::fs::create_dir_all(&static_path).await;
|
|
|
|
|
|
|
|
let fp_clone = file_path.clone();
|
2022-04-02 20:56:51 +02:00
|
|
|
// Extract the contents of the tarball synchronously
|
2022-04-05 09:58:02 +02:00
|
|
|
let res =
|
|
|
|
match tokio::task::spawn_blocking(move || process_archive(&fp_clone, &static_path)).await {
|
|
|
|
Ok(_) => StatusCode::OK,
|
|
|
|
Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
2022-04-02 20:56:51 +02:00
|
|
|
};
|
|
|
|
|
2022-04-05 09:58:02 +02:00
|
|
|
// Remove archive file after use
|
|
|
|
tokio::fs::remove_file(&file_path).await;
|
2022-04-02 20:56:51 +02:00
|
|
|
|
2022-04-05 09:58:02 +02:00
|
|
|
res
|
|
|
|
}
|
2022-04-02 20:56:51 +02:00
|
|
|
|
2022-04-28 10:56:30 +02:00
|
|
|
fn process_archive(archive_path: &Path, static_dir: &Path) -> Result<(), ()> {
|
2022-04-05 09:58:02 +02:00
|
|
|
let file = std::fs::File::open(archive_path).map_err(|_| ())?;
|
|
|
|
let tar = GzDecoder::new(file);
|
|
|
|
let mut archive = Archive::new(tar);
|
|
|
|
|
|
|
|
let mut paths = HashSet::new();
|
|
|
|
|
|
|
|
let entries = archive.entries().map_err(|_| ())?;
|
|
|
|
// Extract each entry into the output directory
|
|
|
|
for entry_res in entries {
|
|
|
|
let mut entry = entry_res.map_err(|_| ())?;
|
|
|
|
entry.unpack_in(static_dir).map_err(|_| ())?;
|
|
|
|
|
|
|
|
if let Ok(path) = entry.path() {
|
|
|
|
paths.insert(path.into_owned());
|
2022-04-02 20:56:51 +02:00
|
|
|
}
|
2022-04-05 09:58:02 +02:00
|
|
|
}
|
2022-04-02 20:56:51 +02:00
|
|
|
|
2022-04-05 09:58:02 +02:00
|
|
|
// Remove any old files that weren't present in new archive
|
|
|
|
let mut items = vec![];
|
2022-04-02 20:56:51 +02:00
|
|
|
|
2022-04-05 09:58:02 +02:00
|
|
|
// Start by populating the vec with the initial files
|
|
|
|
let iter = static_dir.read_dir().map_err(|_| ())?;
|
|
|
|
iter.filter_map(|r| r.ok())
|
|
|
|
.for_each(|e| items.push(e.path()));
|
|
|
|
|
|
|
|
// As long as there are still items in the vec, we keep going
|
|
|
|
while items.len() > 0 {
|
|
|
|
let item = items.pop().unwrap();
|
|
|
|
tracing::debug!("{:?}", item);
|
|
|
|
|
|
|
|
if !paths.contains(item.strip_prefix(&static_dir).unwrap()) {
|
|
|
|
if item.is_dir() {
|
|
|
|
std::fs::remove_dir_all(item);
|
|
|
|
} else {
|
|
|
|
std::fs::remove_file(item);
|
2022-04-02 20:56:51 +02:00
|
|
|
}
|
2022-04-05 09:58:02 +02:00
|
|
|
} else if let Ok(iter) = item.read_dir() {
|
|
|
|
iter.filter_map(|r| r.ok())
|
|
|
|
.for_each(|e| items.push(e.path()));
|
2022-04-02 20:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-05 09:58:02 +02:00
|
|
|
|
|
|
|
Ok(())
|
2022-04-02 20:56:51 +02:00
|
|
|
}
|