44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using System.Text.Json;
|
|
|
|
namespace VideoDownloader.Server;
|
|
|
|
/// <summary>
|
|
/// 이어보기 위치 저장(U3). 라이브러리 폴더의 positions.json 에 영속 —
|
|
/// 서버 재시작·PC 재부팅 후에도 폰과 같은 위치에서 이어보기.
|
|
/// </summary>
|
|
public sealed class PositionStore
|
|
{
|
|
private const string FileName = "positions.json";
|
|
private readonly object _gate = new();
|
|
private readonly string _path;
|
|
|
|
private Dictionary<string, double> _positions;
|
|
|
|
public PositionStore(string libraryFolder)
|
|
{
|
|
_path = Path.Combine(libraryFolder, FileName);
|
|
try
|
|
{
|
|
if (File.Exists(_path))
|
|
_positions = JsonSerializer.Deserialize<Dictionary<string, double>>(File.ReadAllText(_path)) ?? new();
|
|
}
|
|
catch { /* 손상 파일 → 빈 상태로 시작 */ }
|
|
_positions ??= new Dictionary<string, double>();
|
|
}
|
|
|
|
public double Get(string id)
|
|
{
|
|
lock (_gate) return _positions.GetValueOrDefault(id, 0);
|
|
}
|
|
|
|
public void Set(string id, double positionSec)
|
|
{
|
|
if (string.IsNullOrEmpty(id) || positionSec < 0 || double.IsNaN(positionSec) || double.IsInfinity(positionSec))
|
|
return;
|
|
lock (_gate)
|
|
{
|
|
_positions[id] = positionSec;
|
|
try { File.WriteAllText(_path, JsonSerializer.Serialize(_positions)); } catch { /* 디스크 오류는 무시 */ }
|
|
}
|
|
}
|
|
}
|