107 lines
3.9 KiB
C#
107 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace YuncangTrace.LocalService.Services;
|
|
|
|
/// <summary>
|
|
/// 录像存储目录
|
|
///
|
|
/// 约定:
|
|
/// - 默认根目录 <c>~/packVideo</c>(用户目录下,权限够、不会被清理工具删);
|
|
/// - 按 **扫码日期** 分目录:<c>{根}/2026/03/11/</c>,成片直接放在里面;
|
|
/// - 用户可以在「工具 → 设置录像存储路径」改根目录;切换时**把已有文件一起搬过去**。
|
|
/// </summary>
|
|
public static class VideoStorage
|
|
{
|
|
/// <summary>默认根目录:`%USERPROFILE%\packVideo`</summary>
|
|
public static string DefaultRoot { get; } = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "packVideo");
|
|
|
|
/// <summary>某个时间点对应的目录:`{根}/yyyy/MM/dd`</summary>
|
|
public static string FolderFor(string root, DateTime time)
|
|
=> Path.Combine(root, time.ToString("yyyy"), time.ToString("MM"), time.ToString("dd"));
|
|
|
|
/// <summary>
|
|
/// 把 <paramref name="from"/> 下的所有文件搬到 <paramref name="to"/>(保留相对目录结构)
|
|
///
|
|
/// 切换存储路径时用:只搬文件,搬完把**空的**旧目录删掉(旧目录里如果还有别的东西就留着,
|
|
/// 防止误删用户自己放进去的文件)。
|
|
/// </summary>
|
|
/// <returns>移动的文件数 / 出错信息(出错不抛,界面给提示)</returns>
|
|
public static (int Moved, string? Error) MoveAll(string from, string to, IProgress<string>? progress = null)
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(from)) return (0, null);
|
|
|
|
var source = Path.GetFullPath(from).TrimEnd(Path.DirectorySeparatorChar);
|
|
var target = Path.GetFullPath(to).TrimEnd(Path.DirectorySeparatorChar);
|
|
if (string.Equals(source, target, StringComparison.OrdinalIgnoreCase)) return (0, null);
|
|
|
|
Directory.CreateDirectory(target);
|
|
|
|
var moved = 0;
|
|
foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories))
|
|
{
|
|
var relative = Path.GetRelativePath(source, file);
|
|
var destination = Path.Combine(target, relative);
|
|
|
|
var folder = Path.GetDirectoryName(destination);
|
|
if (!string.IsNullOrEmpty(folder)) Directory.CreateDirectory(folder);
|
|
|
|
if (File.Exists(destination)) File.Delete(destination);
|
|
File.Move(file, destination);
|
|
|
|
moved++;
|
|
if (moved % 20 == 0) progress?.Report($"已移动 {moved} 个文件…");
|
|
}
|
|
|
|
DeleteEmptyDirectories(source);
|
|
|
|
// 旧根目录空了就删掉(还有东西就保留)
|
|
try
|
|
{
|
|
if (Directory.Exists(source) && !Directory.EnumerateFileSystemEntries(source).Any())
|
|
{
|
|
Directory.Delete(source, recursive: false);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// 删不掉也无所谓
|
|
}
|
|
|
|
return (moved, null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (0, ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>自底向上删空目录(不动有文件的目录)</summary>
|
|
private static void DeleteEmptyDirectories(string root)
|
|
{
|
|
var directories = Directory.EnumerateDirectories(root, "*", SearchOption.AllDirectories)
|
|
.OrderByDescending(item => item.Length)
|
|
.ToList();
|
|
|
|
foreach (var directory in directories)
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.EnumerateFileSystemEntries(directory).Any())
|
|
{
|
|
Directory.Delete(directory, recursive: false);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// 删不掉跳过
|
|
}
|
|
}
|
|
}
|
|
}
|