M2-U3+U4 완성: 이어보기 위치 동기화(POST/GET /api/position + PositionStore JSON 영속, 플레이어 5초 주기·종료 저장·재개 시크) + 폰 오프라인 저장(ApiClient.DownloadFileAsync .part 스트리밍, OfflineStore manifest, 라이브러리 💾 버튼, 저장 탭 오프라인 재생/삭제). 서버 RED→GREEN E2E + 에뮬레이터 E2E(위치 13.3→35.2초 이어재생, 서버 정지 후 오프라인 재생 프레임 실증). 104 tests GREEN

This commit is contained in:
Yun Chan 2026-08-15 17:40:28 +09:00
parent 17f3e41cd2
commit 27c91c04dd
9 changed files with 356 additions and 8 deletions

View file

@ -0,0 +1,44 @@
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 { /* 디스크 오류는 무시 */ }
}
}
}

View file

@ -113,6 +113,17 @@ public sealed class ServerHost : IAsyncDisposable
return file == null ? Results.NotFound() : Results.File(file.Value.FullPath, "application/octet-stream", file.Value.FileName);
});
// ── 이어보기 위치 동기화 (U3) ──
var positions = new PositionStore(options.LibraryFolder);
app.MapGet("/api/position/{id}", (string id) => Results.Json(new PositionDto(positions.Get(id))));
app.MapPost("/api/position/{id}", async (string id, HttpContext ctx) =>
{
var body = await ctx.Request.ReadFromJsonAsync<PositionDto>();
if (body == null) return Results.BadRequest();
positions.Set(id, body.PositionSec);
return Results.Ok();
});
// ── 웹 컴패니언 (제로 설치 클라이언트) ──
app.MapGet("/", () => Results.Text(WebCompanion.Html, "text/html; charset=utf-8"));

View file

@ -6,6 +6,9 @@ public sealed record QueueItemDto(string Id, string Title, string Url, string St
/// <summary>라이브러리(저장 폴더) 미디어 항목.</summary>
public sealed record LibraryItemDto(string Id, string FileName, long SizeBytes, DateTime Modified);
/// <summary>이어보기 위치(U3 동기화). 초 단위.</summary>
public sealed record PositionDto(double PositionSec);
/// <summary>서버 구성.</summary>
public sealed class ServerOptions
{