Skip to content

Commit afba55b

Browse files
authored
Merge pull request #171 from rage/download-old-submission-fix
Download old submission fix
2 parents 13f3d55 + 5b734bd commit afba55b

File tree

11 files changed

+276
-327
lines changed

11 files changed

+276
-327
lines changed

tmc-client/src/error.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! The client error type.
22
33
use reqwest::{Method, StatusCode};
4-
use std::path::PathBuf;
54
use thiserror::Error;
65
use tmc_langs_util::FileError;
76
use url::Url;
@@ -38,8 +37,8 @@ pub enum ClientError {
3837
TokenParse(String, #[source] serde_json::error::Error),
3938
#[error("Failed to parse as URL: {0}")]
4039
UrlParse(String, #[source] url::ParseError),
41-
#[error("Failed to write response to {0}")]
42-
HttpWriteResponse(PathBuf, #[source] reqwest::Error),
40+
#[error("Failed to write response")]
41+
HttpWriteResponse(#[source] reqwest::Error),
4342
#[error("Failed to deserialize response from {0} as JSON")]
4443
HttpJsonResponse(Url, #[source] reqwest::Error),
4544

tmc-client/src/tmc_client.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use oauth2::{
1111
};
1212
use reqwest::{blocking::Client, Url};
1313
use serde::{Deserialize, Serialize};
14-
use std::collections::HashMap;
1514
use std::io::Write;
1615
use std::path::{Path, PathBuf};
1716
use std::sync::Arc;
1817
use std::thread;
1918
use std::time::Duration;
19+
use std::{collections::HashMap, io::Cursor};
2020
use tempfile::NamedTempFile;
2121
use tmc_langs_util::{progress_reporter, FileError};
2222

@@ -404,9 +404,10 @@ impl TmcClient {
404404
pub fn download_old_submission(
405405
&self,
406406
submission_id: usize,
407-
target: &Path,
407+
mut target: impl Write,
408408
) -> Result<(), ClientError> {
409-
self.download_submission(submission_id, target)
409+
log::info!("downloading old submission {}", submission_id);
410+
self.download_submission(submission_id, &mut target)
410411
}
411412

412413
pub fn get_exercise_submissions_for_current_user(
@@ -584,9 +585,9 @@ impl TmcClient {
584585
solution_download_url: Url,
585586
target: &Path,
586587
) -> Result<(), ClientError> {
587-
let zip_file = NamedTempFile::new().map_err(ClientError::TempFile)?;
588-
self.download_from(solution_download_url, zip_file.path())?;
589-
tmc_langs_plugins::extract_project(zip_file, target, false)?;
588+
let mut buf = vec![];
589+
self.download_from(solution_download_url, &mut buf)?;
590+
tmc_langs_plugins::extract_project(Cursor::new(buf), target, false)?;
590591
Ok(())
591592
}
592593

tmc-client/src/tmc_client/api.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use reqwest::{
1414
};
1515
use serde::de::DeserializeOwned;
1616
use serde_json::Value;
17-
use std::collections::HashMap;
1817
use std::path::Path;
1918
use std::time::SystemTime;
19+
use std::{collections::HashMap, io::Write};
2020
use tmc_langs_plugins::Language;
21-
use tmc_langs_util::{file_util, FileError};
21+
use tmc_langs_util::FileError;
2222
use url::Url;
2323

2424
/// Provides a wrapper for reqwest Response's json that deserializes into Response<T> and converts it into a result
@@ -121,15 +121,14 @@ impl TmcClient {
121121
.json_res()
122122
}
123123

124-
fn download(&self, url_tail: &str, target: &Path) -> Result<(), ClientError> {
124+
fn download(&self, url_tail: &str, mut target: impl Write) -> Result<(), ClientError> {
125125
let url = self
126126
.0
127127
.api_url
128128
.join(url_tail)
129129
.map_err(|e| ClientError::UrlParse(url_tail.to_string(), e))?;
130130

131131
// download zip
132-
let mut target_file = file_util::create_file(target)?;
133132
log::debug!("downloading {}", url);
134133
let mut response = self
135134
.0
@@ -168,24 +167,26 @@ impl TmcClient {
168167
}
169168
} else {
170169
response
171-
.copy_to(&mut target_file)
172-
.map_err(|e| ClientError::HttpWriteResponse(target.to_path_buf(), e))?;
170+
.copy_to(&mut target)
171+
.map_err(ClientError::HttpWriteResponse)?;
173172
Ok(())
174173
}
175174
}
176175

177-
pub(crate) fn download_from(&self, url: Url, target: &Path) -> Result<(), ClientError> {
178-
// download zip
179-
let mut target_file = file_util::create_file(target)?;
176+
pub(crate) fn download_from(
177+
&self,
178+
url: Url,
179+
mut target: impl Write,
180+
) -> Result<(), ClientError> {
180181
log::debug!("downloading {}", url);
181182
self.0
182183
.client
183184
.get(url.clone())
184185
.tmc_headers(self)
185186
.send()
186187
.map_err(|e| ClientError::ConnectionError(Method::GET, url, e))?
187-
.copy_to(&mut target_file)
188-
.map_err(|e| ClientError::HttpWriteResponse(target.to_path_buf(), e))?;
188+
.copy_to(&mut target)
189+
.map_err(ClientError::HttpWriteResponse)?;
189190
Ok(())
190191
}
191192

@@ -578,7 +579,7 @@ impl TmcClient {
578579
organization_slug: &str,
579580
course_name: &str,
580581
exercise_name: &str,
581-
target: &Path,
582+
target: impl Write,
582583
) -> Result<(), ClientError> {
583584
if self.0.token.is_none() {
584585
return Err(ClientError::NotLoggedIn);
@@ -639,7 +640,11 @@ impl TmcClient {
639640
todo!("needs admin?");
640641
}
641642

642-
pub fn download_exercise(&self, exercise_id: usize, target: &Path) -> Result<(), ClientError> {
643+
pub fn download_exercise(
644+
&self,
645+
exercise_id: usize,
646+
target: impl Write,
647+
) -> Result<(), ClientError> {
643648
let url_tail = format!("core/exercises/{}/download", exercise_id);
644649
self.download(&url_tail, target)
645650
}
@@ -686,7 +691,7 @@ impl TmcClient {
686691
pub(super) fn download_solution(
687692
&self,
688693
exercise_id: usize,
689-
target: &Path,
694+
target: impl Write,
690695
) -> Result<(), ClientError> {
691696
if self.0.token.is_none() {
692697
return Err(ClientError::NotLoggedIn);
@@ -800,7 +805,7 @@ impl TmcClient {
800805
pub(super) fn download_submission(
801806
&self,
802807
submission_id: usize,
803-
target: &Path,
808+
target: impl Write,
804809
) -> Result<(), ClientError> {
805810
if self.0.token.is_none() {
806811
return Err(ClientError::NotLoggedIn);

tmc-langs-cli/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ fn run_core_inner(
598598
tmc_langs::download_old_submission(
599599
&client,
600600
exercise_id,
601-
output_path,
601+
&output_path,
602602
submission_id,
603603
submission_url,
604604
)?;
@@ -854,7 +854,7 @@ fn run_core_inner(
854854
None,
855855
)?;
856856
}
857-
tmc_langs::reset(&client, exercise_id, exercise_path)?;
857+
tmc_langs::reset(&client, exercise_id, &exercise_path)?;
858858
Output::finished("reset exercise")
859859
}
860860

0 commit comments

Comments
 (0)