dev2.0
洁 任 2025-07-07 15:33:30 +08:00
commit 04ef99dcc0
4 changed files with 45 additions and 18 deletions

View File

@ -269,10 +269,12 @@ public class MinioDownloadTask : NotifyPropertyChangedBase
{ {
// 验证文件是否存在 // 验证文件是否存在
if (File.Exists(Path.Combine(FilePath, item.Key))) continue; if (File.Exists(Path.Combine(FilePath, item.Key))) continue;
// 1 文件夹 2 存储桶
var localDir = Type == 1 ? FilePath : Path.Combine(FilePath, BucketName); var localDir = Type == 1 ? FilePath : Path.Combine(FilePath, BucketName);
try try
{ {
await Minio.DownLoadObject(BucketName, item.Key, localDir, item.ETag, StopDownTs.Token); // 修改
await Minio.DownLoadObject(BucketName, item.Key, localDir, item.ETag,FileName, StopDownTs.Token);
} }
catch (Exception e) catch (Exception e)
{ {

View File

@ -2,6 +2,7 @@
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Threading.Channels; using System.Threading.Channels;
using System.Windows.Forms.VisualStyles;
using Hopetry.Provider; using Hopetry.Provider;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Minio; using Minio;
@ -285,17 +286,37 @@ namespace Hopetry.Services
public async Task DownLoadObject(string bucketName, string objectKey, string localDir, string objectETag, public async Task DownLoadObject(string bucketName, string objectKey, string localDir, string objectETag,
CancellationToken token = default) CancellationToken token = default)
{ {
var index = objectKey.LastIndexOf("/", StringComparison.Ordinal); await DownLoadObject(bucketName, objectKey, localDir, objectETag, objectKey, token);
}
public async Task DownLoadObject(string bucketName, string objectKey, string localDir, string objectETag,
string name, CancellationToken token = default)
{
var dir = Path.Combine(localDir);
string temp;
if (name.Equals(bucketName))
{
// todo 桶下载
temp = Path.Combine(localDir, objectKey);
}
else
{
temp = objectKey.Substring(objectKey.IndexOf(name, StringComparison.Ordinal));
}
temp = temp.Replace("\\","/" );
var index = temp.LastIndexOf("/", StringComparison.Ordinal);
if (index > 0) if (index > 0)
{ {
var dir = Path.Combine(localDir, objectKey.Substring(0, index)); dir = Path.Combine(localDir, temp.Substring(0, index));
if (!Directory.Exists(dir)) if (!Directory.Exists(dir))
{ {
Directory.CreateDirectory(dir); Directory.CreateDirectory(dir);
} }
} }
var localPath = Path.Combine(localDir, objectKey.Replace('/', Path.DirectorySeparatorChar)); var localPath = Path.Combine(dir, Path.GetFileName(objectKey));
var getArgs = new GetObjectArgs() var getArgs = new GetObjectArgs()
.WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName) .WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName)
.WithObject(objectKey) .WithObject(objectKey)
@ -347,7 +368,8 @@ namespace Hopetry.Services
} }
// 实时监听方法 // 实时监听方法
public async Task RealTimeListen(string bucketName, string localDir,Action<string> action, string prefix = "", string suffix = "", public async Task RealTimeListen(string bucketName, string localDir, Action<string> action, string prefix = "",
string suffix = "",
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
Console.WriteLine("开启实时监听"); Console.WriteLine("开启实时监听");
@ -438,15 +460,6 @@ namespace Hopetry.Services
}; };
var token = downTask.StopDownTs.Token; var token = downTask.StopDownTs.Token;
long totalBytes = 0; long totalBytes = 0;
var index = objectKey.LastIndexOf("/", StringComparison.Ordinal);
if (index > 0)
{
var dir = Path.Combine(filePath, objectKey.Substring(0, index));
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
var args = new StatObjectArgs() var args = new StatObjectArgs()
.WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName) .WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName)
@ -455,7 +468,18 @@ namespace Hopetry.Services
totalBytes = stat.Size; totalBytes = stat.Size;
//var localPath = Path.Combine(filePath, objectKey.Replace('/', Path.DirectorySeparatorChar)); //var localPath = Path.Combine(filePath, objectKey.Replace('/', Path.DirectorySeparatorChar));
var localPath = Path.Combine(filePath, var localPath = Path.Combine(filePath,
objectKey); objectKey[(objectKey.LastIndexOf('/') + 1)..]);
var index = localPath.LastIndexOf("/", StringComparison.Ordinal);
if (index > 0)
{
var dir = localPath.Substring(0, index);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
var getObjectArgs = new GetObjectArgs() var getObjectArgs = new GetObjectArgs()
.WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName) .WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName)
.WithObject(objectKey) .WithObject(objectKey)

View File

@ -449,7 +449,7 @@ namespace HeBianGu.App.Disk
var temp = (MinIODirectoryInfo)dir.Model; var temp = (MinIODirectoryInfo)dir.Model;
//Console.WriteLine($"bucket: {temp.BucketName} objectKey {temp.FullName}"); //Console.WriteLine($"bucket: {temp.BucketName} objectKey {temp.FullName}");
var objectKey = temp.FullName.Replace(temp.BucketName + "/", ""); var objectKey = temp.FullName.Replace(temp.BucketName + "/", "");
ViewModelLocator.DownViewModel.AddDirTask(temp.BucketName, objectKey, downDir); ViewModelLocator.DownViewModel.AddDirTask(temp.BucketName, objectKey,temp.Name, downDir);
} }
MessageProxy.Snacker.Show("已加入下载队列"); MessageProxy.Snacker.Show("已加入下载队列");

View File

@ -428,6 +428,7 @@ public class DownViewModel : MvcViewModelBase
// 异常释放信号源 // 异常释放信号源
//_semaphore.Release(); //_semaphore.Release();
Console.WriteLine($"下载失败或取消:{ex.Message}"); Console.WriteLine($"下载失败或取消:{ex.Message}");
Console.WriteLine(ex.StackTrace);
} }
finally finally
{ {
@ -549,7 +550,7 @@ public class DownViewModel : MvcViewModelBase
{ {
} }
public async void AddDirTask(string bucketName, string objectKey, string downDir) public async void AddDirTask(string bucketName, string objectKey, string name,string downDir)
{ {
// 情景1 只选了一个文件夹 // 情景1 只选了一个文件夹
// 情景2 只选了一个存储桶 // 情景2 只选了一个存储桶
@ -559,7 +560,7 @@ public class DownViewModel : MvcViewModelBase
FilePath = downDir, FilePath = downDir,
ObjectKey = objectKey, ObjectKey = objectKey,
BucketName = bucketName, BucketName = bucketName,
FileName = objectKey, FileName = name, // todo 确定修改这里是否有影响
Status = "等待中", Status = "等待中",
FileIcon = "\xe87a", FileIcon = "\xe87a",
//DownloadInfo = "0/na", //DownloadInfo = "0/na",