Skip to content

Commit

Permalink
✨ 新增网站 favicon 网站地址自定义功能,返回的 html 是已经修改过的,不是等待页面加载完再修改。
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojun1998 committed May 27, 2023
1 parent 141f9de commit ac3b428
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package im.zhaojun.zfile.core.controller;

import cn.hutool.core.util.StrUtil;
import im.zhaojun.zfile.module.config.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.module.config.service.SystemConfigService;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
* 处理前端首页 Controller
Expand All @@ -11,15 +22,36 @@
@Controller
public class FrontIndexController {

@Resource
private SystemConfigService systemConfigService;


/**
* 所有未找到的页面都跳转到首页, 用户解决 vue history 直接访问 404 的问题
* 同时, 读取 index.html 文件, 修改 title 和 favicon 后返回.
*
* @return 转发到 /index.html
*/
@RequestMapping(value = "/**/{[path:[^\\.]*}")
public String redirect() {
// Forward to home page so that route is preserved.
return "forward:/";
@RequestMapping(value = {"/**/{[path:[^\\.]*}", "/"})
@ResponseBody
public String redirect() throws IOException {
// 读取 resources/static/index.html 文件修改 title 和 favicon 后返回
ClassPathResource resource = new ClassPathResource("static/index.html");
InputStream inputStream = resource.getInputStream();
String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);

SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
String siteName = systemConfig.getSiteName();
if (StrUtil.isNotBlank(siteName)) {
content = content.replace("<title>ZFile</title>", "<title>" + siteName + "</title>");
}

String faviconUrl = systemConfig.getFaviconUrl();
if (StrUtil.isNotBlank(faviconUrl)) {
content = content.replace("/favicon.svg", faviconUrl);
}

return content;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ public class UpdateSiteSettingRequest {

@ApiModelProperty(value = "站点 Logo 链接打开方式", example = "_blank")
private String siteHomeLogoTargetMode;

@ApiModelProperty(value = "网站 favicon 图标地址", example = "https://www.example.com/favicon.ico")
private String faviconUrl;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO system_config (`name`, `title`) VALUES ('faviconUrl', '网站 favicon 图标地址');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO system_config (`name`, `title`) VALUES ('faviconUrl', '网站 favicon 图标地址');

0 comments on commit ac3b428

Please sign in to comment.