feat: add empty task handler for hard_link_or_copy (#310)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2024-03-12 20:16:01 +08:00 committed by GitHub
parent fe341c2c6b
commit 36f8c62260
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 36 additions and 13 deletions

View File

@ -56,25 +56,41 @@ impl Content {
// hard_link_or_copy_task hard links or copies the task content to the destination. // hard_link_or_copy_task hard links or copies the task content to the destination.
pub async fn hard_link_or_copy_task( pub async fn hard_link_or_copy_task(
&self, &self,
task_id: &str, task: super::metadata::Task,
to: &Path, to: &Path,
range: Option<Range>, range: Option<Range>,
) -> Result<()> { ) -> Result<()> {
// Copy the task content to the destination by range // Copy the task content to the destination by range
// if the range is specified. // if the range is specified.
if let Some(range) = range { if let Some(range) = range {
self.copy_task_by_range(task_id, to, range).await?; // If the range length is 0, no need to copy. Need to open the file to
// ensure the file exists.
if range.length == 0 {
info!("range length is 0, no need to copy");
File::create(to).await?;
return Ok(());
}
self.copy_task_by_range(task.id.as_str(), to, range).await?;
info!("copy range of task success"); info!("copy range of task success");
return Ok(()); return Ok(());
} }
// If the hard link fails, // If the hard link fails,
// copy the task content to the destination. // copy the task content to the destination.
if let Err(err) = self.hard_link_task(task_id, to).await { if let Err(err) = self.hard_link_task(task.id.as_str(), to).await {
info!("hard link task failed: {}", err); info!("hard link task failed: {}", err);
// If the task is empty, no need to copy. Need to open the file to
// ensure the file exists.
if task.is_empty() {
info!("task is empty, no need to copy");
File::create(to).await?;
return Ok(());
}
fs::remove_file(to).await?; fs::remove_file(to).await?;
self.copy_task(task_id, to).await?; self.copy_task(task.id.as_str(), to).await?;
info!("copy task success"); info!("copy task success");
return Ok(()); return Ok(());
} }

View File

@ -58,13 +58,11 @@ impl Storage {
// hard_link_or_copy_task hard links or copies the task content to the destination. // hard_link_or_copy_task hard links or copies the task content to the destination.
pub async fn hard_link_or_copy_task( pub async fn hard_link_or_copy_task(
&self, &self,
task_id: &str, task: metadata::Task,
to: &Path, to: &Path,
range: Option<Range>, range: Option<Range>,
) -> Result<()> { ) -> Result<()> {
self.content self.content.hard_link_or_copy_task(task, to, range).await
.hard_link_or_copy_task(task_id, to, range)
.await
} }
// read_task_by_range returns the reader of the task by range. // read_task_by_range returns the reader of the task by range.

View File

@ -100,6 +100,17 @@ impl Task {
self.finished_at.is_some() self.finished_at.is_some()
} }
// is_empty returns whether the task is empty.
pub fn is_empty(&self) -> bool {
if let Some(content_length) = self.content_length() {
if content_length == 0 {
return true;
}
}
false
}
// content_length returns the content length of the task. // content_length returns the content length of the task.
pub fn content_length(&self) -> Option<u64> { pub fn content_length(&self) -> Option<u64> {
match self.response_header.get(header::CONTENT_LENGTH.as_str()) { match self.response_header.get(header::CONTENT_LENGTH.as_str()) {

View File

@ -274,7 +274,7 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
// Hard link or copy the task content to the destination. // Hard link or copy the task content to the destination.
if let Err(err) = task_manager if let Err(err) = task_manager
.hard_link_or_copy( .hard_link_or_copy(
task_id.as_str(), task,
Path::new(output_path.as_str()), Path::new(output_path.as_str()),
download.range.clone(), download.range.clone(),
) )

View File

@ -165,13 +165,11 @@ impl Task {
// hard_link_or_copy hard links or copies the task content to the destination. // hard_link_or_copy hard links or copies the task content to the destination.
pub async fn hard_link_or_copy( pub async fn hard_link_or_copy(
&self, &self,
task_id: &str, task: metadata::Task,
to: &Path, to: &Path,
range: Option<Range>, range: Option<Range>,
) -> ClientResult<()> { ) -> ClientResult<()> {
self.storage self.storage.hard_link_or_copy_task(task, to, range).await
.hard_link_or_copy_task(task_id, to, range)
.await
} }
// download downloads a task. // download downloads a task.