using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using FileUploader.Models;
using Hopetry.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Win32;
using ModernWpf.Controls;
namespace Hopetry
{
///
/// Interaction logic for MainWindow.xaml
///
/* public partial class MainWindow : Window
{
private AppDbContext _db = new AppDbContext();
private MinioService _minioService;
public ObservableCollection UploadQueue { get; } = new ObservableCollection();
public bool ShutdownAfterUpload { get; set; }
public MainWindow()
{
InitializeComponent();
}
}*/
public partial class MainWindow : Window
{
private AppDbContext _db = new AppDbContext();
private MinioService _minioService;
public ObservableCollection UploadQueue { get; } = new ObservableCollection();
public bool ShutdownAfterUpload { get; set; }
Dictionary _pages;
public MainWindow()
{
InitializeComponent();
/* _pages = new Dictionary
{
{ "home", new Uri("Views/HomePage.xaml", UriKind.Relative) },
{ "files", new Uri("Views/FilesPage.xaml", UriKind.Relative) },
{ "share", new Uri("Views/SharePage.xaml", UriKind.Relative) },
{ "trash", new Uri("Views/TrashPage.xaml", UriKind.Relative) }
};
MainFrame.Navigate(_pages["home"]);*/
DataContext = this;
// 初始化导航完成
_db.Database.Migrate();
Console.WriteLine(Directory.GetCurrentDirectory());
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("global.json")
.Build();
_minioService = new MinioService(config);
}
private void NavigationView_SelectionChanged(object sender,
NavigationViewSelectionChangedEventArgs e)
{
if (e.SelectedItemContainer?.Tag is string tag && _pages.TryGetValue(tag, out Uri uri))
{
MainFrame.Navigate(uri);
}
}
private async void AddUser_Click(object sender, RoutedEventArgs e)
{
/* var dialog = new UserDialog();
if (dialog.ShowDialog() == true)
{
_db.Users.Add(new User { Username = dialog.Username });
await _db.SaveChangesAsync();
//LogTextBox.AppendText($"用户 {dialog.Username} 添加成功\n");
}*/
}
private void SelectFile_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog();
if (dialog.ShowDialog() == true)
{
UploadQueue.Add(new FileRecord
{
FileName = Path.GetFileName(dialog.FileName),
LocalPath = dialog.FileName,
UserId = 1
});
}
}
private async void StartUpload_Click(object sender, RoutedEventArgs e)
{
foreach (var file in UploadQueue)
{
try
{
await _minioService.UploadFileAsync(file);
file.MinioPath = $"uploads/{file.FileName}";
file.UploadTime = DateTime.Now;
_db.FileRecords.Add(file);
await _db.SaveChangesAsync();
//LogTextBox.AppendText($"文件 {file.FileName} 上传成功\n");
}
catch (Exception ex)
{
//LogTextBox.AppendText($"上传失败: {ex.Message}\n");
}
}
if (ShutdownAfterUpload)
Process.Start("shutdown", "/s /t 0");
}
}
}