Files
velopack/src/Velopack/Sources/SimpleFileSource.cs
T

62 lines
2.3 KiB
C#
Raw Normal View History

using System;
2024-01-16 11:55:18 +00:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-12-14 14:16:06 +00:00
using Microsoft.Extensions.Logging;
2023-12-31 11:09:44 +00:00
namespace Velopack.Sources
{
/// <summary>
/// Retrieves available updates from a local or network-attached disk. The directory
/// must contain one or more valid packages, as well as a 'RELEASES' index file.
/// </summary>
2024-01-15 11:30:43 +00:00
public class SimpleFileSource : IUpdateSource
{
/// <summary> The local directory containing packages to update to. </summary>
public virtual DirectoryInfo BaseDirectory { get; }
/// <inheritdoc cref="SimpleFileSource" />
2024-01-15 11:30:43 +00:00
public SimpleFileSource(DirectoryInfo baseDirectory)
{
BaseDirectory = baseDirectory;
}
/// <inheritdoc />
2024-01-15 17:04:03 +00:00
public Task<VelopackAssetFeed> GetReleaseFeed(ILogger logger, string channel, Guid? stagingId = null, VelopackAsset latestLocalRelease = null)
{
2024-01-15 17:04:03 +00:00
if (!BaseDirectory.Exists) {
logger.Error($"The local update directory '{BaseDirectory.FullName}' does not exist.");
return Task.FromResult(new VelopackAssetFeed());
}
2024-01-15 17:04:03 +00:00
2024-01-16 11:55:18 +00:00
var list = new List<VelopackAsset>();
foreach (var pkg in Directory.EnumerateFiles(BaseDirectory.FullName, "*.nupkg")) {
try {
var asset = VelopackAsset.FromNupkg(pkg);
if (asset?.Version != null) {
list.Add(asset);
}
} catch (Exception ex) {
logger.Warn(ex, $"Error while reading local package '{pkg}'.");
}
}
2024-01-15 17:04:03 +00:00
2024-01-16 11:55:18 +00:00
return Task.FromResult(new VelopackAssetFeed { Assets = list.ToArray() });
}
/// <inheritdoc />
2024-01-15 17:04:03 +00:00
public Task DownloadReleaseEntry(ILogger logger, VelopackAsset releaseEntry, string localFile, Action<int> progress)
{
2024-01-15 17:04:03 +00:00
var releasePath = Path.Combine(BaseDirectory.FullName, releaseEntry.FileName);
if (!File.Exists(releasePath))
throw new Exception($"The file '{releasePath}' does not exist. The packages directory is invalid.");
File.Copy(releasePath, localFile, true);
progress?.Invoke(100);
return Task.CompletedTask;
}
}
}