Skip to content

Commit ecbacd2

Browse files
committed
Ref: 重构站点地图实现方式,不使用xml文件
1 parent 53162fd commit ecbacd2

File tree

5 files changed

+81
-69
lines changed

5 files changed

+81
-69
lines changed

upupor-service/src/main/java/com/upupor/service/common/CcConstant.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ public static final class CvCache {
368368
*/
369369
public static final String CREATE_CONTENT_TIME_OUT = "create_content_time_out";
370370

371+
/**
372+
* SiteMap
373+
*/
374+
public static final String SITE_MAP = "siteMap";
375+
376+
371377

372378
public static final String createContentIntervalkey(String userId){
373379
return CcConstant.CvCache.CREATE_CONTENT_TIME_OUT + userId;

upupor-service/src/main/java/com/upupor/service/scheduled/GenerateSiteMapScheduled.java

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,12 @@
4646
import org.springframework.scheduling.annotation.Scheduled;
4747
import org.springframework.stereotype.Service;
4848
import org.springframework.util.CollectionUtils;
49-
import org.springframework.util.ResourceUtils;
5049
import org.springframework.util.StringUtils;
5150

52-
import java.io.BufferedWriter;
53-
import java.io.File;
54-
import java.io.FileWriter;
55-
import java.io.IOException;
5651
import java.text.SimpleDateFormat;
5752
import java.util.*;
5853

54+
import static com.upupor.service.common.CcConstant.CvCache.SITE_MAP;
5955
import static com.upupor.service.common.CcConstant.CvCache.TAG_COUNT;
6056

6157

@@ -123,11 +119,7 @@ public void googleSitemap() {
123119
return;
124120
}
125121

126-
// 写到文件 upupor-google-sitemap.xml
127-
writeToFile(s);
128-
// 备份
129-
bakSeoData(s);
130-
122+
RedisUtil.set(SITE_MAP, s);
131123
}
132124

133125
private void generateRadio(List<GoogleSeoDto> googleSeoDtoList, SimpleDateFormat sdf) {
@@ -315,62 +307,6 @@ private void generatePageSiteMap(List<GoogleSeoDto> googleSeoDtoList, SimpleDate
315307
}
316308

317309

318-
private void writeToFile(String s) {
319-
BufferedWriter out = null;
320-
try {
321-
// 这个文件是写到了打包后的classes目录下的文件,开发环境看不到
322-
File file = ResourceUtils.getFile("classpath:static/upupor-google-sitemap.xml");
323-
if (!file.exists()) {
324-
boolean newFile = file.createNewFile();
325-
if (!newFile) {
326-
log.error("upupor-google-sitemap.xml文件创建失败");
327-
}
328-
}
329-
out = new BufferedWriter(new FileWriter(file));
330-
out.write(s);
331-
out.flush();
332-
out.close();
333-
log.info("Google站点地图生成完毕");
334-
} catch (Exception e) {
335-
log.error("写入站点地图失败,{}", e.getMessage());
336-
} finally {
337-
try {
338-
if (Objects.nonNull(out)) {
339-
out.close();
340-
}
341-
} catch (IOException e) {
342-
e.printStackTrace();
343-
}
344-
}
345-
}
346-
347-
private void bakSeoData(String s) {
348-
try {
349-
// 生成Google站点地图
350-
Seo google = seoService.getBySeoId("google");
351-
if (Objects.isNull(google)) {
352-
Seo seo = new Seo();
353-
seo.setSeoId("google");
354-
seo.setCreateTime(CcDateUtil.getCurrentTime());
355-
seo.setSeoStatus(SeoStatus.NORMAL);
356-
seo.setSysUpdateTime(new Date());
357-
seo.setSeoContent(s);
358-
Boolean addSeo = seoService.addSeo(seo);
359-
if (!addSeo) {
360-
log.error("添加Google Seo信息失败");
361-
}
362-
} else {
363-
google.setSeoContent(s);
364-
Boolean update = seoService.updateSeo(google);
365-
if (!update) {
366-
log.error("更新Google Seo信息失败");
367-
}
368-
}
369-
} catch (Exception e) {
370-
371-
}
372-
}
373-
374310
private void generateContentSiteMap(List<GoogleSeoDto> googleSeoDtoList, SimpleDateFormat sdf) {
375311
// 文章总数
376312
Integer total = contentService.total();
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2021-2022 yangrunkang
5+
*
6+
* Author: yangrunkang
7+
* Email: yangrunkang53@gmail.com
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in all
17+
* copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
* SOFTWARE.
26+
*/
27+
28+
package com.upupor.web.outer;
29+
30+
import com.alibaba.druid.util.StringUtils;
31+
import com.upupor.service.business.aggregation.dao.entity.Seo;
32+
import com.upupor.service.business.aggregation.service.SeoService;
33+
import com.upupor.service.common.CcConstant;
34+
import com.upupor.service.scheduled.GenerateSiteMapScheduled;
35+
import com.upupor.service.utils.RedisUtil;
36+
import io.swagger.annotations.Api;
37+
import io.swagger.annotations.ApiOperation;
38+
import lombok.RequiredArgsConstructor;
39+
import org.springframework.web.bind.annotation.*;
40+
41+
import java.util.Objects;
42+
43+
import static com.upupor.service.common.CcConstant.CvCache.SITE_MAP;
44+
45+
46+
/**
47+
* 活动控制器
48+
*
49+
* @author YangRunkang(cruise)
50+
* @date 2020/02/05 00:13
51+
*/
52+
@Api(tags = "对外接口")
53+
@RestController
54+
@RequiredArgsConstructor
55+
public class OuterController {
56+
private final GenerateSiteMapScheduled generateSiteMapScheduled;
57+
58+
@ApiOperation("站点地图")
59+
@GetMapping(value = "/upupor-google-sitemap.xml", produces = {"application/xml; charset=UTF-8"})
60+
public String siteMap() {
61+
62+
String s = RedisUtil.get(SITE_MAP);
63+
if (StringUtils.isEmpty(s)) {
64+
generateSiteMapScheduled.googleSitemap();
65+
s = RedisUtil.get(SITE_MAP);
66+
}
67+
68+
69+
return StringUtils.isEmpty(s) ? "生成SiteMap失败" : s;
70+
}
71+
72+
}

upupor-web/src/main/resources/static/upupor-google-sitemap.xml

Lines changed: 0 additions & 2 deletions
This file was deleted.

upupor-web/src/main/resources/templates/layout/footer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<li class="list-inline-item me-1"><a class="cv-link text-black-50" href="/logo-design" title="logo设计">logo设计</a></li>
4949
<li class="list-inline-item me-1"><a class="cv-link text-black-50" href="/feedback" title="反馈">反馈</a></li>
5050
<li class="list-inline-item me-1"><a class="cv-link text-black-50" href="/integral-rules" title="积分规则">积分规则</a></li>
51-
<li class="list-inline-item me-1"><a class="cv-link text-black-50" href="https://www.upupor.com/upupor-google-sitemap.xml" title="站点地图">站点地图</a></li>
51+
<li class="list-inline-item me-1"><a class="cv-link text-black-50" href="/upupor-google-sitemap.xml" title="站点地图">站点地图</a></li>
5252
<li class="list-inline-item me-1">
5353
<a class="cv-link text-black-50" data-bs-toggle="modal" data-bs-target="#wechat" title="微信公众号: www-upupor-com">
5454
<img class="operation-pic" th:src="${ossStatic} + @{/icons/system/footer/wechat.svg}" alt="微信公众号: www-upupor-com" style="height: 16px; width: 16px;"/> 微信

0 commit comments

Comments
 (0)