Skip to content

Commit ec8d408

Browse files
authored
Fix cache management with preview (stable) option (#61)
1 parent b7d8ae1 commit ec8d408

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

nanoFirmwareFlasher/FirmwarePackage.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,29 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
125125
return ExitCodes.E9006;
126126
}
127127

128-
var fwFiles = Directory.EnumerateFiles(LocationPath, $"{_targetName}-*.zip").OrderByDescending(f => f).ToList();
128+
List<FileInfo> fwFiles = new();
129+
130+
if (_preview)
131+
{
132+
fwFiles = Directory.GetFiles(LocationPath)
133+
.Select(f => new FileInfo(f))
134+
.Where(f => f.Name.Contains("-preview.") && f.Extension == ".zip")
135+
.OrderByDescending(f => f.Name)
136+
.ToList();
137+
}
138+
else
139+
{
140+
fwFiles = Directory.GetFiles(LocationPath)
141+
.Select(f => new FileInfo(f))
142+
.Where(f => !f.Name.Contains("-preview.") && f.Extension == ".zip")
143+
.OrderByDescending(f => f.Name)
144+
.ToList();
145+
}
129146

130147
if (fwFiles.Any())
131148
{
132149
// get file creation date (from the 1st one)
133-
if ((DateTime.UtcNow - File.GetLastWriteTimeUtc(fwFiles.First())).TotalHours < 4)
150+
if ((DateTime.UtcNow - File.GetLastWriteTimeUtc(fwFiles.First().FullName)).TotalHours < 4)
134151
{
135152
// fw package has less than 4 hours
136153
// skip download
@@ -301,12 +318,11 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
301318
{
302319
// couldn't download the fw file
303320
// check if there is one available
304-
fwFiles = Directory.EnumerateFiles(LocationPath, $"{_targetName}-*.zip").OrderByDescending(f => f).ToList();
305321

306322
if (fwFiles.Any())
307323
{
308324
// take the 1st one
309-
fwFileName = fwFiles.First();
325+
fwFileName = fwFiles.First().FullName;
310326

311327
// get the version form the file name
312328
var pattern = @"(\d+\.\d+\.\d+)(\.\d+|-.+)(?=\.zip)";
@@ -357,15 +373,12 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
357373
Console.ForegroundColor = ConsoleColor.White;
358374
}
359375

360-
// be nice to the user and delete any fw packages other than the last one
361-
var allFwFiles = Directory.EnumerateFiles(LocationPath, "*.zip").OrderByDescending(f => f).ToList();
362-
if (allFwFiles.Count > 1)
363-
{
364-
foreach (var file in allFwFiles.Skip(1))
365-
{
366-
File.Delete(file);
367-
}
368-
}
376+
// be nice to the user and delete any fw packages older than a month
377+
Directory.GetFiles(LocationPath)
378+
.Select(f => new FileInfo(f))
379+
.Where(f => f.Extension == ".zip" && f.LastWriteTime < DateTime.Now.AddMonths(-1))
380+
.ToList()
381+
.ForEach(f => f.Delete());
369382

370383
if (Verbosity >= VerbosityLevel.Normal)
371384
{

0 commit comments

Comments
 (0)