Пример #1
0
    /// <summary>
    /// Exports all feeds listed in a <see cref="Selections"/> document along with any OpenPGP public key files required for validation.
    /// </summary>
    /// <param name="feedCache">Used to get local feed files.</param>
    /// <param name="openPgp">Used to get export keys feeds were signed with.</param>
    /// <exception cref="UnauthorizedAccessException">The file could not be read or written.</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the directory is not permitted.</exception>
    /// <exception cref="IOException">A feed or GnuPG could not be read from the cache.</exception>
    public void ExportFeeds(IFeedCache feedCache, IOpenPgp openPgp)
    {
        #region Sanity checks
        if (feedCache == null)
        {
            throw new ArgumentNullException(nameof(feedCache));
        }
        if (openPgp == null)
        {
            throw new ArgumentNullException(nameof(openPgp));
        }
        #endregion

        string contentDir = Path.Combine(_destination, "content");
        Directory.CreateDirectory(contentDir);

        var feedUris = _selections.Implementations
                       .SelectMany(x => new [] { x.InterfaceUri, x.FromFeed })
                       .WhereNotNull().Distinct().ToList();

        foreach (var feedUri in feedUris)
        {
            string filePath = Path.Combine(contentDir, feedUri.PrettyEscape());
            if (!filePath.EndsWith(".xml"))
            {
                filePath += ".xml";
            }

            string?path = feedCache.GetPath(feedUri);
            if (path != null)
            {
                Log.Info("Exporting feed " + feedUri.ToStringRfc());
                File.Copy(path, filePath, overwrite: true);
            }
        }

        foreach (var signature in feedUris.SelectMany(feedCache.GetSignatures).OfType <ValidSignature>().Distinct())
        {
            Log.Info("Exporting GPG key " + signature.FormatKeyID());
            openPgp.DeployPublicKey(signature, contentDir);
        }
    }