mirror of
https://github.com/Megghy/MegghysAPI.git
synced 2025-12-06 14:16:56 +08:00
添加项目文件。
This commit is contained in:
141
Core/FileManager.cs
Normal file
141
Core/FileManager.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using MegghysAPI.Attributes;
|
||||
using Minio;
|
||||
using Minio.DataModel.Args;
|
||||
|
||||
namespace MegghysAPI.Core
|
||||
{
|
||||
internal class FileManager
|
||||
{
|
||||
|
||||
private static IMinioClient minio = new MinioClient()
|
||||
.WithEndpoint(Config.Instance.MinIOEndpoint)
|
||||
.WithRegion(Config.Instance.MinIORegion)
|
||||
.WithCredentials(Config.Instance.MinIOAccessKey, Config.Instance.MinIOSecretKey)
|
||||
.WithSSL(false)
|
||||
.Build();
|
||||
public const string FavoritePath = "/Files/Favorite/";
|
||||
[AutoInit("初始化文件服务")]
|
||||
public static void Init()
|
||||
{
|
||||
}
|
||||
public static async Task<string> DownloadFileAsync(string fileName, string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
var temp = Path.Combine(Datas.TempFilePath, fileName);
|
||||
if (File.Exists(temp))
|
||||
return temp;
|
||||
var result = await minio.GetObjectAsync(new Minio.DataModel.Args.GetObjectArgs()
|
||||
.WithBucket(Config.Instance.MinIOBucket)
|
||||
.WithObject(fileName)
|
||||
.WithCallbackStream(stream =>
|
||||
{
|
||||
// 将下载流保存为本地文件
|
||||
using var fileStream = File.Create(temp);
|
||||
stream.CopyTo(fileStream);
|
||||
}));
|
||||
|
||||
return temp;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logs.Warn($"未能从储存空间下载文件: {path}{fileName}\r\n{ex}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static bool UploadBytes(byte[] data, string remotePath, string contentType = "application/octet-stream", bool overwrite = false)
|
||||
{
|
||||
using var stream = new MemoryStream(data);
|
||||
return UploadStream(stream, remotePath, contentType, overwrite: overwrite);
|
||||
}
|
||||
public static async Task<bool> UploadStreamAsync(Stream stream, string remotePath, string contentType = "application/octet-stream", bool overwrite = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!overwrite && await ContainsFileAsync(remotePath))
|
||||
return true;
|
||||
// Upload a file to bucket.
|
||||
var putObjectArgs = new PutObjectArgs()
|
||||
.WithBucket(Config.Instance.MinIOBucket)
|
||||
.WithStreamData(stream)
|
||||
.WithObjectSize(stream.Length)
|
||||
.WithObject(remotePath)
|
||||
.WithContentType(contentType);
|
||||
await minio.PutObjectAsync(putObjectArgs);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logs.Warn($"未能向储存空间路径上传文件流 {remotePath}\r\n{ex}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static bool UploadStream(Stream stream, string remotePath, string contentType = "application/octet-stream", bool overwrite = false)
|
||||
=> UploadStreamAsync(stream, remotePath, contentType, overwrite).Result;
|
||||
/*public static async Task<FtpStatus> UploadFileAsync(string localFilePath, string remotePath = SetuPath, bool overwrite = false)
|
||||
{
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(localFilePath))
|
||||
return false;
|
||||
return await FTPConnection.UploadFile(localFilePath, remotePath, overwrite ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logs.Warn($"未能向储存空间路径上传文件 {localFilePath} => {remotePath}\r\n{ex}");
|
||||
return FtpStatus.Failed;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static FtpStatus UploadFile(string localFilePath, string remotePath = SetuPath, bool overwrite = false)
|
||||
=> UploadFileAsync(localFilePath, remotePath, overwrite).Result;*/
|
||||
public static async Task<string[]> ListFilesNameAsync(string path)
|
||||
{
|
||||
return (await ListFilesAsync(path)).Select(x => x.Name).ToArray();
|
||||
}
|
||||
|
||||
public class MItem(Minio.DataModel.Item item) : Minio.DataModel.Item
|
||||
{
|
||||
public string Name { get; set; } = item.Key.Contains('/') ? item.Key[(item.Key.LastIndexOf('/') + 1)..] : item.Key;
|
||||
}
|
||||
public static async Task<MItem[]> ListFilesAsync(string path, bool recursive = false, bool includeUserMetadata = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = minio.ListObjectsEnumAsync(new ListObjectsArgs()
|
||||
.WithBucket(Config.Instance.MinIOBucket)
|
||||
.WithRecursive(recursive)
|
||||
.WithIncludeUserMetadata(includeUserMetadata));
|
||||
|
||||
var fileList = new List<Minio.DataModel.Item>();
|
||||
await foreach (var item in result)
|
||||
{
|
||||
fileList.Add(item);
|
||||
}
|
||||
return [.. fileList.Select(x => new MItem(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logs.Warn($"未能列出储存空间中 {path} 的文件\r\n{ex}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static async Task<bool> ContainsFileAsync(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
var stat = await minio.StatObjectAsync(
|
||||
new StatObjectArgs()
|
||||
.WithBucket(Config.Instance.MinIOBucket)
|
||||
.WithObject(path)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
50
Core/TimerManager.cs
Normal file
50
Core/TimerManager.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using MegghysAPI.Attributes;
|
||||
|
||||
namespace MegghysAPI.Core
|
||||
{
|
||||
public static class TimerManager
|
||||
{
|
||||
|
||||
private static Dictionary<MethodInfo, AutoTimerAttribute> timers = new();
|
||||
private static long time = 0;
|
||||
[AutoInit(Order = 100)]
|
||||
public static void RegisterAll()
|
||||
{
|
||||
System.Timers.Timer temp = new()
|
||||
{
|
||||
Interval = 1000,
|
||||
AutoReset = true,
|
||||
};
|
||||
temp.Elapsed += (_, _) =>
|
||||
{
|
||||
if (time != 0)
|
||||
timers.Where(timer => time % timer.Value.Time == 0)
|
||||
.ForEach(timer =>
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
var sw = new Stopwatch();
|
||||
sw.Start();
|
||||
timer.Key.Invoke(null, null);
|
||||
sw.Stop();
|
||||
if (timer.Value.Log)
|
||||
Logs.Text($"[{sw.ElapsedMilliseconds} ms] [{timer.Key.DeclaringType.Name}.{timer.Key.Name}] 计时器触发 [{timer.Value.Time}-{time}]");
|
||||
});
|
||||
});
|
||||
time++;
|
||||
};
|
||||
AppDomain.CurrentDomain.GetAssemblies()
|
||||
.ForEach(a => a.GetTypes().Where(t => t.IsClass).ForEach(t => t.GetMethods().Where(m => m.GetCustomAttributes(true).FirstOrDefault(a => a is AutoTimerAttribute) != null)
|
||||
.ForEach(m =>
|
||||
{
|
||||
var attr = m.GetCustomAttributes(true).FirstOrDefault(a => a is AutoTimerAttribute) as AutoTimerAttribute;
|
||||
timers.Add(m, attr);
|
||||
Logs.Info($"注册自动计时器 {m.DeclaringType.Name}.{m.Name} <{attr.Time} s>");
|
||||
})));
|
||||
temp.Start();
|
||||
timers.Where(timer => timer.Value.CallOnRegister).ForEach(timer => Task.Run(() => timer.Key.Invoke(null, null)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user