限定图片路径

This commit is contained in:
sxu 2024-09-28 18:56:22 +08:00
parent d0d07f5419
commit 15c1a7e89a
5 changed files with 70 additions and 5 deletions

View File

@ -1,5 +1,9 @@
package com.bonus.sgzb.common.core.constant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 通用常量信息
*
@ -139,6 +143,12 @@ public class Constants
public static final String[] JOB_ERROR_STR = { "java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml",
"org.springframework", "org.apache", "com.bonus.sgzb.common.core.utils.file" };
// 常见图片格式
public static final List<String> IMAGE_EXTENSIONS = Arrays.asList("jpg", "jpeg", "png", "gif");
public static final String UPLOAD_FILE_URL_PART = "myqcloud.com";
/**
* 系统初始密码
*/

View File

@ -7,6 +7,9 @@ import com.bonus.sgzb.common.core.text.StrFormatter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import static com.bonus.sgzb.common.core.constant.Constants.IMAGE_EXTENSIONS;
import static com.bonus.sgzb.common.core.constant.Constants.UPLOAD_FILE_URL_PART;
/**
* 字符串工具类
*
@ -620,4 +623,46 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
// System.out.println(getDeviceCode("CSG", "线路施工类", "102", new Date(), "500322"));
// }
public static boolean checkIfFileUrlsValid(String fileUrls) {
boolean url_valid = true;
if (StringUtils.isEmpty(fileUrls)) {
return true;
} else {
String[] fileUrlArray = fileUrls.split(",");
for (String fileUrl : fileUrlArray) {
if (!fileUrl.toLowerCase().contains(UPLOAD_FILE_URL_PART)) {
url_valid = false;
}
}
return url_valid;
}
}
public static boolean checkFileUrlsEndWithExtensions(String fileUrls) {
if (StringUtils.isEmpty(fileUrls)) {
return true;
} else {
String[] fileUrlArray = fileUrls.split(",");
List<String> fileUrlList = Arrays.asList(fileUrlArray);
for (String fileUrl : fileUrlList) {
boolean endsWithExtension = false;
for (String extension : IMAGE_EXTENSIONS) {
if (fileUrl.toLowerCase().endsWith(extension)) {
endsWithExtension = true;
break;
}
}
if (!endsWithExtension) {
return false;
}
}
return true;
}
}
public static void main(String[] args) {
String fileUrls = "https://example.com/file1.png,https://example.com/file2.gif";
boolean allEndWithExtensions = checkFileUrlsEndWithExtensions(fileUrls);
System.out.println("所有文件URL都以给定的扩展名结尾 " + allEndWithExtensions);
}
}

View File

@ -82,7 +82,7 @@ public class AgreementInfoController extends BaseController {
@ApiOperation("协议管理-修改")
@PostMapping("/update")
public AjaxResult update(@Validated @RequestBody AgreementInfo bean) {
return toAjax(agreementInfoService.update(bean));
return agreementInfoService.update(bean);
}

View File

@ -42,7 +42,7 @@ public interface AgreementInfoService {
* @param bean
* @return
*/
int update(AgreementInfo bean);
AjaxResult update(AgreementInfo bean);
/**
* 根据id删除

View File

@ -1,6 +1,7 @@
package com.bonus.sgzb.material.service.impl;
import com.bonus.sgzb.common.core.utils.DateUtils;
import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.material.domain.AgreementInfo;
import com.bonus.sgzb.material.mapper.AgreementInfoMapper;
@ -8,7 +9,6 @@ import com.bonus.sgzb.material.service.AgreementInfoService;
import com.bonus.sgzb.material.vo.GlobalContants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@ -52,8 +52,18 @@ public class AgreementInfoServiceImpl implements AgreementInfoService {
}
@Override
public int update(AgreementInfo bean) {
return agreementInfoMapper.update(bean);
public AjaxResult update(AgreementInfo bean) {
boolean url_valid = StringUtils.checkIfFileUrlsValid(bean.getFileUrl());
boolean extent_valid = StringUtils.checkFileUrlsEndWithExtensions(bean.getFileUrl());
if (!url_valid && extent_valid) {
return AjaxResult.error("图片地址无效,不可上传");
}
int count = agreementInfoMapper.update(bean);
if (count > 0) {
return AjaxResult.success("上传成功");
} else {
return AjaxResult.error("上传失败");
}
}
@Override