コード例 #1
0
        public async Task TestWriteOpmlFileAsync()
        {
            var catInfos = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("Work 996", "work-996")
            };
            var siteRootUrl = "https://996.icu";

            var info = new OpmlDoc
            {
                SiteTitle               = $"Work 996 - OPML",
                CategoryInfo            = catInfos,
                HtmlUrl                 = $"{siteRootUrl}/post",
                XmlUrl                  = $"{siteRootUrl}/rss",
                CategoryXmlUrlTemplate  = $"{siteRootUrl}/rss/category/[catTitle]",
                CategoryHtmlUrlTemplate = $"{siteRootUrl}/category/list/[catTitle]"
            };

            var path = Path.Join(Path.GetTempPath(), $"Moonglade-UT-OPML-{Guid.NewGuid()}.xml");

            var writer = new FileSystemOpmlWriter();
            await writer.WriteOpmlFileAsync(path, info);

            Assert.IsTrue(File.Exists(path));
        }
コード例 #2
0
ファイル: OpmlController.cs プロジェクト: yuyu2you/Moonglade
        public async Task <IActionResult> Index()
        {
            var feedDirectoryPath = $@"{AppDomain.CurrentDomain.GetData(Constants.DataDirectory)}\feed";

            if (!Directory.Exists(feedDirectoryPath))
            {
                Directory.CreateDirectory(feedDirectoryPath);
            }

            var opmlDataFile = $@"{AppDomain.CurrentDomain.GetData(Constants.DataDirectory)}\{Constants.OpmlFileName}";

            if (!System.IO.File.Exists(opmlDataFile))
            {
                Logger.LogInformation($"OPML file not found, writing new file on {opmlDataFile}");

                var catResponse = await _categoryService.GetAllCategoriesAsync();

                if (!catResponse.IsSuccess)
                {
                    return(ServerError("Unsuccessful response from _categoryService.GetAllCategoriesAsync()."));
                }

                var catInfos = catResponse.Item.Select(c => new OpmlCategoryInfo
                {
                    DisplayName = c.DisplayName,
                    Title       = c.Title
                });

                var oi = new OpmlInfo
                {
                    SiteTitle               = $"{_blogConfig.SiteTitle} - OPML",
                    CategoryInfo            = catInfos,
                    HtmlUrl                 = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/post",
                    XmlUrl                  = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/rss",
                    CategoryXmlUrlTemplate  = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/rss/category/[catTitle]",
                    CategoryHtmlUrlTemplate = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/category/list/[catTitle]"
                };

                await FileSystemOpmlWriter.WriteOpmlFileAsync(
                    $@"{AppDomain.CurrentDomain.GetData(Constants.DataDirectory)}\{Constants.OpmlFileName}", oi);

                Logger.LogInformation("OPML file write completed.");

                if (!System.IO.File.Exists(opmlDataFile))
                {
                    Logger.LogInformation("OPML file still not found, something just went very very wrong...");
                    return(NotFound());
                }
            }

            string opmlContent = await System.IO.File.ReadAllTextAsync(opmlDataFile, Encoding.UTF8);

            if (opmlContent.Length > 0)
            {
                return(Content(opmlContent, "text/xml"));
            }

            return(NotFound());
        }