From b0cdc46433e90425e1460c23178a9a90737eced6 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Thu, 23 May 2024 00:15:48 +0800 Subject: [PATCH] [Core] Support preview images --- .../edurt/datacap/common/utils/UrlUtils.java | 19 ++++++++++++++ .../datacap/security/SecurityConfigure.java | 2 +- .../datacap/security/WebAppConfigure.java | 25 +++++++++++++++++++ .../service/impl/UploadServiceImpl.java | 24 +++++++++++++++++- core/datacap-ui/src/model/avatar.ts | 5 ++++ core/datacap-ui/src/model/dashboard.ts | 2 ++ .../pages/admin/dashboard/DashboardHome.vue | 2 +- 7 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 core/datacap-common/src/main/java/io/edurt/datacap/common/utils/UrlUtils.java create mode 100644 core/datacap-security/src/main/java/io/edurt/datacap/security/WebAppConfigure.java create mode 100644 core/datacap-ui/src/model/avatar.ts diff --git a/core/datacap-common/src/main/java/io/edurt/datacap/common/utils/UrlUtils.java b/core/datacap-common/src/main/java/io/edurt/datacap/common/utils/UrlUtils.java new file mode 100644 index 00000000..74aee6d5 --- /dev/null +++ b/core/datacap-common/src/main/java/io/edurt/datacap/common/utils/UrlUtils.java @@ -0,0 +1,19 @@ +package io.edurt.datacap.common.utils; + +public class UrlUtils +{ + private UrlUtils() + { + } + + /** + * Replaces multiple consecutive slashes in the given URL with a single slash. + * + * @param url the URL to fix + * @return the fixed URL with single slashes + */ + public static String fixUrl(String url) + { + return url.replaceAll("/+", "/"); + } +} diff --git a/core/datacap-security/src/main/java/io/edurt/datacap/security/SecurityConfigure.java b/core/datacap-security/src/main/java/io/edurt/datacap/security/SecurityConfigure.java index af56e6a3..c2f457b5 100644 --- a/core/datacap-security/src/main/java/io/edurt/datacap/security/SecurityConfigure.java +++ b/core/datacap-security/src/main/java/io/edurt/datacap/security/SecurityConfigure.java @@ -68,7 +68,7 @@ public class SecurityConfigure .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .authorizeRequests() - .antMatchers("/", "/**/*.js", "/**/*.css", "/api/auth/**", "/fonts/**", "/static/**", "/h2-console/**", "/api/v1/captcha", "/api/v1/table/dataDownload/**") + .antMatchers("/", "/**/*.js", "/**/*.css", "/api/auth/**", "/fonts/**", "/static/**", "/h2-console/**", "/api/v1/captcha", "/api/v1/table/dataDownload/**", "/upload/**") .permitAll() .anyRequest() .authenticated(); diff --git a/core/datacap-security/src/main/java/io/edurt/datacap/security/WebAppConfigure.java b/core/datacap-security/src/main/java/io/edurt/datacap/security/WebAppConfigure.java new file mode 100644 index 00000000..56dadc33 --- /dev/null +++ b/core/datacap-security/src/main/java/io/edurt/datacap/security/WebAppConfigure.java @@ -0,0 +1,25 @@ +package io.edurt.datacap.security; + +import io.edurt.datacap.service.initializer.InitializerConfigure; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebAppConfigure + implements WebMvcConfigurer +{ + private final InitializerConfigure initializer; + + public WebAppConfigure(InitializerConfigure initializer) + { + this.initializer = initializer; + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) + { + registry.addResourceHandler("/upload/**") + .addResourceLocations("file:" + initializer.getDataHome() + "/"); + } +} diff --git a/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/UploadServiceImpl.java b/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/UploadServiceImpl.java index bb1c43c1..f0e74af7 100644 --- a/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/UploadServiceImpl.java +++ b/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/UploadServiceImpl.java @@ -3,6 +3,7 @@ package io.edurt.datacap.service.service.impl; import com.google.inject.Injector; import io.edurt.datacap.common.response.CommonResponse; import io.edurt.datacap.common.utils.SpiUtils; +import io.edurt.datacap.common.utils.UrlUtils; import io.edurt.datacap.fs.FsRequest; import io.edurt.datacap.fs.FsResponse; import io.edurt.datacap.service.body.UploadBody; @@ -16,6 +17,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletRequest; + import java.io.IOException; @Slf4j @@ -24,12 +27,14 @@ public class UploadServiceImpl implements UploadService { private final Injector injector; + private final HttpServletRequest request; private final DashboardRepository dashboard; private final InitializerConfigure initializer; - public UploadServiceImpl(Injector injector, DashboardRepository dashboard, InitializerConfigure initializer) + public UploadServiceImpl(Injector injector, HttpServletRequest request, DashboardRepository dashboard, InitializerConfigure initializer) { this.injector = injector; + this.request = request; this.dashboard = dashboard; this.initializer = initializer; } @@ -49,6 +54,9 @@ public class UploadServiceImpl .path(response.getRemote()) .type(initializer.getFsConfigure().getType()) .build(); + if (initializer.getFsConfigure().getType().equals("Local")) { + entity.setPath(getAccess(entity)); + } value.setAvatar(entity); dashboard.save(value); }); @@ -109,4 +117,18 @@ public class UploadServiceImpl configure.getMode().toString().toLowerCase(), configure.getCode()); } + + /** + * Returns the access URL for the given AvatarEntity. + * + * @param configure the AvatarEntity containing the path + * @return the access URL + */ + private String getAccess(AvatarEntity configure) + { + String protocol = request.getScheme(); + String host = request.getServerName(); + int port = request.getServerPort(); + return protocol + "://" + host + ":" + port + UrlUtils.fixUrl("/upload" + configure.getPath().replaceFirst(initializer.getDataHome(), "")); + } } diff --git a/core/datacap-ui/src/model/avatar.ts b/core/datacap-ui/src/model/avatar.ts new file mode 100644 index 00000000..843bce2c --- /dev/null +++ b/core/datacap-ui/src/model/avatar.ts @@ -0,0 +1,5 @@ +export interface AvatarModel +{ + type?: string + path?: string +} diff --git a/core/datacap-ui/src/model/dashboard.ts b/core/datacap-ui/src/model/dashboard.ts index 31feba61..607ba548 100644 --- a/core/datacap-ui/src/model/dashboard.ts +++ b/core/datacap-ui/src/model/dashboard.ts @@ -1,5 +1,6 @@ import { UserModel } from '@/model/user.ts' import { ReportModel } from '@/model/report.ts' +import { AvatarModel } from '@/model/avatar.ts' export interface DashboardModel { @@ -13,6 +14,7 @@ export interface DashboardModel user?: UserModel reports?: ReportModel[] code?: string + avatar?: AvatarModel } export class DashboardRequest diff --git a/core/datacap-ui/src/views/pages/admin/dashboard/DashboardHome.vue b/core/datacap-ui/src/views/pages/admin/dashboard/DashboardHome.vue index 3423e874..f400610b 100644 --- a/core/datacap-ui/src/views/pages/admin/dashboard/DashboardHome.vue +++ b/core/datacap-ui/src/views/pages/admin/dashboard/DashboardHome.vue @@ -45,7 +45,7 @@
- +