mirror of
https://gitee.com/liweiyi/ChestnutCMS.git
synced 2025-12-06 08:28:23 +08:00
修正svg图片上传资源库报错问题
This commit is contained in:
parent
a3261f18cb
commit
36c3b3b320
@ -87,6 +87,10 @@ public class ResourceType_Image implements IResourceType {
|
||||
|
||||
@Override
|
||||
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());
|
||||
boolean needWatermark = needWatermark(site);
|
||||
boolean needThumbnail = needThumbnail(site);
|
||||
@ -112,63 +116,6 @@ public class ResourceType_Image implements IResourceType {
|
||||
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) {
|
||||
if (!ImageWatermarkProperty.getValue(site.getConfigProps())) {
|
||||
return false;
|
||||
|
||||
@ -106,7 +106,7 @@ public interface IResourceService extends IService<CmsResource> {
|
||||
* @param width 缩略图宽度
|
||||
* @param height 缩略图高度
|
||||
*/
|
||||
void createThumbnailIfNotExists(InternalURL internalURL, int width, int height) throws Exception;
|
||||
boolean createThumbnailIfNotExists(InternalURL internalURL, int width, int height) throws Exception;
|
||||
|
||||
/**
|
||||
* 处理默认缩略图
|
||||
|
||||
@ -45,6 +45,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -374,25 +375,32 @@ public class ResourceServiceImpl extends ServiceImpl<CmsResourceMapper, CmsResou
|
||||
}
|
||||
|
||||
@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())
|
||||
|| !ResourceType_Image.isImage(internalUrl.getPath())) {
|
||||
return; // 非图片资源忽略
|
||||
return false; // 非图片资源忽略
|
||||
}
|
||||
|
||||
Long siteId = MapUtils.getLong(internalUrl.getParams(), InternalDataType_Resource.InternalUrl_Param_SiteId);
|
||||
CmsSite site = siteService.getSite(siteId);
|
||||
|
||||
String filePath = internalUrl.getPath();
|
||||
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());
|
||||
IFileStorageType fst = fileStorageService.getFileStorageType(storageTypeId);
|
||||
FileStorageHelper storageHelper = FileStorageHelper.of(fst, site);
|
||||
if (storageHelper.exists(thumbnailPath)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
CmsResource resource = this.getById(internalUrl.getId());
|
||||
if (Objects.nonNull(resource)) {
|
||||
if (Objects.isNull(resource)) {
|
||||
return false;
|
||||
}
|
||||
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
|
||||
try (InputStream is = storageHelper.read(resource.getPath())) {
|
||||
String format = FileExUtils.getExtension(resource.getPath());
|
||||
@ -400,7 +408,7 @@ public class ResourceServiceImpl extends ServiceImpl<CmsResourceMapper, CmsResou
|
||||
}
|
||||
storageHelper.write(thumbnailPath, os.toByteArray());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -430,9 +438,15 @@ public class ResourceServiceImpl extends ServiceImpl<CmsResourceMapper, CmsResou
|
||||
InternalURL internalURL = InternalUrlUtils.parseInternalUrl(iurl);
|
||||
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);
|
||||
} else {
|
||||
// 返回源图路径
|
||||
return actualPreviewUrl;
|
||||
}
|
||||
}
|
||||
// 非内部链接返回原路径
|
||||
return iurl;
|
||||
|
||||
@ -102,8 +102,10 @@ public class ImageSizeFunction extends AbstractFunc {
|
||||
return thumbnailUrl;
|
||||
}
|
||||
try {
|
||||
resourceService.createThumbnailIfNotExists(internalUrl, width, height);
|
||||
if (resourceService.createThumbnailIfNotExists(internalUrl, width, height)) {
|
||||
return thumbnailUrl;
|
||||
}
|
||||
return originalUrl;
|
||||
} catch (Exception e) {
|
||||
log.warn("Generate thumbnail failed: " + originalUrl, e);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user