Skip to content

Commit

Permalink
mount on job, fix: #3
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Jan 19, 2024
1 parent 625d404 commit e8ec149
Show file tree
Hide file tree
Showing 6 changed files with 1,086 additions and 1,022 deletions.
1,001 changes: 1,001 additions & 0 deletions src/MDriveSync.Client.API/wwwroot/assets/index-KxJlzSCy.js

Large diffs are not rendered by default.

1,001 changes: 0 additions & 1,001 deletions src/MDriveSync.Client.API/wwwroot/assets/index-Rwnb1OPm.js

This file was deleted.

24 changes: 12 additions & 12 deletions src/MDriveSync.Client.API/wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<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-Rwnb1OPm.js"></script>
<!doctype html>
<html lang="en">
<head>
<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-KxJlzSCy.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-Sa9bKrsM.css">
</head>
<body>
<div id="root"></div>
</body>
</html>
</head>
<body>
<div id="root"></div>
</body>
</html>
61 changes: 53 additions & 8 deletions src/MDriveSync.Core/Services/AliyunDriveMounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ public class AliyunDriveMounter : IDokanOperations, IDisposable
/// </summary>
private string _driveParentFileId = "root";

/// <summary>
/// 别名
/// </summary>
private string _alias = "";

/// <summary>
/// 显示真实大小
/// </summary>
private bool _isRealSize;

/// <summary>
/// 下载请求
/// </summary>
Expand All @@ -137,9 +147,25 @@ public class AliyunDriveMounter : IDokanOperations, IDisposable
Timeout = TimeSpan.FromMinutes(45)
};

public AliyunDriveMounter(AliyunDriveConfig driveConfig, AliyunDriveMountConfig driveMountConfig)
/// <summary>
/// 创建挂载
/// </summary>
/// <param name="driveConfig">云盘配置</param>
/// <param name="driveMountConfig">挂载配置</param>
/// <param name="alias">别名</param>
public AliyunDriveMounter(
AliyunDriveConfig driveConfig,
AliyunDriveMountConfig driveMountConfig,
string alias = "")
{
_log = Log.Logger;
_alias = alias;

// 如果挂载为目录,则显示真实大小
if (!string.IsNullOrWhiteSpace(driveMountConfig.MountPath))
{
_isRealSize = true;
}

_driveApi = new AliyunDriveApi();

Expand Down Expand Up @@ -196,7 +222,8 @@ private async Task<byte[]> DownloadFileSegment(string url, int start, int end)
/// <returns></returns>
private string GetPathKey(string fileName)
{
return $"{_driveMountConfig.MountPath.TrimPath()}/{fileName.TrimPath()}".ToUrlPath();
// {_driveMountConfig.MountPath.TrimPath()}/
return $"{fileName.TrimPath()}".ToUrlPath();
}

/// <summary>
Expand Down Expand Up @@ -1445,7 +1472,7 @@ public IList<FileInformation> FindFilesHelper(string fileName, string searchPatt
}

/// <summary>
/// 创建文件/文件夹
/// 创建/打开文件/文件夹
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="access">访问权限</param>
Expand Down Expand Up @@ -1743,9 +1770,9 @@ public NtStatus ReadFile(string fileName, byte[] buffer, out int bytesRead, long
partialContent = DownloadFileSegment(url, (int)offset, endOffset).GetAwaiter().GetResult();
}

if (fileName.Contains("jpg"))
{
}
//if (fileName.Contains("jpg"))
//{
//}

// 确保不会复制超出 buffer 大小的数据
int bytesToCopy = Math.Min(buffer.Length, partialContent.Length);
Expand Down Expand Up @@ -1919,8 +1946,21 @@ public NtStatus GetDiskFreeSpace(out long freeBytesAvailable, out long totalNumb
// diskSpaceInfo.FreeSpace - 云盘的剩余空间

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

// 如果显示真实使用量,则根据文件计算
if (_isRealSize)
{
totalNumberOfFreeBytes = _driveFiles.Sum(x => x.Value.Size ?? 0);
freeBytesAvailable = totalNumberOfBytes > 0 ? totalNumberOfBytes - totalNumberOfFreeBytes : long.MaxValue;

_log.Information($"真实大小 {totalNumberOfFreeBytes}");
}
else
{
totalNumberOfFreeBytes = _driveConfig?.Metadata?.UsedSize ?? 0;
freeBytesAvailable = totalNumberOfBytes > 0 ? totalNumberOfBytes - totalNumberOfFreeBytes : long.MaxValue;
}


return NtStatus.Success;
}
Expand All @@ -1939,6 +1979,11 @@ public NtStatus GetVolumeInformation(out string volumeLabel, out FileSystemFeatu
// 设置卷标,这个标签可以根据您的需求自定义,例如"我的云盘"
volumeLabel = _driveConfig?.Name ?? "我的云盘";

if (!string.IsNullOrWhiteSpace(_alias))
{
volumeLabel += $"({_alias})";
}

// 设置文件系统的特性。这些特性描述了文件系统支持的不同功能。
// 例如,可以设置为支持Unicode文件名、持久ACLs(访问控制列表)、大文件等
features = FileSystemFeatures.UnicodeOnDisk |
Expand Down
14 changes: 13 additions & 1 deletion src/MDriveSync.Core/Services/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,18 @@ public void JobUpdate(JobConfig cfg)
throw new LogicException("作业标识错误");
}

var drive = DriveDb.Instacne.GetAll().Where(c => c.Id == _driveConfig.Id).FirstOrDefault();
if (drive == null)
{
throw new LogicException("配置配置错误,请重启程序");
}

// 禁止作业指向同一目标
if (!string.IsNullOrWhiteSpace(cfg.Target) && drive.Jobs.Any(x => x.Target == cfg.Target))
{
throw new LogicException("多个作业禁止指向云盘同一个目标目录");
}

// 清除表达式所有作业
_schedulers.Clear();

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

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

Expand Down
7 changes: 7 additions & 0 deletions src/MDriveSync.Core/Services/TimedHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ public void JobAdd(string driveId, JobConfig cfg)
cfg.Id = Guid.NewGuid().ToString("N");

drive.Jobs ??= new List<JobConfig>();

// 禁止作业指向同一目标
if (!string.IsNullOrWhiteSpace(cfg.Target) && drive.Jobs.Any(x => x.Target == cfg.Target))
{
throw new LogicException("多个作业禁止指向云盘同一个目标目录");
}

drive.Jobs.Add(cfg);

// 持久化
Expand Down

0 comments on commit e8ec149

Please sign in to comment.