账户登录

This commit is contained in:
张锐 committed 2026-11-24 14:04:54 +08:00
1 parent 8c18fd9088
commit fc53e42592
1 file changed
+83 -1
@@ -1,5 +1,87 @@
package com.shoplix.admin.service; package com.shoplix.admin.service;
import com.shoplix.admin.modules.media.res.MediaUploadAdminRes; import com.shoplix.common.code.enums.BusinessCodePrefixEnum; import com.shoplix.common.exception.BusinessException; import com.shoplix.common.utils.BusinessCodeUtils; import com.shoplix.modules.media.repository.entity.MediaAssetEntity; import com.shoplix.modules.media.repository.service.MediaAssetRepositoryService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.shoplix.admin.modules.media.res.MediaUploadAdminRes; import com.shoplix.common.code.enums.BusinessCodePrefixEnum; import com.shoplix.common.exception.BusinessException; import com.shoplix.common.utils.BusinessCodeUtils; import com.shoplix.modules.media.repository.entity.MediaAssetEntity; import com.shoplix.modules.media.repository.service.MediaAssetRepositoryService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.nio.file.*; import java.util.*; import java.io.IOException; import java.nio.file.*; import java.util.*;
/** 运营后台媒体上传业务。 */ @Service public class AdminMediaService {private static final long MAX_IMAGE_SIZE=10*1024*1024;private final MediaAssetRepositoryService repository;private final BusinessCodeUtils codes;private final Path uploadRoot;/** 注入媒体依赖。 */ public AdminMediaService(MediaAssetRepositoryService repository,BusinessCodeUtils codes,@Value("${shoplix.media.upload-dir:./uploads}") String uploadDir){this.repository=repository;this.codes=codes;this.uploadRoot=Paths.get(uploadDir).toAbsolutePath().normalize();} /** 校验并保存产品图片。 */ public MediaUploadAdminRes uploadImage(String siteCode,String altText,MultipartFile file){if(file.isEmpty()||file.getSize()>MAX_IMAGE_SIZE)throw new BusinessException("MEDIA_SIZE_INVALID","图片不能为空且不能超过10MB");String original=Objects.requireNonNullElse(file.getOriginalFilename(),"image");String extension=extension(original);String detectedType=detectType(file);String storage=UUID.randomUUID().toString().replace("-","")+"."+extension;try{Files.createDirectories(uploadRoot);Path target=uploadRoot.resolve(storage).normalize();if(!target.startsWith(uploadRoot))throw new BusinessException("MEDIA_PATH_INVALID","文件路径不合法");file.transferTo(target);}catch(IOException e){throw new BusinessException("MEDIA_UPLOAD_FAILED","图片上传失败");}MediaAssetEntity entity=new MediaAssetEntity();entity.setMediaCode(codes.generate(BusinessCodePrefixEnum.MEDIA));entity.setSiteCode(siteCode);entity.setMediaType("IMAGE");entity.setOriginalFileName(original);entity.setStorageFileName(storage);entity.setRelativeUrl("/uploads/"+storage);entity.setMimeType(detectedType);entity.setFileExtension(extension);entity.setFileSize(file.getSize());entity.setAltText(altText);entity.setStatus("ENABLED");if(repository.insert(entity)!=1)throw new BusinessException("MEDIA_SAVE_FAILED","媒体记录保存失败");return new MediaUploadAdminRes(entity.getMediaCode(),entity.getRelativeUrl(),original,file.getSize());} /** 根据文件签名识别真实图片类型。 */ private String detectType(MultipartFile file){try{byte[] bytes=file.getBytes();if(bytes.length>=8&&bytes[0]==(byte)0x89&&bytes[1]==0x50&&bytes[2]==0x4E&&bytes[3]==0x47)return "image/png";if(bytes.length>=3&&bytes[0]==(byte)0xFF&&bytes[1]==(byte)0xD8&&bytes[2]==(byte)0xFF)return "image/jpeg";if(bytes.length>=6&&bytes[0]=='G'&&bytes[1]=='I'&&bytes[2]=='F')return "image/gif";if(bytes.length>=12&&bytes[0]=='R'&&bytes[1]=='I'&&bytes[2]=='F'&&bytes[8]=='W'&&bytes[9]=='E'&&bytes[10]=='B'&&bytes[11]=='P')return "image/webp";}catch(IOException e){throw new BusinessException("MEDIA_READ_FAILED","图片读取失败");}throw new BusinessException("MEDIA_TYPE_INVALID","文件内容不是支持的图片格式");} /** 提取并校验文件扩展名。 */ private String extension(String name){int index=name.lastIndexOf('.');if(index<0)return "jpg";String value=name.substring(index+1).toLowerCase(Locale.ROOT);if(!Set.of("jpg","jpeg","png","webp","gif").contains(value))throw new BusinessException("MEDIA_EXTENSION_INVALID","图片扩展名不支持");return value;}} /** 运营后台媒体上传业务。 */ @Service public class AdminMediaService {
private static final long MAX_IMAGE_SIZE = 10 * 1024 * 1024;
private final MediaAssetRepositoryService repository;
private final BusinessCodeUtils codes;
private final Path uploadRoot;
/**
* 注入媒体依赖。
*/
public AdminMediaService(MediaAssetRepositoryService repository, BusinessCodeUtils codes, @Value("${shoplix.media.upload-dir:./uploads}") String uploadDir) {
this.repository = repository;
this.codes = codes;
this.uploadRoot = Paths.get(uploadDir).toAbsolutePath().normalize();
}
/**
* 校验并保存产品图片。
*/
public MediaUploadAdminRes uploadImage(String siteCode, String altText, MultipartFile file) {
if (file.isEmpty() || file.getSize() > MAX_IMAGE_SIZE)
throw new BusinessException("MEDIA_SIZE_INVALID", "图片不能为空且不能超过10MB");
String original = Objects.requireNonNullElse(file.getOriginalFilename(), "image");
String extension = extension(original);
String detectedType = detectType(file);
String storage = UUID.randomUUID().toString().replace("-", "") + "." + extension;
try {
Files.createDirectories(uploadRoot);
Path target = uploadRoot.resolve(storage).normalize();
if (!target.startsWith(uploadRoot))
throw new BusinessException("MEDIA_PATH_INVALID", "文件路径不合法");
file.transferTo(target);
} catch (IOException e) {
throw new BusinessException("MEDIA_UPLOAD_FAILED", "图片上传失败");
}
MediaAssetEntity entity = new MediaAssetEntity();
entity.setMediaCode(codes.generate(BusinessCodePrefixEnum.MEDIA));
entity.setSiteCode(siteCode);
entity.setMediaType("IMAGE");
entity.setOriginalFileName(original);
entity.setStorageFileName(storage);
entity.setRelativeUrl("/uploads/" + storage);
entity.setMimeType(detectedType);
entity.setFileExtension(extension);
entity.setFileSize(file.getSize());
entity.setAltText(altText);
entity.setStatus("ENABLED");
if (repository.insert(entity) != 1)
throw new BusinessException("MEDIA_SAVE_FAILED", "媒体记录保存失败");
return new MediaUploadAdminRes(entity.getMediaCode(), entity.getRelativeUrl(), original, file.getSize());
}
/**
* 根据文件签名识别真实图片类型。
*/
private String detectType(MultipartFile file) {
try {
byte[] bytes = file.getBytes();
if (bytes.length >= 8 && bytes[0] == (byte) 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4E && bytes[3] == 0x47)
return "image/png";
if (bytes.length >= 3 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xD8 && bytes[2] == (byte) 0xFF)
return "image/jpeg";
if (bytes.length >= 6 && bytes[0] == 'G' && bytes[1] == 'I' && bytes[2] == 'F')
return "image/gif";
if (bytes.length >= 12 && bytes[0] == 'R' && bytes[1] == 'I' && bytes[2] == 'F' && bytes[8] == 'W' && bytes[9] == 'E' && bytes[10] == 'B' && bytes[11] == 'P')
return "image/webp";
} catch (IOException e) {
throw new BusinessException("MEDIA_READ_FAILED", "图片读取失败");
}
throw new BusinessException("MEDIA_TYPE_INVALID", "文件内容不是支持的图片格式");
}
/**
* 提取并校验文件扩展名。
*/
private String extension(String name) {
int index = name.lastIndexOf('.');
if (index < 0) return "jpg";
String value = name.substring(index + 1).toLowerCase(Locale.ROOT);
if (!Set.of("jpg", "jpeg", "png", "webp", "gif").contains(value))
throw new BusinessException("MEDIA_EXTENSION_INVALID", "图片扩展名不支持");
return value;
}
}