62 lines
2.0 KiB
C#
62 lines
2.0 KiB
C#
using System.IO;
|
|
|
|
namespace AxCopilot.Services;
|
|
|
|
/// <summary>
|
|
/// 앱 내부 임시 파일 관리.
|
|
/// %APPDATA%\AxCopilot\temp\ 에 임시 파일을 생성하고,
|
|
/// 1일 이상 지난 파일을 자동으로 정리합니다.
|
|
/// </summary>
|
|
public static class TempFileService
|
|
{
|
|
private static readonly string TempDir = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
"AxCopilot", "temp");
|
|
|
|
/// <summary>
|
|
/// 임시 파일 경로를 생성하고 반환합니다.
|
|
/// 호출 시 1일 이상 지난 기존 임시 파일을 자동 정리합니다.
|
|
/// </summary>
|
|
/// <param name="prefix">파일명 접두사 (예: "clip_image")</param>
|
|
/// <param name="extension">확장자 (예: ".png", ".txt")</param>
|
|
public static string CreateTempFile(string prefix, string extension)
|
|
{
|
|
Directory.CreateDirectory(TempDir);
|
|
CleanupOldFiles();
|
|
|
|
var fileName = $"{prefix}_{DateTime.Now:yyyyMMdd_HHmmss_fff}{extension}";
|
|
return Path.Combine(TempDir, fileName);
|
|
}
|
|
|
|
/// <summary>1일 이상 지난 임시 파일을 삭제합니다. 열려 있는 파일은 건너뜁니다.</summary>
|
|
private static void CleanupOldFiles()
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(TempDir)) return;
|
|
|
|
var cutoff = DateTime.Now.AddDays(-1);
|
|
foreach (var file in Directory.EnumerateFiles(TempDir))
|
|
{
|
|
try
|
|
{
|
|
if (File.GetCreationTime(file) < cutoff)
|
|
File.Delete(file);
|
|
}
|
|
catch (IOException)
|
|
{
|
|
// 파일이 열려 있음 — 건너뛰기
|
|
}
|
|
catch (UnauthorizedAccessException)
|
|
{
|
|
// 권한 없음 — 건너뛰기
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogService.Debug($"임시 파일 정리 중 오류: {ex.Message}");
|
|
}
|
|
}
|
|
}
|