-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
201a209
commit 4bd9ac9
Showing
13 changed files
with
201 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.IO.Compression; | ||
|
||
namespace Nerosoft.Starfish.Common; | ||
public static class GzipHelper | ||
{ | ||
public static string CompressToBase64(string source) | ||
{ | ||
var buffer = Compress(source); | ||
|
||
return Convert.ToBase64String(buffer); | ||
} | ||
|
||
public static byte[] Compress(string source) | ||
{ | ||
var data = Encoding.UTF8.GetBytes(source); | ||
|
||
var stream = new MemoryStream(); | ||
var zip = new GZipStream(stream, CompressionMode.Compress, true); | ||
zip.Write(data, 0, data.Length); | ||
zip.Close(); | ||
var buffer = new byte[stream.Length]; | ||
stream.Position = 0; | ||
_ = stream.Read(buffer, 0, buffer.Length); | ||
stream.Close(); | ||
|
||
return buffer; | ||
} | ||
|
||
public static string DecompressFromBase64(string base64Data) | ||
{ | ||
var data = Convert.FromBase64String(base64Data); | ||
return Decompress(data); | ||
} | ||
|
||
public static string Decompress(byte[] data) | ||
{ | ||
return Decompress(data, data.Length); | ||
} | ||
|
||
public static string Decompress(byte[] data, int count) | ||
{ | ||
var stream = new MemoryStream(data, 0, count); | ||
var zip = new GZipStream(stream, CompressionMode.Decompress, true); | ||
var destStream = new MemoryStream(); | ||
var buffer = new byte[0x1000]; | ||
while (true) | ||
{ | ||
var reader = zip.Read(buffer, 0, buffer.Length); | ||
if (reader <= 0) | ||
{ | ||
break; | ||
} | ||
|
||
destStream.Write(buffer, 0, reader); | ||
} | ||
|
||
zip.Close(); | ||
stream.Close(); | ||
destStream.Position = 0; | ||
buffer = destStream.ToArray(); | ||
destStream.Close(); | ||
return Encoding.UTF8.GetString(buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
Source/Starfish.Service/UseCases/Setting/PushRedisUseCase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Text.Json; | ||
using Nerosoft.Euonia.Application; | ||
using Nerosoft.Euonia.Claims; | ||
using Nerosoft.Starfish.Common; | ||
using Nerosoft.Starfish.Domain; | ||
using Nerosoft.Starfish.Transit; | ||
using StackExchange.Redis; | ||
|
||
namespace Nerosoft.Starfish.UseCases; | ||
|
||
internal interface IPushRedisUseCase : INonOutputUseCase<PushRedisInput>; | ||
|
||
internal record PushRedisInput(long AppId, string Environment, PushRedisRequestDto Data) : IUseCaseInput; | ||
|
||
internal sealed class PushRedisUseCase : IPushRedisUseCase | ||
{ | ||
private readonly ISettingArchiveRepository _settingRepository; | ||
private readonly IAppInfoRepository _appInfoRepository; | ||
private readonly UserPrincipal _identity; | ||
|
||
public PushRedisUseCase(ISettingArchiveRepository settingRepository, IAppInfoRepository appInfoRepository, UserPrincipal identity) | ||
{ | ||
_settingRepository = settingRepository; | ||
_appInfoRepository = appInfoRepository; | ||
_identity = identity; | ||
} | ||
|
||
public async Task ExecuteAsync(PushRedisInput input, CancellationToken cancellationToken = default) | ||
{ | ||
var permission = await _appInfoRepository.CheckPermissionAsync(input.AppId, _identity.GetUserIdOfInt64(), cancellationToken); | ||
|
||
if (!permission.IsIn(1, 2)) | ||
{ | ||
throw new UnauthorizedAccessException(); | ||
} | ||
|
||
var archive = await _settingRepository.GetAsync(input.AppId, input.Environment, cancellationToken); | ||
|
||
if (archive == null) | ||
{ | ||
throw new SettingNotFoundException(input.AppId, input.Environment); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(archive.Data)) | ||
{ | ||
throw new InvalidDataException(); | ||
} | ||
|
||
var data = GzipHelper.DecompressFromBase64(archive.Data); | ||
|
||
var items = JsonSerializer.Deserialize<Dictionary<string, string>>(data); | ||
|
||
var connection = ConnectionMultiplexer.Connect(input.Data.ConnectionString); | ||
using (connection) | ||
{ | ||
var entries = items.Select(t => new HashEntry(t.Key, t.Value)).ToArray(); | ||
|
||
var database = connection.GetDatabase(input.Data.Database); | ||
|
||
await database.HashSetAsync(input.Data.Key, entries); | ||
await database.KeyExpireAsync(input.Data.Key, default(TimeSpan?)); | ||
await connection.CloseAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Nerosoft.Starfish.Transit; | ||
|
||
/// <summary> | ||
/// 同步配置到Redis请求Dto | ||
/// </summary> | ||
public class PushRedisRequestDto | ||
{ | ||
/// <summary> | ||
/// Redis连接字符串 | ||
/// </summary> | ||
public string ConnectionString { get; set; } | ||
|
||
/// <summary> | ||
/// 数据库 | ||
/// </summary> | ||
public int Database { get; set; } | ||
|
||
/// <summary> | ||
/// Redis键名称 | ||
/// </summary> | ||
public string Key { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.