|
@@ -1,19 +1,24 @@
|
|
package cn.iocoder.yudao.module.museum.service.museumpicture;
|
|
package cn.iocoder.yudao.module.museum.service.museumpicture;
|
|
|
|
|
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
|
+import cn.iocoder.yudao.module.infra.api.file.FileApi;
|
|
import cn.iocoder.yudao.module.museum.controller.admin.museumpicture.vo.MuseumPictureSaveVO;
|
|
import cn.iocoder.yudao.module.museum.controller.admin.museumpicture.vo.MuseumPictureSaveVO;
|
|
import cn.iocoder.yudao.module.museum.controller.admin.museumpicture.vo.PicturePageReqVO;
|
|
import cn.iocoder.yudao.module.museum.controller.admin.museumpicture.vo.PicturePageReqVO;
|
|
import cn.iocoder.yudao.module.museum.dal.database.museumpicture.MuseumPictureDO;
|
|
import cn.iocoder.yudao.module.museum.dal.database.museumpicture.MuseumPictureDO;
|
|
import cn.iocoder.yudao.module.museum.dal.mysql.museumpicture.MuseumPictureMapper;
|
|
import cn.iocoder.yudao.module.museum.dal.mysql.museumpicture.MuseumPictureMapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.util.Set;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipInputStream;
|
|
import java.util.zip.ZipInputStream;
|
|
|
|
|
|
@@ -25,6 +30,9 @@ public class MuseumPictureServiceImpl implements MuseumPictureService {
|
|
@Resource
|
|
@Resource
|
|
private MuseumPictureMapper museumPictureMapper;
|
|
private MuseumPictureMapper museumPictureMapper;
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private FileApi fileApi;
|
|
|
|
+
|
|
// @Override
|
|
// @Override
|
|
// public Integer createMuseumPicture(MuseumPictureSaveVO saveVO){
|
|
// public Integer createMuseumPicture(MuseumPictureSaveVO saveVO){
|
|
// return museumPictureMapper.insert(BeanUtils.toBean(saveVO, MuseumPictureDO.class));
|
|
// return museumPictureMapper.insert(BeanUtils.toBean(saveVO, MuseumPictureDO.class));
|
|
@@ -66,10 +74,93 @@ public class MuseumPictureServiceImpl implements MuseumPictureService {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+// private final Set<String> ALLOWED_EXTENSIONS = Set.of("jpg", "jpeg", "png", "gif");
|
|
|
|
+
|
|
|
|
+// @Override
|
|
|
|
+// public void processZipUpload(MultipartFile file, Long groupId) throws IOException {
|
|
|
|
+// try (ZipInputStream zis = new ZipInputStream(file.getInputStream())) {
|
|
|
|
+// ZipEntry entry;
|
|
|
|
+// while ((entry = zis.getNextEntry()) != null) {
|
|
|
|
+// processZipEntry(zis, entry, groupId);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// private void processZipEntry(ZipInputStream zis, ZipEntry entry, Long groupId) throws IOException {
|
|
|
|
+// if (entry.isDirectory() || !isImageFile(entry.getName())) {
|
|
|
|
+// return;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// // 读取文件内容并上传
|
|
|
|
+// byte[] content = IoUtil.readBytes(zis);
|
|
|
|
+// String url = fileApi.createFile(content);
|
|
|
|
+//
|
|
|
|
+// // 构建并保存数据库记录
|
|
|
|
+// MuseumPictureDO photo = new MuseumPictureDO();
|
|
|
|
+// photo.setGroupId(Math.toIntExact(groupId));
|
|
|
|
+// photo.setPictureUrl(url);
|
|
|
|
+// museumPictureMapper.insert(photo);
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// private boolean isImageFile(String filename) {
|
|
|
|
+// String extension = FilenameUtil.getExtension(filename);
|
|
|
|
+// return StringUtils.isNotEmpty(extension);
|
|
|
|
+//// ALLOWED_EXTENSIONS.contains(extension.toLowerCase());
|
|
|
|
+// }
|
|
|
|
+
|
|
// @Override
|
|
// @Override
|
|
// public MuseumPictureDO selectMuseumPictureSet(Integer group_id){
|
|
// public MuseumPictureDO selectMuseumPictureSet(Integer group_id){
|
|
// return museumPictureMapper.selectMuseumPictureSetById(group_id);
|
|
// return museumPictureMapper.selectMuseumPictureSetById(group_id);
|
|
// }
|
|
// }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void processZipUpload(MultipartFile file, Long groupId) {
|
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
|
+ throw new IllegalArgumentException("上传的文件不能为空");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (InputStream inputStream = file.getInputStream();
|
|
|
|
+ ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
|
|
|
|
+
|
|
|
|
+ ZipEntry entry;
|
|
|
|
+ while ((entry = zipInputStream.getNextEntry()) != null) {
|
|
|
|
+ if (!entry.isDirectory() && isImageFile(entry.getName())) {
|
|
|
|
+ // 使用 ByteArrayOutputStream 读取当前条目的内容
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
|
+ int bytesRead;
|
|
|
|
+ while ((bytesRead = zipInputStream.read(buffer)) != -1) {
|
|
|
|
+ baos.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ byte[] content = baos.toByteArray();
|
|
|
|
+
|
|
|
|
+ // 上传文件并获取 URL
|
|
|
|
+ String url = fileApi.createFile(content);
|
|
|
|
+ // 保存到数据库
|
|
|
|
+ savePhotoToDatabase(groupId, url);
|
|
|
|
+ }
|
|
|
|
+ // 关闭当前条目
|
|
|
|
+ zipInputStream.closeEntry();
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException("处理压缩包时发生错误", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean isImageFile(String filename) {
|
|
|
|
+ // 简单检查文件扩展名是否为图片格式
|
|
|
|
+ String extension = StrUtil.subAfter(filename, '.', true).toLowerCase();
|
|
|
|
+ return extension != null && ("jpg,jpeg,png,gif,bmp".contains(extension));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void savePhotoToDatabase(Long groupId, String url) {
|
|
|
|
+ MuseumPictureDO photo = new MuseumPictureDO();
|
|
|
|
+ photo.setGroupId(Math.toIntExact(groupId));
|
|
|
|
+ photo.setPictureUrl(url);
|
|
|
|
+ museumPictureMapper.insert(photo);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// @Override
|
|
// @Override
|