From ecbacd26c43b8eb709831baacde59050dbe5a941 Mon Sep 17 00:00:00 2001 From: yangrunkang Date: Sun, 16 Jan 2022 23:47:25 +0800 Subject: [PATCH] =?UTF-8?q?Ref:=20=E9=87=8D=E6=9E=84=E7=AB=99=E7=82=B9?= =?UTF-8?q?=E5=9C=B0=E5=9B=BE=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F,?= =?UTF-8?q?=E4=B8=8D=E4=BD=BF=E7=94=A8xml=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/upupor/service/common/CcConstant.java | 6 ++ .../scheduled/GenerateSiteMapScheduled.java | 68 +----------------- .../com/upupor/web/outer/OuterController.java | 72 +++++++++++++++++++ .../static/upupor-google-sitemap.xml | 2 - .../resources/templates/layout/footer.html | 2 +- 5 files changed, 81 insertions(+), 69 deletions(-) create mode 100644 upupor-web/src/main/java/com/upupor/web/outer/OuterController.java delete mode 100644 upupor-web/src/main/resources/static/upupor-google-sitemap.xml diff --git a/upupor-service/src/main/java/com/upupor/service/common/CcConstant.java b/upupor-service/src/main/java/com/upupor/service/common/CcConstant.java index 9cd7485f..f2c1a37b 100644 --- a/upupor-service/src/main/java/com/upupor/service/common/CcConstant.java +++ b/upupor-service/src/main/java/com/upupor/service/common/CcConstant.java @@ -368,6 +368,12 @@ public static final class CvCache { */ public static final String CREATE_CONTENT_TIME_OUT = "create_content_time_out"; + /** + * SiteMap + */ + public static final String SITE_MAP = "siteMap"; + + public static final String createContentIntervalkey(String userId){ return CcConstant.CvCache.CREATE_CONTENT_TIME_OUT + userId; diff --git a/upupor-service/src/main/java/com/upupor/service/scheduled/GenerateSiteMapScheduled.java b/upupor-service/src/main/java/com/upupor/service/scheduled/GenerateSiteMapScheduled.java index 0305d599..f628484e 100644 --- a/upupor-service/src/main/java/com/upupor/service/scheduled/GenerateSiteMapScheduled.java +++ b/upupor-service/src/main/java/com/upupor/service/scheduled/GenerateSiteMapScheduled.java @@ -46,16 +46,12 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; -import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; +import static com.upupor.service.common.CcConstant.CvCache.SITE_MAP; import static com.upupor.service.common.CcConstant.CvCache.TAG_COUNT; @@ -123,11 +119,7 @@ public void googleSitemap() { return; } - // 写到文件 upupor-google-sitemap.xml - writeToFile(s); - // 备份 - bakSeoData(s); - + RedisUtil.set(SITE_MAP, s); } private void generateRadio(List googleSeoDtoList, SimpleDateFormat sdf) { @@ -315,62 +307,6 @@ private void generatePageSiteMap(List googleSeoDtoList, SimpleDate } - private void writeToFile(String s) { - BufferedWriter out = null; - try { - // 这个文件是写到了打包后的classes目录下的文件,开发环境看不到 - File file = ResourceUtils.getFile("classpath:static/upupor-google-sitemap.xml"); - if (!file.exists()) { - boolean newFile = file.createNewFile(); - if (!newFile) { - log.error("upupor-google-sitemap.xml文件创建失败"); - } - } - out = new BufferedWriter(new FileWriter(file)); - out.write(s); - out.flush(); - out.close(); - log.info("Google站点地图生成完毕"); - } catch (Exception e) { - log.error("写入站点地图失败,{}", e.getMessage()); - } finally { - try { - if (Objects.nonNull(out)) { - out.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - private void bakSeoData(String s) { - try { - // 生成Google站点地图 - Seo google = seoService.getBySeoId("google"); - if (Objects.isNull(google)) { - Seo seo = new Seo(); - seo.setSeoId("google"); - seo.setCreateTime(CcDateUtil.getCurrentTime()); - seo.setSeoStatus(SeoStatus.NORMAL); - seo.setSysUpdateTime(new Date()); - seo.setSeoContent(s); - Boolean addSeo = seoService.addSeo(seo); - if (!addSeo) { - log.error("添加Google Seo信息失败"); - } - } else { - google.setSeoContent(s); - Boolean update = seoService.updateSeo(google); - if (!update) { - log.error("更新Google Seo信息失败"); - } - } - } catch (Exception e) { - - } - } - private void generateContentSiteMap(List googleSeoDtoList, SimpleDateFormat sdf) { // 文章总数 Integer total = contentService.total(); diff --git a/upupor-web/src/main/java/com/upupor/web/outer/OuterController.java b/upupor-web/src/main/java/com/upupor/web/outer/OuterController.java new file mode 100644 index 00000000..aa8f7e49 --- /dev/null +++ b/upupor-web/src/main/java/com/upupor/web/outer/OuterController.java @@ -0,0 +1,72 @@ +/* + * MIT License + * + * Copyright (c) 2021-2022 yangrunkang + * + * Author: yangrunkang + * Email: yangrunkang53@gmail.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.upupor.web.outer; + +import com.alibaba.druid.util.StringUtils; +import com.upupor.service.business.aggregation.dao.entity.Seo; +import com.upupor.service.business.aggregation.service.SeoService; +import com.upupor.service.common.CcConstant; +import com.upupor.service.scheduled.GenerateSiteMapScheduled; +import com.upupor.service.utils.RedisUtil; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.Objects; + +import static com.upupor.service.common.CcConstant.CvCache.SITE_MAP; + + +/** + * 活动控制器 + * + * @author YangRunkang(cruise) + * @date 2020/02/05 00:13 + */ +@Api(tags = "对外接口") +@RestController +@RequiredArgsConstructor +public class OuterController { + private final GenerateSiteMapScheduled generateSiteMapScheduled; + + @ApiOperation("站点地图") + @GetMapping(value = "/upupor-google-sitemap.xml", produces = {"application/xml; charset=UTF-8"}) + public String siteMap() { + + String s = RedisUtil.get(SITE_MAP); + if (StringUtils.isEmpty(s)) { + generateSiteMapScheduled.googleSitemap(); + s = RedisUtil.get(SITE_MAP); + } + + + return StringUtils.isEmpty(s) ? "生成SiteMap失败" : s; + } + +} diff --git a/upupor-web/src/main/resources/static/upupor-google-sitemap.xml b/upupor-web/src/main/resources/static/upupor-google-sitemap.xml deleted file mode 100644 index 8dd87ceb..00000000 --- a/upupor-web/src/main/resources/static/upupor-google-sitemap.xml +++ /dev/null @@ -1,2 +0,0 @@ -手动刷新地址 -/refresh-google-sitemap diff --git a/upupor-web/src/main/resources/templates/layout/footer.html b/upupor-web/src/main/resources/templates/layout/footer.html index 13046512..35fa64b9 100644 --- a/upupor-web/src/main/resources/templates/layout/footer.html +++ b/upupor-web/src/main/resources/templates/layout/footer.html @@ -48,7 +48,7 @@
  • logo设计
  • 反馈
  • 积分规则
  • -
  • 站点地图
  • +
  • 站点地图
  • 微信公众号: www-upupor-com 微信