From e0dccb6fd24427a81db9977ca56845d73bdd7bb8 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sat, 22 Jun 2024 19:30:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=AE=89=E5=85=A8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E3=80=82=E6=96=B0=E5=A2=9E=E4=B9=A6=E7=AD=BE?= =?UTF-8?q?=E8=80=97=E6=97=B6=E9=95=BF=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/BookmarkServiceImpl.java | 52 ++++++++++++---- bookMarkService/business/pom.xml | 6 -- bookMarkService/common/pom.xml | 62 +++++++++---------- .../fanxb/bookmark/common/util/HttpUtil.java | 8 +++ bookMarkService/pom.xml | 2 +- bookmark_front/.gitignore | 1 + 6 files changed, 81 insertions(+), 50 deletions(-) diff --git a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/impl/BookmarkServiceImpl.java b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/impl/BookmarkServiceImpl.java index f1f4eaa..46d220b 100644 --- a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/impl/BookmarkServiceImpl.java +++ b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/impl/BookmarkServiceImpl.java @@ -43,6 +43,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.MalformedURLException; +import java.net.SocketTimeoutException; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.Charset; @@ -280,11 +281,14 @@ public class BookmarkServiceImpl implements BookmarkService { bookmark.setUserId(userId); bookmark.setCreateTime(System.currentTimeMillis()); bookmark.setAddTime(bookmark.getCreateTime()); - bookmark.setIcon(getIconPath(bookmark.getUrl(), bookmark.getIcon(), bookmark.getIconUrl())); + bookmark.setIcon(bookmark.getType() == 1 ? "" : getIconPath(bookmark.getUrl(), bookmark.getIcon(), bookmark.getIconUrl(), true)); //文件夹和书签都建立搜索key pinYinService.changeBookmark(bookmark); bookmarkDao.insertOne(bookmark); userApi.versionPlus(userId); + if (StrUtil.isEmpty(bookmark.getIcon()) && bookmark.getType() == 0) { + updateIconAsync(bookmark.getBookmarkId(), bookmark.getUrl(), userId); + } return bookmark; } @@ -294,13 +298,33 @@ public class BookmarkServiceImpl implements BookmarkService { bookmark.setUserId(userId); if (bookmark.getType() == 0) { pinYinService.changeBookmark(bookmark); - bookmark.setIcon(getIconPath(bookmark.getUrl(), null, null)); + bookmark.setIcon(getIconPath(bookmark.getUrl(), null, null, true)); + if (StrUtil.isEmpty(bookmark.getIcon())) { + updateIconAsync(bookmark.getBookmarkId(), bookmark.getUrl(), userId); + } } bookmarkDao.editBookmark(bookmark); userApi.versionPlus(userId); return bookmark.getIcon(); } + /** + * 异步更新书签icon + * + * @param id 书签id + * @param url 书签url + * @param userId userId + */ + private void updateIconAsync(int id, String url, int userId) { + ThreadPoolUtil.execute(() -> { + String icon = getIconPath(url, null, null, false); + if (StrUtil.isEmpty(icon)) { + return; + } + bookmarkDao.updateIcon(id, icon); + }); + } + @Override @Transactional(rollbackFor = Exception.class) @@ -353,7 +377,7 @@ public class BookmarkServiceImpl implements BookmarkService { while (!(deal = bookmarkDao.selectUserNoIcon(userId, start, size)).isEmpty()) { start += size; deal.forEach(item -> { - String icon = getIconPath(item.getUrl(), null, null); + String icon = getIconPath(item.getUrl(), null, null, false); if (StrUtil.isNotEmpty(icon)) { bookmarkDao.updateIcon(item.getBookmarkId(), icon); } @@ -387,13 +411,14 @@ public class BookmarkServiceImpl implements BookmarkService { /** * 获取icon,通过网络获取,或者从base64还原 * - * @param url url - * @param icon icon - * @param iconUrl iconUrl + * @param url 书签url路径 + * @param icon base64编码的icon + * @param iconUrl base64编码的文件,文件名,用于获取文件名后缀 + * @param quick 是否快速获取 * @return {@link String} * @author fanxb */ - private String getIconPath(String url, String icon, String iconUrl) { + private String getIconPath(String url, String icon, String iconUrl, boolean quick) { String host; try { URL urlObj = new URL(url); @@ -420,7 +445,7 @@ public class BookmarkServiceImpl implements BookmarkService { return iconPath; } //再根据url解析 - iconPath = saveFile(host, urlIconAddress + "/icon?url=" + host + "&size=16..128..256"); + iconPath = saveFile(host, urlIconAddress + "/icon?url=" + host + "&size=16..128..256", quick); if (StrUtil.isNotEmpty(iconPath)) { hostIconDao.insert(host, iconPath); } @@ -430,13 +455,14 @@ public class BookmarkServiceImpl implements BookmarkService { /** * 保存文件到icon路径 * - * @param host host - * @param url url + * @param host host + * @param url url + * @param quick 是否快速获取,快速获取超时时间1s * @return {@link String} * @author FleyX */ - private String saveFile(String host, String url) { - try (Response res = HttpUtil.getClient(false).newCall(new Request.Builder().url(url) + private String saveFile(String host, String url, boolean quick) { + try (Response res = (quick ? HttpUtil.getSHORT_CLIENT() : HttpUtil.getClient(false)).newCall(new Request.Builder().url(url) .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.36") .get().build()).execute()) { assert res.body() != null; @@ -450,6 +476,8 @@ public class BookmarkServiceImpl implements BookmarkService { } else { log.info("未获取到icon:{}", url); } + } catch (SocketTimeoutException timeoutException) { + log.info("获取icon超时:{}", host); } catch (Exception e) { log.error("url获取icon故障:{}", url, e); } diff --git a/bookMarkService/business/pom.xml b/bookMarkService/business/pom.xml index 5ce2c43..284712d 100644 --- a/bookMarkService/business/pom.xml +++ b/bookMarkService/business/pom.xml @@ -23,12 +23,6 @@ bookmark-common 1.0-SNAPSHOT - - - - org.springframework.boot - spring-boot-starter-web - diff --git a/bookMarkService/common/pom.xml b/bookMarkService/common/pom.xml index b82dec7..b09a7fe 100644 --- a/bookMarkService/common/pom.xml +++ b/bookMarkService/common/pom.xml @@ -74,44 +74,44 @@ 8.0.33 - - - org.springframework.boot - spring-boot-starter-mail - + + + + + - - - org.projectlombok - lombok - - - - com.alibaba - fastjson - 1.2.83 - + + + + + + + + + + + - - org.elasticsearch.client - elasticsearch-rest-high-level-client - + + + + - - cn.hutool - hutool-all - 5.8.21 - + + + + + - - mysql - mysql-connector-java - 8.0.33 - + + + + + @@ -140,7 +140,7 @@ cn.hutool hutool-all - 5.8.21 + 5.8.25 diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HttpUtil.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HttpUtil.java index 17665d4..377441b 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HttpUtil.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HttpUtil.java @@ -53,6 +53,14 @@ public class HttpUtil { .readTimeout(60, TimeUnit.SECONDS) .build(); + /** + * 超时时间1s + */ + @Getter + private static final OkHttpClient SHORT_CLIENT = new OkHttpClient.Builder().connectTimeout(1, TimeUnit.SECONDS) + .readTimeout(1, TimeUnit.SECONDS) + .build(); + /** * 获取客户端 * diff --git a/bookMarkService/pom.xml b/bookMarkService/pom.xml index 7f846df..3bd493f 100644 --- a/bookMarkService/pom.xml +++ b/bookMarkService/pom.xml @@ -22,7 +22,7 @@ org.springframework.boot spring-boot-starter-parent - 2.7.14 + 2.7.17 diff --git a/bookmark_front/.gitignore b/bookmark_front/.gitignore index 3a97fa6..037f9a5 100644 --- a/bookmark_front/.gitignore +++ b/bookmark_front/.gitignore @@ -24,3 +24,4 @@ pnpm-debug.log* *.njsproj *.sln *.sw? +