first commit
This commit is contained in:
98
client/Reader/MainForm.cs
Normal file
98
client/Reader/MainForm.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Reader
|
||||
{
|
||||
public partial class MainForm : Form
|
||||
{
|
||||
private static readonly HttpClient http = new HttpClient();
|
||||
private const string ServerUrl = "http://localhost:5000";
|
||||
|
||||
public MainForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void btnBrowse_Click(object sender, EventArgs e)
|
||||
{
|
||||
using var fbd = new FolderBrowserDialog();
|
||||
if (fbd.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
txtFolder.Text = fbd.SelectedPath;
|
||||
}
|
||||
}
|
||||
|
||||
private async void btnRead_Click(object sender, EventArgs e)
|
||||
{
|
||||
var folder = txtFolder.Text;
|
||||
if (string.IsNullOrWhiteSpace(folder) || !Directory.Exists(folder))
|
||||
{
|
||||
MessageBox.Show("请选择有效的文件夹。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
dgv.Rows.Clear();
|
||||
var files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories);
|
||||
var shaList = new System.Collections.Generic.List<string>();
|
||||
var fileMap = new System.Collections.Generic.Dictionary<string, string>();
|
||||
foreach (var f in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
var sha = await Task.Run(() => ComputeSha256(f));
|
||||
shaList.Add(sha);
|
||||
fileMap[sha] = f;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
if (shaList.Count == 0)
|
||||
{
|
||||
MessageBox.Show("没有找到文件。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
return;
|
||||
}
|
||||
var payload = new { shas = shaList };
|
||||
var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
|
||||
var resp = await http.PostAsync(ServerUrl + "/read", content);
|
||||
if (!resp.IsSuccessStatusCode)
|
||||
{
|
||||
MessageBox.Show("读取失败: " + await resp.Content.ReadAsStringAsync(), "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
var text = await resp.Content.ReadAsStringAsync();
|
||||
using var doc = JsonDocument.Parse(text);
|
||||
var root = doc.RootElement;
|
||||
var data = root.GetProperty("data");
|
||||
foreach (var sha in shaList)
|
||||
{
|
||||
var fileName = Path.GetFileName(fileMap[sha]);
|
||||
if (data.TryGetProperty(sha, out var item) && item.ValueKind != JsonValueKind.Null)
|
||||
{
|
||||
var up = item.GetProperty("upload_code").GetString() ?? "";
|
||||
var down = item.GetProperty("download_code").GetString() ?? "";
|
||||
var created = item.GetProperty("created_at").GetString() ?? "";
|
||||
dgv.Rows.Add(fileName, up, down, created);
|
||||
}
|
||||
else
|
||||
{
|
||||
dgv.Rows.Add(fileName, "无", "无", "无");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string ComputeSha256(string filePath)
|
||||
{
|
||||
using var sha = SHA256.Create();
|
||||
using var stream = File.OpenRead(filePath);
|
||||
var hash = sha.ComputeHash(stream);
|
||||
var sb = new StringBuilder();
|
||||
foreach (var b in hash) sb.Append(b.ToString("x2"));
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user