]> OzVa Git service - ozva-cloud/commitdiff
fix: remove unzip file even failed to unzip
authorsigoden <sigoden@gmail.com>
Thu, 2 Jun 2022 11:43:43 +0000 (19:43 +0800)
committersigoden <sigoden@gmail.com>
Thu, 2 Jun 2022 11:43:43 +0000 (19:43 +0800)
src/server.rs

index 2d7779aa80108362b92a762aa437a78fbd7d0267..8cf2da49b9b44fd80983f21489cd92eaafb416f2 100644 (file)
@@ -260,27 +260,9 @@ impl InnerService {
 
         let query = req.uri().query().unwrap_or_default();
         if query == "unzip" {
-            let root = path.parent().unwrap();
-            let mut zip = ZipFileReader::new(File::open(&path).await?).await?;
-            for i in 0..zip.entries().len() {
-                let entry = &zip.entries()[i];
-                let entry_name = entry.name();
-                let entry_path = root.join(entry_name);
-                if entry_name.ends_with('/') {
-                    fs::create_dir_all(entry_path).await?;
-                } else {
-                    if !self.args.allow_delete && fs::metadata(&entry_path).await.is_ok() {
-                        continue;
-                    }
-                    if let Some(parent) = entry_path.parent() {
-                        if fs::symlink_metadata(parent).await.is_err() {
-                            fs::create_dir_all(&parent).await?;
-                        }
-                    }
-                    let mut outfile = fs::File::create(&entry_path).await?;
-                    let mut reader = zip.entry_reader(i).await?;
-                    io::copy(&mut reader, &mut outfile).await?;
-                }
+            if let Err(e) = self.unzip_file(path).await {
+                eprintln!("Failed to unzip {}, {}", path.display(), e);
+                status!(res, StatusCode::BAD_REQUEST);
             }
             fs::remove_file(&path).await?;
         }
@@ -539,6 +521,32 @@ impl InnerService {
             .unwrap_or_default()
     }
 
+    async fn unzip_file(&self, path: &Path) -> BoxResult<()> {
+        let root = path.parent().unwrap();
+        let mut zip = ZipFileReader::new(File::open(&path).await?).await?;
+        for i in 0..zip.entries().len() {
+            let entry = &zip.entries()[i];
+            let entry_name = entry.name();
+            let entry_path = root.join(entry_name);
+            if entry_name.ends_with('/') {
+                fs::create_dir_all(entry_path).await?;
+            } else {
+                if !self.args.allow_delete && fs::metadata(&entry_path).await.is_ok() {
+                    continue;
+                }
+                if let Some(parent) = entry_path.parent() {
+                    if fs::symlink_metadata(parent).await.is_err() {
+                        fs::create_dir_all(&parent).await?;
+                    }
+                }
+                let mut outfile = fs::File::create(&entry_path).await?;
+                let mut reader = zip.entry_reader(i).await?;
+                io::copy(&mut reader, &mut outfile).await?;
+            }
+        }
+        Ok(())
+    }
+
     fn extract_path(&self, path: &str) -> Option<PathBuf> {
         let decoded_path = percent_decode(path[1..].as_bytes()).decode_utf8().ok()?;
         let slashes_switched = if cfg!(windows) {