Skip to content

Commit

Permalink
Ref: 重构站点地图实现方式,不使用xml文件
Browse files Browse the repository at this point in the history
  • Loading branch information
yangrunkang committed Jan 16, 2022
1 parent 53162fd commit ecbacd2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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<GoogleSeoDto> googleSeoDtoList, SimpleDateFormat sdf) {
Expand Down Expand Up @@ -315,62 +307,6 @@ private void generatePageSiteMap(List<GoogleSeoDto> 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<GoogleSeoDto> googleSeoDtoList, SimpleDateFormat sdf) {
// 文章总数
Integer total = contentService.total();
Expand Down
72 changes: 72 additions & 0 deletions upupor-web/src/main/java/com/upupor/web/outer/OuterController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* MIT License
*
* Copyright (c) 2021-2022 yangrunkang
*
* Author: yangrunkang
* Email: [email protected]
*
* 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;
}

}

This file was deleted.

2 changes: 1 addition & 1 deletion upupor-web/src/main/resources/templates/layout/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<li class="list-inline-item me-1"><a class="cv-link text-black-50" href="/logo-design" title="logo设计">logo设计</a></li>
<li class="list-inline-item me-1"><a class="cv-link text-black-50" href="/feedback" title="反馈">反馈</a></li>
<li class="list-inline-item me-1"><a class="cv-link text-black-50" href="/integral-rules" title="积分规则">积分规则</a></li>
<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>
<li class="list-inline-item me-1"><a class="cv-link text-black-50" href="/upupor-google-sitemap.xml" title="站点地图">站点地图</a></li>
<li class="list-inline-item me-1">
<a class="cv-link text-black-50" data-bs-toggle="modal" data-bs-target="#wechat" title="微信公众号: www-upupor-com">
<img class="operation-pic" th:src="${ossStatic} + @{/icons/system/footer/wechat.svg}" alt="微信公众号: www-upupor-com" style="height: 16px; width: 16px;"/> 微信
Expand Down

0 comments on commit ecbacd2

Please sign in to comment.