diff --git a/WinUINotes.Grpc/Protos/FileTransfer.proto b/WinUINotes.Grpc/Protos/FileTransfer.proto new file mode 100644 index 0000000..ba7d95e --- /dev/null +++ b/WinUINotes.Grpc/Protos/FileTransfer.proto @@ -0,0 +1,54 @@ +syntax = "proto3"; + +option csharp_namespace = "WinUINotes.Grpc"; + +package filetransfer; + +service FileTransfer { + rpc InitUpload (InitUploadRequest) returns (InitUploadResponse); + rpc UploadChunk (stream UploadChunkRequest) returns (UploadChunkResponse); + rpc CompleteUpload (CompleteUploadRequest) returns (CompleteUploadResponse); + rpc CheckUploadStatus (CheckUploadStatusRequest) returns (CheckUploadStatusResponse); +} + +message InitUploadRequest { + string file_name = 1; + int64 file_size = 2; + string file_hash = 3; +} + +message InitUploadResponse { + string session_id = 1; + int64 uploaded_bytes = 2; +} + +message UploadChunkRequest { + string session_id = 1; + bytes chunk_data = 2; + int64 offset = 3; +} + +message UploadChunkResponse { + int64 uploaded_bytes = 1; +} + +message CompleteUploadRequest { + string session_id = 1; + string file_hash = 2; +} + +message CompleteUploadResponse { + bool success = 1; + string message = 2; +} + +message CheckUploadStatusRequest { + string file_name = 1; + string file_hash = 2; +} + +message CheckUploadStatusResponse { + string session_id = 1; + int64 uploaded_bytes = 2; + bool exists = 3; +} \ No newline at end of file diff --git a/WinUINotes.Grpc/Protos/greet.proto b/WinUINotes.Grpc/Protos/greet.proto index 4ae009f..1589d63 100644 --- a/WinUINotes.Grpc/Protos/greet.proto +++ b/WinUINotes.Grpc/Protos/greet.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -option csharp_namespace = "WinUINotes"; +option csharp_namespace = "WinUINotes.Grpc"; package greet; diff --git a/WinUINotes.Grpc/Protos/my_diary.proto b/WinUINotes.Grpc/Protos/my_diary.proto index a2c9919..12deff5 100644 --- a/WinUINotes.Grpc/Protos/my_diary.proto +++ b/WinUINotes.Grpc/Protos/my_diary.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -option csharp_namespace = "ZR.Admin.Grpc"; +option csharp_namespace = "WinUINotes.Grpc"; package my.diary; diff --git a/WinUINotes.Grpc/WinUINotes.Grpc.csproj b/WinUINotes.Grpc/WinUINotes.Grpc.csproj index 793582c..a7be502 100644 --- a/WinUINotes.Grpc/WinUINotes.Grpc.csproj +++ b/WinUINotes.Grpc/WinUINotes.Grpc.csproj @@ -20,5 +20,6 @@ + \ No newline at end of file diff --git a/WinUINotes/AboutPage.xaml b/WinUINotes/AboutPage.xaml new file mode 100644 index 0000000..1daed03 --- /dev/null +++ b/WinUINotes/AboutPage.xaml @@ -0,0 +1,14 @@ + + + + + + + diff --git a/WinUINotes/AboutPage.xaml.cs b/WinUINotes/AboutPage.xaml.cs new file mode 100644 index 0000000..8ecd2e1 --- /dev/null +++ b/WinUINotes/AboutPage.xaml.cs @@ -0,0 +1,31 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Navigation; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Foundation; +using Windows.Foundation.Collections; + +// To learn more about WinUI, the WinUI project structure, +// and more about our project templates, see: http://aka.ms/winui-project-info. + +namespace WinUINotes +{ + /// + /// An empty page that can be used on its own or navigated to within a Frame. + /// + public sealed partial class AboutPage : Page + { + public AboutPage() + { + InitializeComponent(); + } + } +} diff --git a/WinUINotes/App.xaml.cs b/WinUINotes/App.xaml.cs index ed69ee4..a859194 100644 --- a/WinUINotes/App.xaml.cs +++ b/WinUINotes/App.xaml.cs @@ -26,7 +26,9 @@ namespace WinUINotes /// public partial class App : Application { - private Window? _window; + private static Window? _window; + + public static Window m_window => _window; /// /// Initializes the singleton application object. This is the first line of authored code diff --git a/WinUINotes/Common/FileHelper.cs b/WinUINotes/Common/FileHelper.cs new file mode 100644 index 0000000..317a087 --- /dev/null +++ b/WinUINotes/Common/FileHelper.cs @@ -0,0 +1,73 @@ +using System; +using System.IO; +using System.Security.Cryptography; +using System.Threading; +using System.Threading.Tasks; + +namespace WinUINotes.Common +{ + public static class FileHelper + { + // 最佳缓冲区大小经过测试得出(根据硬件调整) + private const int OptimalBufferSize = 1 * 1024 * 1024; // 1MB缓冲区 + public static string CalculateFileHash(string filePath) + { + using (var md5 = MD5.Create()) + using (var stream = File.OpenRead(filePath)) + { + var hash = md5.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + } + + //public static async Task CalculateFileHashAsync(string filePath) + //{ + // using (var md5 = MD5.Create()) + // using (var stream = File.OpenRead(filePath)) + // { + // var hash = await md5.ComputeHashAsync(stream); + // return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + // } + //} + public static async Task CalculateFileHashAsync(string filePath, int bufferSize = 81920 /* 默认缓冲区大小 */, CancellationToken cancellationToken = default) + { + using (var md5 = MD5.Create()) + { + // 使用带有异步选项和优化缓冲区大小的FileStream + using (var stream = new FileStream( + filePath, + FileMode.Open, + FileAccess.Read, + FileShare.Read, + bufferSize: bufferSize, + options: FileOptions.SequentialScan | FileOptions.Asynchronous)) + { + var hash = await md5.ComputeHashAsync(stream, cancellationToken) + .ConfigureAwait(false); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + } + } + + // 快速文件标识(前1MB哈希 + 文件大小) + public static async Task GetQuickFileIdentity(string filePath) + { + const int sampleSize = 1 * 1024 * 1024; // 1MB + var fileInfo = new FileInfo(filePath); + long fileSize = fileInfo.Length; + + using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + byte[] buffer = new byte[Math.Min(sampleSize, fileSize)]; + int bytesRead = await fs.ReadAsync(buffer, 0, buffer.Length); + + using (var sha = SHA256.Create()) + { + byte[] hashBytes = sha.ComputeHash(buffer); + return $"{fileSize}-{BitConverter.ToString(hashBytes).Replace("-", "")}"; + } + } + } + + } +} \ No newline at end of file diff --git a/WinUINotes/FileUploadPage.xaml b/WinUINotes/FileUploadPage.xaml new file mode 100644 index 0000000..80f9851 --- /dev/null +++ b/WinUINotes/FileUploadPage.xaml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + +