修正svg图片上传资源库报错问题

This commit is contained in:
liweiyi 2025-09-04 11:34:59 +08:00
parent a3261f18cb
commit 36c3b3b320
4 changed files with 39 additions and 76 deletions

View File

@ -87,6 +87,10 @@ public class ResourceType_Image implements IResourceType {
@Override @Override
public List<File> process(CmsResource resource, File file) throws IOException { public List<File> process(CmsResource resource, File file) throws IOException {
String extension = FilenameUtils.getExtension(file.getName());
if ("svg".equalsIgnoreCase(extension)) {
return List.of(); // 不处理svg图片
}
CmsSite site = siteService.getSite(resource.getSiteId()); CmsSite site = siteService.getSite(resource.getSiteId());
boolean needWatermark = needWatermark(site); boolean needWatermark = needWatermark(site);
boolean needThumbnail = needThumbnail(site); boolean needThumbnail = needThumbnail(site);
@ -112,63 +116,6 @@ public class ResourceType_Image implements IResourceType {
return files; return files;
} }
// @Override
// public void asyncProcess(CmsResource resource) {
// CmsSite site = siteService.getSite(resource.getSiteId());
// boolean needWatermark = needWatermark(site);
// boolean needThumbnail = needThumbnail(site);
// if (!needWatermark && !needThumbnail) {
// return; // 不需要处理
// }
// asyncTaskManager.execute(() -> {
// String fileStorageType = FileStorageTypeProperty.getValue(site.getConfigProps());
// IFileStorageType fst = fileStorageTypeMap.get(IFileStorageType.BEAN_NAME_PREIFX + fileStorageType);
// FileStorageHelper fileStorageHelper = FileStorageHelper.of(fst, site);
// String tempDirectory = ResourceUtils.getResourceTempDirectory(site);
// File tempFile = new File(tempDirectory + resource.getPath());
// FileExUtils.mkdirs(tempFile.getParent());
// Map<String, File> paths = new HashMap<>();
// try (InputStream read = fileStorageHelper.read(resource.getPath())) {
// FileExUtils.transfer(read, tempFile);
// // 获取图片属性
// BufferedImage bi = ImageIO.read(tempFile);
// resource.setWidth(bi.getWidth());
// resource.setHeight(bi.getHeight());
// // 先处理图片水印
// if (needWatermark) {
// watermark(site, bi, tempFile);
// paths.put(resource.getPath(), tempFile);
// resource.setFileSize(tempFile.length());
// }
// // 默认缩略图处理
// thumbnail(site, resource, tempFile);
// // 更新文件
// for (Map.Entry<String, File> entry : paths.entrySet()) {
// try (FileInputStream is = new FileInputStream(entry.getValue())) {
// fileStorageHelper.write(entry.getKey(), is, entry.getValue().length());
// } catch (IOException e) {
// log.error("Write temp file to storage {} failed.", fst.getType(), e);
// }
// }
// if (needWatermark) {
// resourceService.updateById(resource);
// }
// } catch (IOException e) {
// log.error("Image resource process fail.", e);
// } finally {
// try {
// // 删除临时文件
// Files.deleteIfExists(tempFile.toPath());
// for (File file : paths.values()) {
// Files.deleteIfExists(file.toPath());
// }
// } catch (IOException e) {
// log.error("Delete temp file failed.", e);
// }
// }
// });
// }
private boolean needWatermark(CmsSite site) { private boolean needWatermark(CmsSite site) {
if (!ImageWatermarkProperty.getValue(site.getConfigProps())) { if (!ImageWatermarkProperty.getValue(site.getConfigProps())) {
return false; return false;

View File

@ -106,7 +106,7 @@ public interface IResourceService extends IService<CmsResource> {
* @param width 缩略图宽度 * @param width 缩略图宽度
* @param height 缩略图高度 * @param height 缩略图高度
*/ */
void createThumbnailIfNotExists(InternalURL internalURL, int width, int height) throws Exception; boolean createThumbnailIfNotExists(InternalURL internalURL, int width, int height) throws Exception;
/** /**
* 处理默认缩略图 * 处理默认缩略图

View File

@ -45,6 +45,7 @@ import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -374,25 +375,32 @@ public class ResourceServiceImpl extends ServiceImpl<CmsResourceMapper, CmsResou
} }
@Override @Override
public void createThumbnailIfNotExists(InternalURL internalUrl, int width, int height) throws Exception { public boolean createThumbnailIfNotExists(InternalURL internalUrl, int width, int height) throws Exception {
if (!InternalDataType_Resource.ID.equals(internalUrl.getType()) if (!InternalDataType_Resource.ID.equals(internalUrl.getType())
|| !ResourceType_Image.isImage(internalUrl.getPath())) { || !ResourceType_Image.isImage(internalUrl.getPath())) {
return; // 非图片资源忽略 return false; // 非图片资源忽略
} }
Long siteId = MapUtils.getLong(internalUrl.getParams(), InternalDataType_Resource.InternalUrl_Param_SiteId); Long siteId = MapUtils.getLong(internalUrl.getParams(), InternalDataType_Resource.InternalUrl_Param_SiteId);
CmsSite site = siteService.getSite(siteId); CmsSite site = siteService.getSite(siteId);
String filePath = internalUrl.getPath(); String filePath = internalUrl.getPath();
String thumbnailPath = ImageUtils.getThumbnailFileName(filePath, width, height); String thumbnailPath = ImageUtils.getThumbnailFileName(filePath, width, height);
String extension = FilenameUtils.getExtension(thumbnailPath);
if ("svg".equalsIgnoreCase(extension)) {
return false; // 不处理svg图片
}
String storageTypeId = FileStorageTypeProperty.getValue(site.getConfigProps()); String storageTypeId = FileStorageTypeProperty.getValue(site.getConfigProps());
IFileStorageType fst = fileStorageService.getFileStorageType(storageTypeId); IFileStorageType fst = fileStorageService.getFileStorageType(storageTypeId);
FileStorageHelper storageHelper = FileStorageHelper.of(fst, site); FileStorageHelper storageHelper = FileStorageHelper.of(fst, site);
if (storageHelper.exists(thumbnailPath)) { if (storageHelper.exists(thumbnailPath)) {
return; return false;
} }
CmsResource resource = this.getById(internalUrl.getId()); CmsResource resource = this.getById(internalUrl.getId());
if (Objects.nonNull(resource)) { if (Objects.isNull(resource)) {
return false;
}
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
try (InputStream is = storageHelper.read(resource.getPath())) { try (InputStream is = storageHelper.read(resource.getPath())) {
String format = FileExUtils.getExtension(resource.getPath()); String format = FileExUtils.getExtension(resource.getPath());
@ -400,7 +408,7 @@ public class ResourceServiceImpl extends ServiceImpl<CmsResourceMapper, CmsResou
} }
storageHelper.write(thumbnailPath, os.toByteArray()); storageHelper.write(thumbnailPath, os.toByteArray());
} }
} return true;
} }
@Override @Override
@ -430,9 +438,15 @@ public class ResourceServiceImpl extends ServiceImpl<CmsResourceMapper, CmsResou
InternalURL internalURL = InternalUrlUtils.parseInternalUrl(iurl); InternalURL internalURL = InternalUrlUtils.parseInternalUrl(iurl);
if (Objects.nonNull(internalURL)) { if (Objects.nonNull(internalURL)) {
// 先检查是否存在缩略图如果不存在需要生成 // 先检查是否存在缩略图如果不存在需要生成
createThumbnailIfNotExists(internalURL, w, h); boolean res = createThumbnailIfNotExists(internalURL, w, h);
String actualPreviewUrl = InternalUrlUtils.getActualPreviewUrl(internalURL);
if (res) {
// 返回缩略图路径 // 返回缩略图路径
return ImageUtils.getThumbnailFileName(InternalUrlUtils.getActualPreviewUrl(internalURL), w, h); return ImageUtils.getThumbnailFileName(InternalUrlUtils.getActualPreviewUrl(internalURL), w, h);
} else {
// 返回源图路径
return actualPreviewUrl;
}
} }
// 非内部链接返回原路径 // 非内部链接返回原路径
return iurl; return iurl;

View File

@ -102,8 +102,10 @@ public class ImageSizeFunction extends AbstractFunc {
return thumbnailUrl; return thumbnailUrl;
} }
try { try {
resourceService.createThumbnailIfNotExists(internalUrl, width, height); if (resourceService.createThumbnailIfNotExists(internalUrl, width, height)) {
return thumbnailUrl; return thumbnailUrl;
}
return originalUrl;
} catch (Exception e) { } catch (Exception e) {
log.warn("Generate thumbnail failed: " + originalUrl, e); log.warn("Generate thumbnail failed: " + originalUrl, e);
} }