add extraction to download_file

This commit is contained in:
mii443
2024-10-10 07:53:34 +00:00
parent 771f1409db
commit 3cdc980865
3 changed files with 19 additions and 5 deletions

View File

@ -22,6 +22,7 @@ strum_macros = "0.24"
log = "0.4.0" log = "0.4.0"
env_logger = "0.9.0" env_logger = "0.9.0"
poise = "0.6.1" poise = "0.6.1"
anyhow = "1.0.89"
[dependencies.tokio] [dependencies.tokio]
version = "1.0" version = "1.0"

View File

@ -1,10 +1,10 @@
use core::slice::SlicePattern;
use std::{ use std::{
io::Read, io::Read,
sync::mpsc::{self, Receiver, Sender}, sync::mpsc::{self, Receiver, Sender},
time::Duration, time::Duration,
}; };
use anyhow::{Context, Result};
use bollard::{ use bollard::{
container::{ container::{
CreateContainerOptions, DownloadFromContainerOptions, ListContainersOptions, LogOutput, CreateContainerOptions, DownloadFromContainerOptions, ListContainersOptions, LogOutput,
@ -16,7 +16,7 @@ use bollard::{
}; };
use flate2::{write::GzEncoder, Compression}; use flate2::{write::GzEncoder, Compression};
use futures_util::StreamExt; use futures_util::StreamExt;
use tar::Header; use tar::{Archive, Header};
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use crate::language::Language; use crate::language::Language;
@ -174,7 +174,7 @@ impl Container {
return None; return None;
} }
pub async fn download_file(&self, path: &str) -> Vec<u8> { pub async fn download_file(&self, path: &str) -> Result<Vec<u8>> {
let docker = Docker::connect_with_local_defaults().unwrap(); let docker = Docker::connect_with_local_defaults().unwrap();
let options = Some(DownloadFromContainerOptions { path }); let options = Some(DownloadFromContainerOptions { path });
let mut download = docker.download_from_container(&self.id, options); let mut download = docker.download_from_container(&self.id, options);
@ -183,7 +183,20 @@ impl Container {
result.append(&mut d.into()); result.append(&mut d.into());
} }
result Self::extract(result)
}
fn extract(data: Vec<u8>) -> Result<Vec<u8>> {
let mut archive = Archive::new(data.as_slice());
let mut entry = archive
.entries()?
.next()
.context("File not found")?
.context("File not found.")?;
let mut result = vec![];
entry.read_to_end(&mut result)?;
Ok(result)
} }
pub async fn upload_file(&self, content: &str, file_name: String) { pub async fn upload_file(&self, content: &str, file_name: String) {

View File

@ -89,7 +89,7 @@ async fn on_message(ctx: &serenity::Context, data: &Data, message: &Message) {
let timeout = Arc::new(Mutex::new(false)); let timeout = Arc::new(Mutex::new(false));
let t = Arc::clone(&timeout); let t = Arc::clone(&timeout);
tokio::spawn(async move { tokio::spawn(async move {
sleep_until(Instant::now() + Duration::from_secs(10)).await; sleep_until(Instant::now() + Duration::from_secs(30)).await;
if let Ok(_) = end_tx.send(()) { if let Ok(_) = end_tx.send(()) {
*t.lock().unwrap() = true; *t.lock().unwrap() = true;
} }