|
1 |
| -use anyhow::{anyhow, Context, Result}; |
2 |
| -use base64::prelude::BASE64_STANDARD; |
3 |
| -use base64::Engine; |
4 |
| -use colored::Colorize; |
5 |
| -use flate2::read::GzDecoder; |
6 |
| -use futures_util::StreamExt; |
7 |
| -use indicatif::{ProgressBar, ProgressStyle}; |
8 |
| -use reqwest::Client; |
9 |
| -use std::fs::OpenOptions; |
10 |
| -use std::io::{Read, Seek, SeekFrom}; |
11 | 1 | use std::{
|
12 | 2 | cmp::min,
|
13 | 3 | fs::{self, File},
|
14 |
| - io::{self, Write}, |
| 4 | + io::{self, BufWriter, Read, Seek, SeekFrom, Write}, |
15 | 5 | path::Path,
|
16 | 6 | };
|
| 7 | + |
| 8 | +use anyhow::{Context, Result}; |
| 9 | +use base64::{prelude::BASE64_STANDARD, Engine}; |
| 10 | +use colored::Colorize; |
| 11 | +use flate2::read::GzDecoder; |
| 12 | +use futures_util::StreamExt; |
| 13 | +use indicatif::{ProgressBar, ProgressStyle}; |
| 14 | +use reqwest::Client; |
17 | 15 | use truncatable::Truncatable;
|
18 | 16 |
|
19 | 17 | /// Creates the parent directory for a given path if it does not exist.
|
@@ -129,27 +127,39 @@ pub fn extract_gzip(gzip_path: &str, filename: &str, prefix: &str) -> Result<()>
|
129 | 127 | );
|
130 | 128 | Ok(())
|
131 | 129 | }
|
132 |
| -//try to decode a base64 file in place, the file must exist,if the file is not base64 encoded ,it is ok |
133 |
| -pub fn decode_base64(filename: &str) -> Result<()> { |
134 |
| - // copy file to buffer |
135 |
| - let mut file = OpenOptions::new().read(true).write(true).open(filename)?; |
136 |
| - let mut base64_buf = Vec::<u8>::new(); |
| 130 | + |
| 131 | +/// Try and decode a base64 encoded file in place. |
| 132 | +/// |
| 133 | +/// Decodes the base64 encoded content of a file in place and writes the decoded content back to the |
| 134 | +/// file. If the file does not contain base64 encoded content, maintains the file as is. |
| 135 | +/// |
| 136 | +/// # Arguments |
| 137 | +/// |
| 138 | +/// * `filename` - A string slice that holds the path to the file to decode. |
| 139 | +pub fn try_decode_base64_file_inplace(filename: &str) -> Result<()> { |
| 140 | + // Open the file for reading and writing |
| 141 | + let mut file = File::open(filename)?; |
| 142 | + let mut base64_buf = Vec::new(); |
| 143 | + |
| 144 | + // Read the file content into the buffer |
137 | 145 | file.read_to_end(&mut base64_buf)?;
|
138 |
| - //try decode |
139 |
| - let decoded = BASE64_STANDARD.decode(base64_buf.as_slice()); |
140 |
| - // the file is not base64 encoded .It is ok .Do Nothing |
141 |
| - if let Err(_) = decoded { |
142 |
| - return Ok(()); |
143 |
| - } |
144 |
| - //try to clear file |
145 |
| - if let Err(e) = file.set_len(0) { |
146 |
| - return Err(anyhow!("fail to clear file,why? {}", e)); |
147 |
| - } |
148 |
| - if let Err(e) = file.seek(SeekFrom::Start(0)) { |
149 |
| - return Err(anyhow!("fail to clear file,why? {}", e)); |
| 146 | + |
| 147 | + // Try to decode the base64 content |
| 148 | + match BASE64_STANDARD.decode(&base64_buf) { |
| 149 | + Ok(decoded_bytes) => { |
| 150 | + // Truncate the file and seek to the beginning |
| 151 | + file.set_len(0)?; |
| 152 | + file.seek(SeekFrom::Start(0))?; |
| 153 | + |
| 154 | + // Write the decoded bytes back to the file |
| 155 | + let mut writer = BufWriter::new(&file); |
| 156 | + writer.write_all(&decoded_bytes)?; |
| 157 | + } |
| 158 | + Err(_) => { |
| 159 | + // If decoding fails, do nothing and return Ok |
| 160 | + return Ok(()); |
| 161 | + } |
150 | 162 | }
|
151 |
| - //write bytes to file |
152 |
| - let decoded_bytes = decoded.expect("this can't be happening"); |
153 |
| - file.write_all(&decoded_bytes)?; |
| 163 | + |
154 | 164 | Ok(())
|
155 | 165 | }
|
0 commit comments