Skip to content

Commit

Permalink
feat: DEMO model
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Jun 19, 2024
1 parent 9cf4e0e commit 346719a
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ docker run --name mdrive -d --restart=always \

- 只读模式:WebUI 下如果开启只读模式,则允许编辑和修改,只能查看,默认 `ReadOnly: false`。使用方式,可以通过修改 `appsettings.json` 或 docker 使用环境变量 `-e READ_ONLY=true`
- 基础认证:WebUI 账号和密码,如果开启则打开网站管理后台时需要输入账号和密码,默认启用 `BasicAuth`。使用方式,可以通过修改 `appsettings.json` 或 docker 使用环境变量 ` -e BASIC_AUTH_USER=admin -e BASIC_AUTH_PASSWORD=123456`
- 演示模式:网站配置为演示模式,只能查看和下载,功能受限,默认 `Demo: null`。使用方式,可以通过修改 `appsettings.json` 或 docker 使用环境变量 `-e DEMO=true`

## 系统配置

Expand All @@ -221,6 +222,10 @@ docker run --name mdrive -d --restart=always \
{
// 网站配置为只读模式
"ReadOnly": null,

// 网站配置为演示模式
"Demo": null,

// 网站登录授权账号密码
"BasicAuth": {
"User": "",
Expand Down Expand Up @@ -468,6 +473,7 @@ docker run --name mdrive -d --restart=always \

## 路线图

- [3.5] 支持 Linux 磁盘挂载。
- [3.x] 支持超时自动锁定管理后台。
- [3.x] 支持在线上传。
- [3.x] 多模块支持,支持 Duplicati、Kopia 模块,直接进行加密。
Expand Down
58 changes: 58 additions & 0 deletions src/MDriveSync.Client.API/Controllers/AliyunStorageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public Result<List<AliyunStorageConfig>> GetDrives()
[HttpGet("export")]
public IActionResult Export()
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

var aliyunDriveConfigs = AliyunStorageDb.Instance.DB.GetAll();
var localJobConfigs = LocalStorageDb.Instance.DB.GetAll();

Expand All @@ -81,6 +86,11 @@ public IActionResult Export()
[HttpPost("import")]
public Result Import([FromForm] IFormFile file)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

if (file == null)
{
return Result.Fail("文件不能为空");
Expand Down Expand Up @@ -162,6 +172,11 @@ public Result Import([FromForm] IFormFile file)
[HttpPost("mount/{driveId}")]
public Result DriveMount(string driveId)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_timedHostedService.DriveMount(driveId);
return Result.Ok();
}
Expand All @@ -174,6 +189,11 @@ public Result DriveMount(string driveId)
[HttpPost("unmount/{driveId}")]
public Result DriveUnmount(string driveId)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_timedHostedService.DriveUnmount(driveId);
return Result.Ok();
}
Expand All @@ -186,6 +206,11 @@ public Result DriveUnmount(string driveId)
[HttpPost("job/mount/{jobId}")]
public Result DriveJobMount(string jobId)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_timedHostedService.DriveJobMount(jobId);
return Result.Ok();
}
Expand All @@ -198,6 +223,11 @@ public Result DriveJobMount(string jobId)
[HttpPost("job/unmount/{jobId}")]
public Result DriveJobUnmount(string jobId)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_timedHostedService.DriveJobUnmount(jobId);
return Result.Ok();
}
Expand Down Expand Up @@ -767,6 +797,11 @@ public FilePathKeyResult GetDetail(string jobId, string fileId)
[HttpPost()]
public Result DriveAdd([FromBody] AliyunDriveEditRequest cfg)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_timedHostedService.DriveAdd(cfg);
return Result.Ok();
}
Expand All @@ -780,6 +815,11 @@ public Result DriveAdd([FromBody] AliyunDriveEditRequest cfg)
[HttpPut("{driveId}")]
public Result DriveEdit(string driveId, [FromBody] AliyunDriveEditRequest cfg)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_timedHostedService.DriveEdit(driveId, cfg);
return Result.Ok();
}
Expand All @@ -792,6 +832,11 @@ public Result DriveEdit(string driveId, [FromBody] AliyunDriveEditRequest cfg)
[HttpDelete("{driveId}")]
public Result DriveDelete(string driveId)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_timedHostedService.DriveDelete(driveId);
return Result.Ok();
}
Expand Down Expand Up @@ -819,6 +864,11 @@ public Result JobUpdate([FromBody] AliyunJobConfig cfg)
[HttpPost("job/{driveId}")]
public Result JobAdd(string driveId, [FromBody] AliyunJobConfig cfg)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_timedHostedService.JobAdd(driveId, cfg);
return Result.Ok();
}
Expand All @@ -832,6 +882,14 @@ public Result JobAdd(string driveId, [FromBody] AliyunJobConfig cfg)
[HttpPut("job/{jobId}/{state}")]
public Result JobStateChange(string jobId, JobState state)
{
if (state == JobState.Deleted)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}
}

var jobs = _timedHostedService.Jobs();
if (jobs.TryGetValue(jobId, out var job) && job != null)
{
Expand Down
29 changes: 29 additions & 0 deletions src/MDriveSync.Client.API/Controllers/LocalStorageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MDriveSync.Core.DB;
using MDriveSync.Core.Models;
using MDriveSync.Core.ViewModels;
using MDriveSync.Infrastructure;
using Microsoft.AspNetCore.Mvc;

namespace MDriveSync.Client.API.Controllers
Expand Down Expand Up @@ -61,6 +62,11 @@ public Result<List<LocalJobConfig>> GetJobs()
[HttpPost()]
public Result StorageAdd([FromBody] LocalStorageEditRequest cfg)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_localStorageHostedService.DriveAdd(cfg.Name);
return Result.Ok();
}
Expand All @@ -74,6 +80,11 @@ public Result StorageAdd([FromBody] LocalStorageEditRequest cfg)
[HttpPut("{storageId}")]
public Result StorageEdit(string storageId, [FromBody] LocalStorageEditRequest cfg)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_localStorageHostedService.DriveEdit(storageId, cfg.Name);
return Result.Ok();
}
Expand All @@ -86,6 +97,11 @@ public Result StorageEdit(string storageId, [FromBody] LocalStorageEditRequest c
[HttpDelete("{storageId}")]
public Result StorageDelete(string storageId)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_localStorageHostedService.DriveDelete(storageId);
return Result.Ok();
}
Expand Down Expand Up @@ -113,6 +129,11 @@ public Result JobUpdate([FromBody] LocalJobConfig cfg)
[HttpPost("job/{storageId}")]
public Result JobAdd(string storageId, [FromBody] LocalJobConfig cfg)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}

_localStorageHostedService.JobAdd(storageId, cfg);
return Result.Ok();
}
Expand All @@ -126,6 +147,14 @@ public Result JobAdd(string storageId, [FromBody] LocalJobConfig cfg)
[HttpPut("job/{jobId}/{state}")]
public Result JobStateChange(string jobId, JobState state)
{
if (state == JobState.Deleted)
{
if (GlobalConfiguration.IsDemoMode == true)
{
throw new LogicException("演示模式,禁止操作");
}
}

var jobs = _localStorageHostedService.Jobs();
if (jobs.TryGetValue(jobId, out var job) && job != null)
{
Expand Down
14 changes: 14 additions & 0 deletions src/MDriveSync.Client.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MDriveSync.Core.Dashboard;
using MDriveSync.Core.Filters;
using MDriveSync.Core.Middlewares;
using MDriveSync.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Quartz.Logging;
using Serilog;
Expand Down Expand Up @@ -160,6 +161,7 @@ public static void Main(string[] args)
var passwordFromConfig = builder.Configuration["BasicAuth:Password"];
var user = string.IsNullOrEmpty(userFromConfig) ?
Environment.GetEnvironmentVariable("BASIC_AUTH_USER") : userFromConfig;

var password = string.IsNullOrEmpty(passwordFromConfig) ?
Environment.GetEnvironmentVariable("BASIC_AUTH_PASSWORD") : passwordFromConfig;

Expand Down Expand Up @@ -202,11 +204,23 @@ public static void Main(string[] args)
isReadOnlyMode = ro;
}
}

if (isReadOnlyMode == true)
{
app.UseMiddleware<ReadOnlyMiddleware>(isReadOnlyMode);
}

// 演示模式处理
var isDemoMode = builder.Configuration.GetSection("Demo").Get<bool?>();
if (isDemoMode != true)
{
if (bool.TryParse(Environment.GetEnvironmentVariable("DEMO"), out var demo) && demo)
{
isDemoMode = demo;
}
}
GlobalConfiguration.IsDemoMode = isDemoMode;

app.MapControllers();

//app.MapGet("/", () =>
Expand Down
4 changes: 4 additions & 0 deletions src/MDriveSync.Client.API/appsettings.Example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
// 网站配置为只读模式
"ReadOnly": null,

// 网站配置为演示模式
"Demo": null,

// 网站登录授权账号密码
"BasicAuth": {
"User": "",
Expand Down
10 changes: 10 additions & 0 deletions src/MDriveSync.Core/Services/AliyunJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,16 @@ public void JobUpdate(AliyunJobConfig cfg)
throw new LogicException("参数错误,请填写必填选项,且符合规范");
}

if (GlobalConfiguration.IsDemoMode == true)
{
if (_jobConfig.Sources.Except(cfg.Sources).Any()
|| cfg.Sources.Except(_jobConfig.Sources).Any()
|| _jobConfig.Target != cfg.Target)
{
throw new LogicException("演示模式,禁止修改来源和目标");
}
}

var allowJobStates = new[] { JobState.Idle, JobState.Error, JobState.Cancelled, JobState.Disabled, JobState.Completed };
if (!allowJobStates.Contains(CurrentState))
{
Expand Down
10 changes: 10 additions & 0 deletions src/MDriveSync.Core/Services/LocalStorageJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,16 @@ public void JobUpdate(LocalJobConfig cfg)
throw new LogicException("参数错误,请填写必填选项,且符合规范");
}

if (GlobalConfiguration.IsDemoMode == true)
{
if (_jobConfig.Sources.Except(cfg.Sources).Any()
|| cfg.Sources.Except(_jobConfig.Sources).Any()
|| _jobConfig.Target != cfg.Target)
{
throw new LogicException("演示模式,禁止修改来源和目标");
}
}

var allowJobStates = new[] { JobState.Idle, JobState.Error, JobState.Cancelled, JobState.Disabled, JobState.Completed };
if (!allowJobStates.Contains(CurrentState))
{
Expand Down
13 changes: 13 additions & 0 deletions src/MDriveSync.Infrastructure/GlobalConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MDriveSync.Infrastructure
{
/// <summary>
/// 全局配置
/// </summary>
public class GlobalConfiguration
{
/// <summary>
/// 网站配置为演示模式
/// </summary>
public static bool? IsDemoMode { get; set; }
}
}

0 comments on commit 346719a

Please sign in to comment.