128 lines
4.8 KiB
C#
128 lines
4.8 KiB
C#
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Text.RegularExpressions;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using DBObj;
|
||
|
|
||
|
namespace DataCheck
|
||
|
{
|
||
|
public class SongChecker
|
||
|
{
|
||
|
private static List<SongData> playingSongList= new();
|
||
|
//public static List<PlayState> playStates;
|
||
|
private static List<SongData> publicSongList = new();
|
||
|
private SongData welcome = new SongData("0", "歡迎光臨", @"D:\video\welcome.mpg", 1 ,true);
|
||
|
private SongData close = new SongData("0", "結束播放", @"D:\video\CLOSE.MPG", 1 ,true);
|
||
|
|
||
|
public SongChecker()
|
||
|
{
|
||
|
string serverPath = Utils.Env.GetPath("video", "");
|
||
|
string localPath = @"D:\video";
|
||
|
// 加入你要同步的資料夾
|
||
|
SyncFolder(serverPath, localPath, "video", new[] { ".mpg" });
|
||
|
}
|
||
|
public static bool AddSongToPlayList(SongData song)
|
||
|
{
|
||
|
playingSongList.Add(song);
|
||
|
return true;
|
||
|
}
|
||
|
public static bool isPlayingUserSong()
|
||
|
{
|
||
|
return playingSongList.Count == 0;
|
||
|
}
|
||
|
public static SongData GetCurrentSong()
|
||
|
{
|
||
|
SongData song = null;
|
||
|
int count = playingSongList.Count;
|
||
|
if (count == 0)
|
||
|
{
|
||
|
song = publicSongList[0];
|
||
|
publicSongList.RemoveAt(0);
|
||
|
publicSongList.Add(song);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
song = playingSongList[0];
|
||
|
playingSongList.RemoveAt(0);
|
||
|
}
|
||
|
return song;
|
||
|
}
|
||
|
public static SongData GetNextSong()
|
||
|
{
|
||
|
return playingSongList.Count > 1 ? playingSongList[1] : null;
|
||
|
}
|
||
|
|
||
|
private void SyncFolder(string serverPath, string localPath, string label, string[] extensions)
|
||
|
{
|
||
|
|
||
|
if (!Directory.Exists(localPath)) Directory.CreateDirectory(localPath);
|
||
|
if (!Directory.Exists(serverPath))
|
||
|
{
|
||
|
Console.WriteLine($"找不到伺服器資料夾:{serverPath}");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var serverFiles = Directory.GetFiles(serverPath)
|
||
|
.Where(f => extensions.Contains(Path.GetExtension(f), StringComparer.OrdinalIgnoreCase))
|
||
|
.Select(f => new FileInfo(f))
|
||
|
.ToDictionary(f => f.Name, f => f);
|
||
|
|
||
|
var localFiles = Directory.GetFiles(localPath)
|
||
|
.Where(f => extensions.Contains(Path.GetExtension(f), StringComparer.OrdinalIgnoreCase))
|
||
|
.Select(f => new FileInfo(f))
|
||
|
.ToDictionary(f => f.Name, f => f);
|
||
|
|
||
|
// 1. 複製或更新檔案
|
||
|
foreach (var serverFile in serverFiles)
|
||
|
{
|
||
|
string dest = Path.Combine(localPath, serverFile.Key);
|
||
|
bool needsCopy = !localFiles.ContainsKey(serverFile.Key) ||
|
||
|
serverFile.Value.LastWriteTime > localFiles[serverFile.Key].LastWriteTime;
|
||
|
if (needsCopy)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
File.Copy(serverFile.Value.FullName, dest, true);
|
||
|
Console.WriteLine($"✅ 更新 {label}: {serverFile.Key}");
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Console.WriteLine($"❌ 複製 {label} 失敗 {serverFile.Key}: {ex.Message}");
|
||
|
continue; // 如果複製失敗就不加入 SongData
|
||
|
}
|
||
|
}
|
||
|
string fileName = Path.GetFileNameWithoutExtension(serverFile.Key);
|
||
|
Match match = Regex.Match(fileName, @"^(?<songNumber>\d+)-.*?-(?<songName>[^-]+)-");
|
||
|
if (match.Success)
|
||
|
{
|
||
|
publicSongList.Add(new SongData(
|
||
|
match.Groups["songNumber"].Value, // songNumber
|
||
|
match.Groups["songName"].Value, // song
|
||
|
Path.Combine(localPath, serverFile.Key), // songFilePathHost1
|
||
|
1, // priority
|
||
|
true
|
||
|
));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 2. 刪除多餘的本地檔案
|
||
|
foreach (var localFile in localFiles)
|
||
|
{
|
||
|
if (!serverFiles.ContainsKey(localFile.Key))
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
File.Delete(localFile.Value.FullName);
|
||
|
Console.WriteLine($"刪除多餘{label}: {localFile.Key}");
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Console.WriteLine($"刪除{label}失敗 {localFile.Key}: {ex.Message}");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|