Skip to content

Commit

Permalink
Merge pull request #7 from xuan525/dev
Browse files Browse the repository at this point in the history
Handle PathTooLongException
  • Loading branch information
xuan25 authored Jun 15, 2021
2 parents c4ea3dd + c7e7cd5 commit c5908d9
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Bili-dl/Bili-dl/BiliDownload/DownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ConfigUtil;
using FlvMerge;
using JsonUtil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
Expand Down Expand Up @@ -45,6 +46,8 @@ public enum Status { Analyzing, Downloading, Merging, Finished, AnalysisFailed }
public delegate void StatusUpdateDel(DownloadTask downloadTask, double progressPercentage, long bps, Status statues);
public event StatusUpdateDel StatusUpdate;

const int MAX_PATH = 260;

public DownloadTask(DownloadInfo downloadInfo)
{
Info = downloadInfo;
Expand Down Expand Up @@ -112,6 +115,8 @@ private bool Analysis()
}
}



private void Segment_Finished()
{
if (CurrentSegment < Segments.Count - 1)
Expand All @@ -124,9 +129,23 @@ private void Segment_Finished()
AbortProgressMonitor();
ProgressPercentage = 100;
StatusUpdate?.Invoke(this, ProgressPercentage, 0, Status.Merging);
string directory = ConfigManager.GetSettings().DownloadPath + "\\"+string.Format("[{0}]{1}", Description, Title)+"\\";
string directory = Path.Combine(ConfigManager.GetSettings().DownloadPath, FilenameValidation(string.Format("[{0}]{1}", Description, Title)));
Directory.CreateDirectory(directory);
string filepath = directory + FilenameValidation(string.Format("[{0}]{1}_{2}-{3}.{4}", Description, Title, Index, Part, Segments[0].Extention));
string filename = FilenameValidation($"{Index}-{Part}");
string filepath = Path.Combine(directory, $"{filename}.{Segments[0].Extention}");

// valid path length
if (filepath.Length > MAX_PATH - 1) // Reserve 1 char for <NULL>
{
int truncateCount = filepath.Length - (MAX_PATH - 1);
if(truncateCount > filename.Length - 1) // Least 1 char for filename
{
// Should never occur
throw new Exception($"无法创建合法文件名,截断失败: {filename} (截断计数: {truncateCount})");
}
filename = filename.Substring(0, filename.Length - truncateCount);
filepath = directory + $"{filename}.{Segments[0].Extention}";
}

int count = Segments.Count;
string[] paths = new string[count];
Expand Down

0 comments on commit c5908d9

Please sign in to comment.