public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 4/6] server/rest: add helpers for compression
Date: Wed, 31 Mar 2021 11:44:16 +0200	[thread overview]
Message-ID: <20210331094418.16609-5-d.csapak@proxmox.com> (raw)
In-Reply-To: <20210331094418.16609-1-d.csapak@proxmox.com>

these helpers will be used in compressing api calls and static files

Signed-off-by: Dominik Csapak <d.csapak@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





  parent reply	other threads:[~2021-03-31  9:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-31  9:44 [pbs-devel] [PATCH proxmox-backup 0/6] add compression to api/static files Dominik Csapak
2021-03-31  9:44 ` [pbs-devel] [PATCH proxmox-backup 1/6] tools: add compression module Dominik Csapak
2021-03-31  9:44 ` [pbs-devel] [PATCH proxmox-backup 2/6] tools/compression: add DeflateEncoder and helpers Dominik Csapak
2021-03-31  9:44 ` [pbs-devel] [PATCH proxmox-backup 3/6] add a CACHE_DIR to the created directories on daemon startup Dominik Csapak
2021-03-31  9:44 ` Dominik Csapak [this message]
2021-03-31  9:44 ` [pbs-devel] [PATCH proxmox-backup 5/6] server/rest: compress api calls Dominik Csapak
2021-03-31  9:44 ` [pbs-devel] [PATCH proxmox-backup 6/6] server/rest: compress static files Dominik Csapak
2021-03-31 10:52 ` [pbs-devel] [PATCH proxmox-backup 0/6] add compression to api/static files Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210331094418.16609-5-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal