2022-03-07 19:25:07 +00:00
|
|
|
using System;
|
2024-01-16 11:55:18 +00:00
|
|
|
using System.Collections.Generic;
|
2022-03-07 19:25:07 +00:00
|
|
|
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;
|
2022-03-07 19:25:07 +00:00
|
|
|
|
2023-12-31 11:09:44 +00:00
|
|
|
namespace Velopack.Sources
|
2022-03-07 19:25:07 +00:00
|
|
|
{
|
|
|
|
|
/// <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
|
2022-03-07 19:25:07 +00:00
|
|
|
{
|
|
|
|
|
/// <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)
|
2022-03-07 19:25:07 +00:00
|
|
|
{
|
|
|
|
|
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)
|
2022-03-07 19:25:07 +00:00
|
|
|
{
|
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());
|
2022-03-07 19:25:07 +00:00
|
|
|
}
|
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() });
|
2022-03-07 19:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-01-15 17:04:03 +00:00
|
|
|
public Task DownloadReleaseEntry(ILogger logger, VelopackAsset releaseEntry, string localFile, Action<int> progress)
|
2022-03-07 19:25:07 +00:00
|
|
|
{
|
2024-01-15 17:04:03 +00:00
|
|
|
var releasePath = Path.Combine(BaseDirectory.FullName, releaseEntry.FileName);
|
2022-03-07 19:25:07 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|