mirror of
https://github.com/mii443/rs-docker-bot.git
synced 2025-08-22 16:15:40 +00:00
add extraction to download_file
This commit is contained in:
@ -22,6 +22,7 @@ strum_macros = "0.24"
|
||||
log = "0.4.0"
|
||||
env_logger = "0.9.0"
|
||||
poise = "0.6.1"
|
||||
anyhow = "1.0.89"
|
||||
|
||||
[dependencies.tokio]
|
||||
version = "1.0"
|
||||
|
@ -1,10 +1,10 @@
|
||||
use core::slice::SlicePattern;
|
||||
use std::{
|
||||
io::Read,
|
||||
sync::mpsc::{self, Receiver, Sender},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use bollard::{
|
||||
container::{
|
||||
CreateContainerOptions, DownloadFromContainerOptions, ListContainersOptions, LogOutput,
|
||||
@ -16,7 +16,7 @@ use bollard::{
|
||||
};
|
||||
use flate2::{write::GzEncoder, Compression};
|
||||
use futures_util::StreamExt;
|
||||
use tar::Header;
|
||||
use tar::{Archive, Header};
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
use crate::language::Language;
|
||||
@ -174,7 +174,7 @@ impl Container {
|
||||
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 options = Some(DownloadFromContainerOptions { path });
|
||||
let mut download = docker.download_from_container(&self.id, options);
|
||||
@ -183,7 +183,20 @@ impl Container {
|
||||
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) {
|
||||
|
@ -89,7 +89,7 @@ async fn on_message(ctx: &serenity::Context, data: &Data, message: &Message) {
|
||||
let timeout = Arc::new(Mutex::new(false));
|
||||
let t = Arc::clone(&timeout);
|
||||
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(()) {
|
||||
*t.lock().unwrap() = true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user