[pbs-devel] [PATCH proxmox-backup 4/6] server/rest: add helpers for compression
Dominik Csapak
d.csapak at proxmox.com
Wed Mar 31 11:44:16 CEST 2021
these helpers will be used in compressing api calls and static files
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
src/server/rest.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 98 insertions(+)
diff --git a/src/server/rest.rs b/src/server/rest.rs
index 150125ec..bf9aab81 100644
--- a/src/server/rest.rs
+++ b/src/server/rest.rs
@@ -39,6 +39,7 @@ use crate::api2::types::{Authid, Userid};
use crate::auth_helpers::*;
use crate::config::cached_user_info::CachedUserInfo;
use crate::tools;
+use crate::tools::compression::{self, CompressionMethod};
use crate::tools::ticket::Ticket;
use crate::tools::FileLogger;
@@ -525,6 +526,81 @@ fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
("application/octet-stream", false)
}
+// returns the compressed file path, and the optionial Path that needs to be written
+async fn get_compressed_file_path(
+ source: PathBuf,
+ compression: &Option<CompressionMethod>,
+) -> Result<(PathBuf, Option<PathBuf>), Error> {
+ let source_ext = source
+ .extension()
+ .and_then(|osstr| osstr.to_str())
+ .unwrap_or("");
+
+ let target_ext = if let Some(method) = compression {
+ format!("{}.{}", source_ext, method.extension())
+ } else {
+ return Ok((source, None));
+ };
+
+ let target_filename = source
+ .file_name()
+ .and_then(|osstr| osstr.to_str())
+ .ok_or_else(|| format_err!("invalid filename"))?;
+
+ let mut target: PathBuf = PathBuf::from(format!(
+ "{}/{}",
+ crate::buildcfg::PROXMOX_BACKUP_CACHE_DIR,
+ target_filename
+ ));
+ target.set_extension(target_ext);
+
+ let source_metadata = tokio::fs::metadata(&source).await?;
+ let target_metadata = if let Ok(metadata) = tokio::fs::metadata(&target).await {
+ metadata
+ } else {
+ return Ok((source, Some(target)));
+ };
+
+ if source_metadata.modified()? > target_metadata.modified()? {
+ Ok((source, Some(target)))
+ } else {
+ Ok((target, None))
+ }
+}
+
+// returns a handle to either the compressed file, or to the original one,
+// if comrpessing fails (e.g. someone else is compressing it right now)
+// in that case, the compression method is changed to None
+async fn get_possibly_compressed_file(
+ source: PathBuf,
+ compression: &mut Option<CompressionMethod>,
+) -> Result<File, Error> {
+ let (filename, to_write) = get_compressed_file_path(source, compression)
+ .await
+ .map_err(|err| http_err!(BAD_REQUEST, "failed to find compress target: {}", err))?;
+
+ // if compressing fails, return the regular file instead
+ match (&to_write, &compression) {
+ (Some(target), Some(method)) => {
+ match compression::compress_file(&filename, &target, &method).await {
+ Ok(file) => Ok(file),
+ Err(_) => {
+ // we are returning the regular file, reset compression
+ *compression = None;
+ File::open(filename)
+ .await
+ .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))
+ }
+ }
+ }
+ (_, _) => {
+ File::open(filename)
+ .await
+ .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))
+ }
+ }
+}
+
async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
let (content_type, _nocomp) = extension_to_content_type(&filename);
@@ -598,6 +674,28 @@ enum AuthData {
ApiToken(String),
}
+fn extract_compression_method(headers: &http::HeaderMap) -> Option<CompressionMethod> {
+ let mut compression = None;
+ if let Some(raw_encoding) = headers.get(header::ACCEPT_ENCODING) {
+ if let Ok(encoding) = raw_encoding.to_str() {
+ for encoding in encoding.split(&[',', ' '][..]) {
+ if let Ok(method) = encoding.parse() {
+ if method != CompressionMethod::Deflate {
+ // fixme: implement other compressors
+ continue;
+ }
+ let method = Some(method);
+ if method > compression {
+ compression = method;
+ }
+ }
+ }
+ }
+ }
+
+ return compression;
+}
+
fn extract_auth_data(headers: &http::HeaderMap) -> Option<AuthData> {
if let Some(raw_cookie) = headers.get(header::COOKIE) {
if let Ok(cookie) = raw_cookie.to_str() {
--
2.20.1
More information about the pbs-devel
mailing list