Skip to content

Commit

Permalink
feat: v3.0.0, rename mdrive.db, add localstorage model
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Jun 17, 2024
1 parent df8cf9b commit 75169b2
Show file tree
Hide file tree
Showing 11 changed files with 703 additions and 462 deletions.
272 changes: 150 additions & 122 deletions src/MDriveSync.Client.API/Controllers/AliyunStorageController.cs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/MDriveSync.Client.API/MDriveSync.Client.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
</Content>
</ItemGroup>

<ItemGroup>
<Folder Include="data\" />
</ItemGroup>

</Project>

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/MDriveSync.Client.API/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MDrive</title>
<script type="module" crossorigin src="/assets/index-0jNC1vnT.js"></script>
<script type="module" crossorigin src="/assets/index-gNqp_E3O.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-zp8xT-Zl.css">
</head>
<body>
Expand Down
6 changes: 2 additions & 4 deletions src/MDriveSync.Core/DB/AliyunStorageDb.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using MDriveSync.Core.Options;

namespace MDriveSync.Core.DB
namespace MDriveSync.Core.DB
{
/// <summary>
/// 阿里云盘数据库
/// </summary>
public class AliyunStorageDb : SingletonBase<AliyunStorageDb>
{
private readonly LiteRepository<AliyunStorageConfig, string> _db = new("drive.db", false);
private readonly LiteRepository<AliyunStorageConfig, string> _db = new("mdrive.db", false);

public LiteRepository<AliyunStorageConfig, string> DB
{
Expand Down
2 changes: 1 addition & 1 deletion src/MDriveSync.Core/DB/LocalStorageDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MDriveSync.Core.DB
/// </summary>
public class LocalStorageDb : SingletonBase<LocalStorageDb>
{
private readonly LiteRepository<LocalStorageConfig, string> _db = new("drive.db", false);
private readonly LiteRepository<LocalStorageConfig, string> _db = new("mdrive.db", false);

public LiteRepository<LocalStorageConfig, string> DB
{
Expand Down
264 changes: 198 additions & 66 deletions src/MDriveSync.Core/DownloadManager.cs

Large diffs are not rendered by default.

27 changes: 24 additions & 3 deletions src/MDriveSync.Core/Models/DownloadTask.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MDriveSync.Core.DB;
using ServiceStack.DataAnnotations;
using System.Reflection.Metadata.Ecma335;

namespace MDriveSync.Core.Models
{
Expand Down Expand Up @@ -160,6 +161,8 @@ public string FileName
[Ignore]
public string SpeedString => Speed switch
{
// 如果 <=0 返回 -
var s when s <= 0 => "-",
var s when s >= 1024 * 1024 * 1024 => $"{s / 1024 / 1024 / 1024:F2} GB/s",
var s when s >= 1024 * 1024 => $"{s / 1024 / 1024:F2} MB/s",
var s when s >= 1024 => $"{s / 1024:F2} KB/s",
Expand All @@ -172,17 +175,17 @@ public string FileName
public string AliyunDriveId { get; set; }

/// <summary>
/// 云盘 ID(云盘作业标识)
/// 云盘存储 ID / 本地存储 ID
/// </summary>
public string DriveId { get; set; }
public string StorageConfigId { get; set; }

/// <summary>
/// 作业 ID
/// </summary>
public string JobId { get; set; }

/// <summary>
/// 文件 ID
/// 文件 ID / Key
/// </summary>
public string FileId { get; set; }

Expand All @@ -200,6 +203,11 @@ public string FileName
/// 错误消息
/// </summary>
public string Error { get; set; }

/// <summary>
/// 是否为本地文件
/// </summary>
public bool IsLocalFile { get; set; }
}

/// <summary>
Expand All @@ -216,18 +224,31 @@ public class DownloadTaskRequest
public string JobId { get; set; }

public string FileId { get; set; }

/// <summary>
/// 是否为本地文件
/// </summary>
public bool IsLocalFile { get; set; }
}

/// <summary>
/// 批量下载请求
/// </summary>
public class BatchDownloadRequest
{
/// <summary>
/// 文件唯一标识或 keys
/// </summary>
public List<string> FileIds { get; set; } = new List<string>();

public string JobId { get; set; }

public string FilePath { get; set; }

/// <summary>
/// 是否为本地文件
/// </summary>
public bool IsLocalFile { get; set; }
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/MDriveSync.Core/Services/AliyunDriveMounterByJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,8 @@ public NtStatus GetDiskFreeSpace(out long freeBytesAvailable, out long totalNumb
// diskSpaceInfo.UsedSpace - 云盘已使用的空间
// diskSpaceInfo.FreeSpace - 云盘的剩余空间

totalNumberOfBytes = _job.CurrrentDrive?.Metadata?.TotalSize ?? long.MaxValue;
totalNumberOfFreeBytes = _job.CurrrentDrive?.Metadata?.UsedSize ?? 0;
totalNumberOfBytes = _job.CurrrentStorageConfig?.Metadata?.TotalSize ?? long.MaxValue;
totalNumberOfFreeBytes = _job.CurrrentStorageConfig?.Metadata?.UsedSize ?? 0;
freeBytesAvailable = totalNumberOfBytes > 0 ? totalNumberOfBytes - totalNumberOfFreeBytes : long.MaxValue;

return NtStatus.Success;
Expand Down
40 changes: 20 additions & 20 deletions src/MDriveSync.Core/Services/AliyunJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public class AliyunJob : IDisposable
private string _driveId;

// 客户端信息
private AliyunStorageConfig _driveConfig;
private AliyunStorageConfig _currentStorageConfig;

/// <summary>
/// 阿里云盘备份盘/资源盘 ID
Expand All @@ -137,7 +137,7 @@ public class AliyunJob : IDisposable
/// <summary>
/// 当前云盘
/// </summary>
public AliyunStorageConfig CurrrentDrive => _driveConfig;
public AliyunStorageConfig CurrrentStorageConfig => _currentStorageConfig;

/// <summary>
/// 云盘所有文件夹
Expand Down Expand Up @@ -219,7 +219,7 @@ public AliyunJob(AliyunStorageConfig driveConfig, AliyunJobConfig jobConfig, ILo
// 本地缓存
_cache = new MemoryCache(new MemoryCacheOptions());

_driveConfig = driveConfig;
_currentStorageConfig = driveConfig;
_jobConfig = jobConfig;

// 上传请求
Expand Down Expand Up @@ -564,7 +564,7 @@ private string AccessToken
{
get
{
return AliyunDriveToken.Instance.GetAccessToken(_driveConfig.Id);
return AliyunDriveToken.Instance.GetAccessToken(_currentStorageConfig.Id);
}
}

Expand Down Expand Up @@ -1199,7 +1199,7 @@ public void JobUpdate(AliyunJobConfig cfg)
throw new LogicException("作业标识错误");
}

var drive = AliyunStorageDb.Instance.DB.GetAll().Where(c => c.Id == _driveConfig.Id).FirstOrDefault();
var drive = AliyunStorageDb.Instance.DB.GetAll().Where(c => c.Id == _currentStorageConfig.Id).FirstOrDefault();
if (drive == null)
{
throw new LogicException("配置配置错误,请重启程序");
Expand Down Expand Up @@ -1244,7 +1244,7 @@ public void JobUpdate(AliyunJobConfig cfg)
_jobConfig.MountConfig.MountReadOnly = cfg.MountConfig?.MountReadOnly ?? false;
_jobConfig.MountConfig.MountPoint = cfg?.MountConfig?.MountPoint;

_driveConfig.SaveJob(_jobConfig);
_currentStorageConfig.SaveJob(_jobConfig);
}

/// <summary>
Expand Down Expand Up @@ -1345,7 +1345,7 @@ public void JobStateChange(JobState state)
}
CurrentState = state;
_jobConfig.State = state;
_driveConfig.SaveJob(_jobConfig);
_currentStorageConfig.SaveJob(_jobConfig);
}
else if (state == JobState.Deleted)
{
Expand All @@ -1355,7 +1355,7 @@ public void JobStateChange(JobState state)
{
throw new LogicException($"当前作业处于 {CurrentState.GetDescription()} 状态,不能删除作业");
}
_driveConfig.SaveJob(_jobConfig, true);
_currentStorageConfig.SaveJob(_jobConfig, true);
}
else if (state == JobState.None)
{
Expand All @@ -1367,7 +1367,7 @@ public void JobStateChange(JobState state)
}
CurrentState = state;
_jobConfig.State = state;
_driveConfig.SaveJob(_jobConfig);
_currentStorageConfig.SaveJob(_jobConfig);
}
else
{
Expand Down Expand Up @@ -1907,7 +1907,7 @@ private void AliyunDriveInitialize()
AliyunDriveInitVipInfo();
// 保存配置
_driveConfig.Save();
_currentStorageConfig.Save();
sw.Stop();
_log.LogInformation($"作业初始化完成,用时:{sw.ElapsedMilliseconds}ms");
Expand Down Expand Up @@ -2133,7 +2133,7 @@ await Parallel.ForEachAsync(addFileKeys, options, async (item, cancellationToken
TotalSize = _driveFiles.Values.Sum(c => c.Size ?? 0)
};

_driveConfig.SaveJob(_jobConfig);
_currentStorageConfig.SaveJob(_jobConfig);

// 校验通过 -> 空闲
ChangeState(JobState.Idle);
Expand Down Expand Up @@ -3002,7 +3002,7 @@ private void AliyunDriveInitInfo()
_driveId = data.ResourceDriveId;
}

_driveConfig.Name = !string.IsNullOrWhiteSpace(data.NickName) ? data.NickName : data.Name;
_currentStorageConfig.Name = !string.IsNullOrWhiteSpace(data.NickName) ? data.NickName : data.Name;
}

/// <summary>
Expand All @@ -3013,9 +3013,9 @@ private void AliyunDriveInitSpaceInfo()
{
var data = _driveApi.SpaceInfo(AccessToken);

_driveConfig.Metadata ??= new();
_driveConfig.Metadata.UsedSize = data?.PersonalSpaceInfo?.UsedSize;
_driveConfig.Metadata.TotalSize = data?.PersonalSpaceInfo?.TotalSize;
_currentStorageConfig.Metadata ??= new();
_currentStorageConfig.Metadata.UsedSize = data?.PersonalSpaceInfo?.UsedSize;
_currentStorageConfig.Metadata.TotalSize = data?.PersonalSpaceInfo?.TotalSize;
}

/// <summary>
Expand All @@ -3025,10 +3025,10 @@ private void AliyunDriveInitSpaceInfo()
private void AliyunDriveInitVipInfo()
{
var data = _driveApi.VipInfo(AccessToken);
_driveConfig.Metadata ??= new();
_driveConfig.Metadata.Identity = data?.Identity;
_driveConfig.Metadata.Level = data?.Level;
_driveConfig.Metadata.Expire = data?.ExpireDateTime;
_currentStorageConfig.Metadata ??= new();
_currentStorageConfig.Metadata.Identity = data?.Identity;
_currentStorageConfig.Metadata.Level = data?.Level;
_currentStorageConfig.Metadata.Expire = data?.ExpireDateTime;
}

/// <summary>
Expand Down Expand Up @@ -3938,7 +3938,7 @@ public void DriveMount()
//_mountDrive = new AliyunDriveMounterByJob(mountPoint, this, _driveFolders, _driveFiles);
//_mountDrive.Mount();

_mountDrive = new AliyunDriveMounter(_driveConfig, _jobConfig.MountConfig, _jobConfig.Name);
_mountDrive = new AliyunDriveMounter(_currentStorageConfig, _jobConfig.MountConfig, _jobConfig.Name);
_mountDrive.Mount();
}

Expand Down
Loading

0 comments on commit 75169b2

Please sign in to comment.