feat: add empty task handler for hard_link_or_copy (#310)
Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
parent
fe341c2c6b
commit
36f8c62260
|
|
@ -56,25 +56,41 @@ impl Content {
|
|||
// hard_link_or_copy_task hard links or copies the task content to the destination.
|
||||
pub async fn hard_link_or_copy_task(
|
||||
&self,
|
||||
task_id: &str,
|
||||
task: super::metadata::Task,
|
||||
to: &Path,
|
||||
range: Option<Range>,
|
||||
) -> Result<()> {
|
||||
// Copy the task content to the destination by range
|
||||
// if the range is specified.
|
||||
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");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// If the hard link fails,
|
||||
// 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);
|
||||
|
||||
// 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?;
|
||||
self.copy_task(task_id, to).await?;
|
||||
self.copy_task(task.id.as_str(), to).await?;
|
||||
info!("copy task success");
|
||||
return Ok(());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,13 +58,11 @@ impl Storage {
|
|||
// hard_link_or_copy_task hard links or copies the task content to the destination.
|
||||
pub async fn hard_link_or_copy_task(
|
||||
&self,
|
||||
task_id: &str,
|
||||
task: metadata::Task,
|
||||
to: &Path,
|
||||
range: Option<Range>,
|
||||
) -> Result<()> {
|
||||
self.content
|
||||
.hard_link_or_copy_task(task_id, to, range)
|
||||
.await
|
||||
self.content.hard_link_or_copy_task(task, to, range).await
|
||||
}
|
||||
|
||||
// read_task_by_range returns the reader of the task by range.
|
||||
|
|
|
|||
|
|
@ -100,6 +100,17 @@ impl Task {
|
|||
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.
|
||||
pub fn content_length(&self) -> Option<u64> {
|
||||
match self.response_header.get(header::CONTENT_LENGTH.as_str()) {
|
||||
|
|
|
|||
|
|
@ -274,7 +274,7 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
|
|||
// Hard link or copy the task content to the destination.
|
||||
if let Err(err) = task_manager
|
||||
.hard_link_or_copy(
|
||||
task_id.as_str(),
|
||||
task,
|
||||
Path::new(output_path.as_str()),
|
||||
download.range.clone(),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -165,13 +165,11 @@ impl Task {
|
|||
// hard_link_or_copy hard links or copies the task content to the destination.
|
||||
pub async fn hard_link_or_copy(
|
||||
&self,
|
||||
task_id: &str,
|
||||
task: metadata::Task,
|
||||
to: &Path,
|
||||
range: Option<Range>,
|
||||
) -> ClientResult<()> {
|
||||
self.storage
|
||||
.hard_link_or_copy_task(task_id, to, range)
|
||||
.await
|
||||
self.storage.hard_link_or_copy_task(task, to, range).await
|
||||
}
|
||||
|
||||
// download downloads a task.
|
||||
|
|
|
|||
Loading…
Reference in New Issue