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

@ -115,6 +115,58 @@ public sealed class ApiClient
}
public string StreamUrl(LibraryItem item) => $"{BaseUrl}/stream/{item.Id}?t={Token}";
public string FileUrl(LibraryItem item) => $"{BaseUrl}/file/{item.Id}?t={Token}";
/// <summary>이어보기 위치 조회(U3). 실패/미저장 시 0.</summary>
public async Task<double> GetPositionAsync(string id)
{
var dto = await GetAsync<PositionDto>($"/api/position/{id}");
return dto?.PositionSec ?? 0;
}
/// <summary>이어보기 위치 저장(U3).</summary>
public Task<bool> PostPositionAsync(string id, double positionSec) =>
PostJsonAsync($"/api/position/{id}", new PositionDto(positionSec));
/// <summary>라이브러리 항목을 폰에 저장(U4). 진행률 0~1, 취소 지원. 임시 .part 로 받아 완료 시 rename.</summary>
public async Task DownloadFileAsync(LibraryItem item, string destPath, IProgress<double>? progress, CancellationToken ct)
{
Directory.CreateDirectory(Path.GetDirectoryName(destPath)!);
var tmp = destPath + ".part";
try
{
using var resp = await _http.GetAsync(FileUrl(item), HttpCompletionOption.ResponseHeadersRead, ct);
resp.EnsureSuccessStatusCode();
var total = resp.Content.Headers.ContentLength ?? item.SizeBytes;
await using var src = await resp.Content.ReadAsStreamAsync(ct);
await using var dst = File.Create(tmp);
var buf = new byte[64 * 1024];
long read = 0;
int n;
while ((n = await src.ReadAsync(buf, ct)) > 0)
{
await dst.WriteAsync(buf.AsMemory(0, n), ct);
read += n;
if (total > 0) progress?.Report((double)read / total);
}
File.Move(tmp, destPath, overwrite: true);
progress?.Report(1);
}
finally
{
try { if (File.Exists(tmp)) File.Delete(tmp); } catch { }
}
}
private async Task<bool> PostJsonAsync(string path, object body)
{
try
{
var r = await _http.PostAsJsonAsync($"{BaseUrl}{path}", body);
return r.IsSuccessStatusCode;
}
catch { return false; }
}
private Task<bool> PostAsync(string path)
{
@ -137,3 +189,5 @@ public sealed class ApiClient
public sealed record QueueItem(string Id, string Title, string Url, string Status, double Percent);
public sealed record LibraryItem(string Id, string FileName, long SizeBytes, DateTime Modified);
public sealed record PositionDto(double PositionSec);