using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace Velopack.Sources
{
///
/// 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.
///
public class SimpleFileSource : IUpdateSource
{
/// The local directory containing packages to update to.
public virtual DirectoryInfo BaseDirectory { get; }
///
public SimpleFileSource(DirectoryInfo baseDirectory)
{
BaseDirectory = baseDirectory;
}
///
public Task GetReleaseFeed(ILogger logger, string channel, Guid? stagingId = null, VelopackAsset latestLocalRelease = null)
{
if (!BaseDirectory.Exists) {
logger.Error($"The local update directory '{BaseDirectory.FullName}' does not exist.");
return Task.FromResult(new VelopackAssetFeed());
}
var list = new List();
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}'.");
}
}
return Task.FromResult(new VelopackAssetFeed { Assets = list.ToArray() });
}
///
public Task DownloadReleaseEntry(ILogger logger, VelopackAsset releaseEntry, string localFile, Action progress)
{
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;
}
}
}