public System.Net.Cache.RequestCacheLevel CacheLevel = System.Net.Cache.RequestCacheLevel.NoCacheNoStore;
/// <summary>
+ /// ダウンロード時に downloadedFileName に改名するか否か。
+ /// </summary>
+ protected bool enableChangeFileName = false;
+
+ /// <summary>
/// キャンセルが呼ばれたか否か。
/// </summary>
private bool cancelCalled = false;
/// <summary>
+ /// ダウンロード中のファイル名につける接尾辞
+ /// </summary>
+ private static readonly string PartialFilePostfix = ".part";
+
+ /// <summary>
/// コンストラクタ
/// </summary>
/// <param name="url">ダウンロード先URL</param>
}
/// <summary>
+ /// 保存先ファイル名を本来のファイル名に変えるか。
+ /// </summary>
+ public bool EnableChangeFileName {
+ get { return enableChangeFileName; }
+ set { enableChangeFileName = value; }
+ }
+
+ /// <summary>
+ /// 保存ファイル。
+ /// </summary>
+ public string Filepath {
+ get { return filepath; }
+ }
+
+ /// <summary>
/// キャンセル可能
/// </summary>
public override bool Cancelable {
} catch (Exception) {
}
- if (File.Exists(filepath)) { // ファイルが存在するとき削除
+ // パス名を変えるときは、HTTPヘッダから取得したファイル名に変更する。
+ if (enableChangeFileName && (!string.IsNullOrEmpty(downloadedFileName))) {
+ filepath = Path.Combine(Path.GetDirectoryName(filepath), downloadedFileName);
+ }
+
+ // ファイルが存在するとき削除
+ if (File.Exists(filepath)) {
File.Delete(filepath);
}
+ // 部分ファイルが存在するときも削除 TODO レジューム処理
+ if (File.Exists(filepath + PartialFilePostfix)) {
+ File.Delete(filepath + PartialFilePostfix);
+ }
}
private void runDownloadToFile()
{
Stopwatch stopwatch = new Stopwatch();
+ string partialFilepath = filepath + PartialFilePostfix;
using (Stream stream = response.GetResponseStream() )
- using (FileStream fs = new FileStream(filepath,
+ using (FileStream fs = new FileStream(partialFilepath,
FileMode.Create,
FileAccess.Write) ) {
+
try {
- File.SetAttributes(filepath, FileAttributes.Hidden);
+ File.SetAttributes(partialFilepath, FileAttributes.Hidden);
long contentLength = response.ContentLength;
stopwatch.Start();
} finally {
timer.Dispose();
}
-
- File.SetAttributes(filepath, FileAttributes.Normal);
} catch (IOException ex) {
if (cancelCalled) {
throw new TaskCanceledException(string.Empty);
}
}
}
+
+ if (File.Exists(partialFilepath)) {
+ File.Move(partialFilepath, filepath);
+ File.SetAttributes(filepath, FileAttributes.Normal);
+ }
}
private void runPostprocess()
- {
+ {
// 更新日を補完
if (File.Exists(filepath)) {
HttpWebResponse httpResponse = response as HttpWebResponse;
DownloadSubTask dlSTask = new DownloadSubTask(inst.InstallerURL, inst.InstallerFile);
VirusScanSubTask scanSTask = new VirusScanSubTask(scanner, inst.InstallerFile, inst.InstallerURL);
+ dlSTask.EnableChangeFileName = true;
dlSTask.TaskEventRaised += delegate(object sender, TaskEventArgs e) {
if (e.Type == TaskEventType.COMPLETED) {
- scanSTask.TargetFilePath = inst.InstallerFile;
+ scanSTask.TargetFilePath = inst.InstallerFile = dlSTask.Filepath;
}
};