[Core] Support preview images

This commit is contained in:
qianmoQ 2024-05-23 00:15:48 +08:00
parent 91493d8286
commit b0cdc46433
7 changed files with 76 additions and 3 deletions

View File

@ -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("/+", "/");
}
}

View File

@ -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();

View File

@ -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() + "/");
}
}

View File

@ -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(), ""));
}
}

View File

@ -0,0 +1,5 @@
export interface AvatarModel
{
type?: string
path?: string
}

View File

@ -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

View File

@ -45,7 +45,7 @@
</template>
<div class="shadow-blackA7 w-full overflow-hidden rounded-md">
<AspectRatio :ratio="16 / 11">
<img class="h-full w-full object-cover" src="/static/images/dashboard.png" :alt="item.name"/>
<img class="h-full w-full object-cover" :src="`${item.avatar?.path ? item.avatar.path : '/static/images/dashboard.png'}`" :alt="item.name"/>
</AspectRatio>
</div>
<template #footer>