AsyncFileService
+ *FileStoreService
* @author Cyan (snow22314@outlook.com) * @version v1.0.0 */ -public abstract class AsyncFileService { +public abstract class FileStoreService { @Async abstract public void removeAll(ListFileServiceAutoConfigure
+ * @author Cyan (snow22314@outlook.com) + * @version v1.0.0 + */ +@Slf4j +@Configuration +@ComponentScan(basePackages = {"io.github.nichetoolkit.file"}) +@MapperScan(basePackages = {"io.github.nichetoolkit.file.mapper"}) +public class FileGaussAutoConfigure { + + public FileGaussAutoConfigure() { + log.debug("================= file-gauss-auto-config initiated ! ==================="); + } +} diff --git a/file-toolkit-gauss-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileChunkMapper.xml b/file-toolkit-gauss-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileChunkMapper.xml new file mode 100644 index 0000000..4973bbc --- /dev/null +++ b/file-toolkit-gauss-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileChunkMapper.xml @@ -0,0 +1,238 @@ + + +FileMinioStarterAutoConfigure
@@ -16,8 +15,6 @@ */ @Slf4j @Configuration -@org.mybatis.spring.annotation.MapperScan("io.github.nichetoolkit.file.mapper") -@MapperScan("io.github.nichetoolkit.file.mapper") @ComponentScan(basePackages = {"io.github.nichetoolkit.file"}) public class FileMinioStarterAutoConfigure { public FileMinioStarterAutoConfigure() { diff --git a/file-toolkit-minio-starter/src/main/java/io/github/nichetoolkit/file/service/impl/MinioFileService.java b/file-toolkit-minio-starter/src/main/java/io/github/nichetoolkit/file/service/MinioFileService.java similarity index 88% rename from file-toolkit-minio-starter/src/main/java/io/github/nichetoolkit/file/service/impl/MinioFileService.java rename to file-toolkit-minio-starter/src/main/java/io/github/nichetoolkit/file/service/MinioFileService.java index ed31971..6aec1a0 100644 --- a/file-toolkit-minio-starter/src/main/java/io/github/nichetoolkit/file/service/impl/MinioFileService.java +++ b/file-toolkit-minio-starter/src/main/java/io/github/nichetoolkit/file/service/MinioFileService.java @@ -1,7 +1,7 @@ -package io.github.nichetoolkit.file.service.impl; +package io.github.nichetoolkit.file.service; +import io.github.nichetoolkit.file.handle.FileStoreService; import io.github.nichetoolkit.file.minio.MinioUtils; -import io.github.nichetoolkit.file.service.AsyncFileService; import io.github.nichetoolkit.rest.RestException; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -16,7 +16,7 @@ * @version v1.0.0 */ @Service -public class MinioFileService extends AsyncFileService { +public class MinioFileService extends FileStoreService { @Override @Async public void removeAll(ListFilePostgresAutoConfigure
+ * @author Cyan (snow22314@outlook.com) + * @version v1.0.0 + */ +@Slf4j +@Configuration +@ComponentScan(basePackages = {"io.github.nichetoolkit.file"}) +@MapperScan(basePackages = {"io.github.nichetoolkit.file.mapper"}) +public class FilePostgresAutoConfigure { + + public FilePostgresAutoConfigure() { + log.debug("================= file-postgres-auto-config initiated ! ==================="); + } +} diff --git a/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileChunkMapper.xml b/file-toolkit-postgres-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileChunkMapper.xml similarity index 100% rename from file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileChunkMapper.xml rename to file-toolkit-postgres-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileChunkMapper.xml diff --git a/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileIndexMapper.xml b/file-toolkit-postgres-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileIndexMapper.xml similarity index 100% rename from file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileIndexMapper.xml rename to file-toolkit-postgres-starter/src/main/java/io/github/nichetoolkit/file/mapper/FileIndexMapper.xml diff --git a/file-toolkit-postgres-starter/src/main/resources/META-INF/spring.factories b/file-toolkit-postgres-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..88fc3b0 --- /dev/null +++ b/file-toolkit-postgres-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=io.github.nichetoolkit.file.configure.FilePostgresAutoConfigure \ No newline at end of file diff --git a/file-toolkit-service-starter/pom.xml b/file-toolkit-service-starter/pom.xml index d18bb5d..3777a34 100644 --- a/file-toolkit-service-starter/pom.xml +++ b/file-toolkit-service-starter/pom.xml @@ -5,11 +5,11 @@FileHandleHelper
+ * @author Cyan (snow22314@outlook.com) + * @version v1.0.0 + */ +@Slf4j +@Component +public class FileHandleHelper implements InitializingBean { + + @Resource(type = FileStoreService.class) + private FileStoreService fileStoreService; + + private static FileHandleHelper INSTANCE = null; + + public static FileHandleHelper getInstance() { + return INSTANCE; + } + + @Override + public void afterPropertiesSet() throws Exception { + INSTANCE = this; + } + + public static FileStoreService fileStoreService() { + return INSTANCE.fileStoreService; + } + + public static void writeFile(String objectName, String itemFilePath) throws RestException { + try (InputStream inputStream = INSTANCE.fileStoreService.getById(objectName)) { + StreamUtils.write(itemFilePath, inputStream); + } catch (IOException exception) { + log.error("the file service download has error: {}", exception.getMessage()); + throw new FileErrorException(FileErrorStatus.SERVICE_DOWNLOAD_ERROR); + } + } +} diff --git a/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/helper/FileServiceHelper.java b/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/helper/FileServiceHelper.java index 00fa310..b34209f 100644 --- a/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/helper/FileServiceHelper.java +++ b/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/helper/FileServiceHelper.java @@ -5,23 +5,20 @@ import io.github.nichetoolkit.file.entity.FileIndexEntity; import io.github.nichetoolkit.file.enums.FileType; import io.github.nichetoolkit.file.error.FileErrorStatus; -import io.github.nichetoolkit.file.error.ImageTransferException; import io.github.nichetoolkit.file.filter.FileFilter; import io.github.nichetoolkit.file.model.FileChunk; import io.github.nichetoolkit.file.model.FileIndex; import io.github.nichetoolkit.file.model.FileRequest; -import io.github.nichetoolkit.file.service.AsyncFileService; import io.github.nichetoolkit.file.service.FileChunkService; import io.github.nichetoolkit.file.service.FileIndexService; -import io.github.nichetoolkit.file.util.ImageUtils; import io.github.nichetoolkit.file.util.Md5Utils; import io.github.nichetoolkit.rest.RestException; import io.github.nichetoolkit.rest.error.natives.FileErrorException; +import io.github.nichetoolkit.rest.identity.IdentityUtils; import io.github.nichetoolkit.rest.util.*; import io.github.nichetoolkit.rice.RestPage; import io.github.nichetoolkit.rice.helper.PropertyHelper; import lombok.extern.slf4j.Slf4j; -import net.coobird.thumbnailator.Thumbnails; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; @@ -29,10 +26,6 @@ import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; -import javax.annotation.PostConstruct; -import javax.naming.NamingEnumeration; -import javax.swing.*; -import java.awt.image.BufferedImage; import java.io.*; import java.util.*; import java.util.stream.Collectors; @@ -47,7 +40,7 @@ public class FileServiceHelper implements InitializingBean { @Autowired - private FileCommonProperties serviceProperties; + private FileCommonProperties commonProperties; @Autowired private FileIndexService fileIndexService; @@ -55,8 +48,6 @@ public class FileServiceHelper implements InitializingBean { @Autowired private FileChunkService fileChunkService; - private AsyncFileService asyncFileService; - private static FileServiceHelper INSTANCE = null; public static FileServiceHelper getInstance() { @@ -68,11 +59,6 @@ public void afterPropertiesSet() throws Exception { INSTANCE = this; } - @PostConstruct - public void InitFileSupperService() { - asyncFileService = ContextUtils.getBean(AsyncFileService.class); - } - public static void checkRestPage(RestPage restPage) throws RestException { if (GeneralUtils.isEmpty(restPage) || GeneralUtils.isEmpty(restPage.getItems())) { log.warn("the file service query result is empty!"); @@ -80,93 +66,6 @@ public static void checkRestPage(RestPage restPage) throws RestException { } } - public static void condenseFile(String randomPath, FileIndex fileIndex) throws RestException { - String filename = fileIndex.getName(); - String zipFilename = fileIndex.getFilename().concat(FileConstants.SUFFIX_REGEX).concat(FileConstants.FILE_ZIP_SUFFIX); - String filePath = randomPath.concat(File.separator).concat(filename); - File file = FileUtils.createFile(filePath); - StreamUtils.write(file, fileIndex.inputStream()); - File zipFile = ZipUtils.zipFile(randomPath, zipFilename, file); - buildProperties(zipFilename, zipFile.length(), FileConstants.FILE_ZIP_SUFFIX, fileIndex); - if (fileIndex.getIsMd5()) { - buildMd5(zipFile, fileIndex); - } - } - - public static void autographImage(String randomPath, FileIndex fileIndex) throws RestException { - InputStream inputStream = fileIndex.inputStream(); - BufferedImage bufferedImage = ImageUtils.read(inputStream); - BufferedImage binaryImage = ImageUtils.binaryImage(bufferedImage); - BufferedImage autographImage = ImageUtils.autograph(binaryImage); - String filename = fileIndex.getFilename().concat(FileConstants.SUFFIX_REGEX).concat(FileConstants.IMAGE_PNG_SUFFIX); - String filePath = randomPath.concat(File.separator).concat(filename); - File file = new File(filePath); - if (file.exists()) { - FileUtils.delete(filePath); - } - ImageUtils.write(autographImage, file); - byte[] bytes = ImageUtils.bytes(file); - fileIndex.setBytes(bytes); - FileUtils.delete(filePath); - } - - public static void condenseImage(String randomPath, FileIndex fileIndex) throws RestException { - Long imageFileSize; - Double imageFileQuality = 1.0d; - Double imageFileScale = 1.0d; - String filename = fileIndex.getFilename().concat(FileConstants.SUFFIX_REGEX).concat(FileConstants.IMAGE_PNG_SUFFIX); - String filePath; - File file; - Integer width = fileIndex.getWidth(); - Integer height = fileIndex.getHeight(); - filePath = randomPath.concat(File.separator).concat(filename); - file = new File(filePath); - do { - if (file.exists()) { - FileUtils.delete(filePath); - } - try { - Thumbnails.of(fileIndex.inputStream()).scale(imageFileScale).outputFormat(FileConstants.IMAGE_PNG_SUFFIX).outputQuality(imageFileQuality).toFile(filePath); - BufferedImage bufferedImage = ImageHelper.read(file); - int imageWidth = bufferedImage.getWidth(); - int imageHeight = bufferedImage.getHeight(); - if (GeneralUtils.isNotEmpty(width) && GeneralUtils.isNotEmpty(height)) { - Thumbnails.of(fileIndex.inputStream()).size(width, height).outputFormat(FileConstants.IMAGE_PNG_SUFFIX).outputQuality(imageFileQuality).toFile(filePath); - } else if (GeneralUtils.isNotEmpty(width) || GeneralUtils.isNotEmpty(height)) { - if (GeneralUtils.isNotEmpty(width)) { - imageFileScale = ((double) width / (double) imageWidth >= 1.0D) ? imageFileScale : ((double) width / (double) imageWidth); - } else { - imageFileScale = ((double) height / (double) imageHeight >= 1.0D) ? imageFileScale : ((double) height / (double) imageHeight); - } - Thumbnails.of(fileIndex.inputStream()).scale(imageFileScale).outputFormat(FileConstants.IMAGE_PNG_SUFFIX).outputQuality(imageFileQuality).toFile(filePath); - } else { - Thumbnails.of(fileIndex.inputStream()).scale(imageFileScale).outputFormat(FileConstants.IMAGE_PNG_SUFFIX).outputQuality(imageFileQuality).toFile(filePath); - } - } catch (IOException exception) { - log.error("the image file has error during condensing: {}", exception.getMessage()); - throw new FileErrorException(FileErrorStatus.FILE_IMAGE_CONDENSE_ERROR); - } - imageFileSize = file.length(); - if (imageFileQuality.equals(INSTANCE.serviceProperties.getMinImageQuality())) { - imageFileScale += -0.1d; - } else { - imageFileQuality += -0.1d; - } - } while (imageFileSize > INSTANCE.serviceProperties.getMaxImageSize() - && imageFileQuality > INSTANCE.serviceProperties.getMinImageQuality() - && imageFileScale > INSTANCE.serviceProperties.getMinImageScale()); - if (GeneralUtils.isNotEmpty(width) && GeneralUtils.isNotEmpty(height)) { - fileIndex.addProperty(FileConstants.IMAGE_CONDENSE_WIDTH_PROPERTY, width); - fileIndex.addProperty(FileConstants.IMAGE_CONDENSE_HEIGHT_PROPERTY, height); - } else { - fileIndex.addProperty(FileConstants.IMAGE_CONDENSE_SCALE_PROPERTY, imageFileScale); - } - fileIndex.addProperty(FileConstants.IMAGE_CONDENSE_QUALITY_PROPERTY, imageFileQuality); - buildProperties(filename, file.length(), FileConstants.IMAGE_PNG_SUFFIX, fileIndex); - buildMd5(file, fileIndex); - FileUtils.clearFile(randomPath); - } - public static void buildProperties(String filename, long size, String suffix, FileIndex fileIndex) { fileIndex.addProperty(FileConstants.ORIGINAL_SUFFIX_PROPERTY, fileIndex.getSuffix()); fileIndex.setSuffix(suffix); @@ -187,7 +86,7 @@ public static void buildChunkFiles(ListFileHandleService
+ * @author Cyan (snow22314@outlook.com) + * @version v1.0.0 + */ +public interface FileHandleService { + + void autographImage(FileIndex fileIndex) throws RestException; + + void condenseImage(FileIndex fileIndex) throws RestException; + + void condenseFile(FileIndex fileIndex) throws RestException; +} diff --git a/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/service/FileIndexService.java b/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/service/FileIndexService.java index 3fe0c02..b31c2a8 100644 --- a/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/service/FileIndexService.java +++ b/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/service/FileIndexService.java @@ -12,7 +12,7 @@ * @author Cyan (snow22314@outlook.com) * @version v1.0.0 */ -public interface FileIndexService extends FilterServiceFileHandleServiceImpl
+ * @author Cyan (snow22314@outlook.com) + * @version v1.0.0 + */ +@Slf4j +@Service +public class FileHandleServiceImpl implements FileHandleService { + + @Autowired + protected FileCommonProperties commonProperties; + @Autowired + private FileCommonProperties serviceProperties; + + @Async + @Override + public void autographImage(FileIndex fileIndex) throws RestException { + String tempPath = FileUtils.createPath(commonProperties.getTempPath()); + String cachePath = FileUtils.createPath(tempPath, fileIndex.getId()); + String randomPath = FileUtils.createPath(cachePath, GeneralUtils.uuid()); + InputStream inputStream = fileIndex.inputStream(); + BufferedImage bufferedImage = ImageUtils.read(inputStream); + BufferedImage binaryImage = ImageUtils.binaryImage(bufferedImage); + BufferedImage autographImage = ImageUtils.autograph(binaryImage); + String filename = fileIndex.getFilename().concat(FileConstants.SUFFIX_REGEX).concat(FileConstants.IMAGE_PNG_SUFFIX); + String filePath = randomPath.concat(File.separator).concat(filename); + File file = new File(filePath); + if (file.exists()) { + FileUtils.delete(filePath); + } + ImageUtils.write(autographImage, file); + byte[] bytes = ImageUtils.bytes(file); + fileIndex.setBytes(bytes); + FileUtils.delete(filePath); + FileUtils.clear(randomPath); + FileUtils.clear(cachePath); + } + + @Async + @Override + public void condenseImage(FileIndex fileIndex) throws RestException { + String tempPath = FileUtils.createPath(commonProperties.getTempPath()); + String cachePath = FileUtils.createPath(tempPath, fileIndex.getId()); + String randomPath = FileUtils.createPath(cachePath, GeneralUtils.uuid()); + Long imageFileSize; + Double imageFileQuality = 1.0d; + Double imageFileScale = 1.0d; + String filename = fileIndex.getFilename().concat(FileConstants.SUFFIX_REGEX).concat(FileConstants.IMAGE_PNG_SUFFIX); + String filePath; + File file; + Integer width = fileIndex.getWidth(); + Integer height = fileIndex.getHeight(); + filePath = randomPath.concat(File.separator).concat(filename); + file = new File(filePath); + do { + if (file.exists()) { + FileUtils.delete(filePath); + } + try { + Thumbnails.of(fileIndex.inputStream()).scale(imageFileScale).outputFormat(FileConstants.IMAGE_PNG_SUFFIX).outputQuality(imageFileQuality).toFile(filePath); + BufferedImage bufferedImage = ImageHandleHelper.read(file); + int imageWidth = bufferedImage.getWidth(); + int imageHeight = bufferedImage.getHeight(); + if (GeneralUtils.isNotEmpty(width) && GeneralUtils.isNotEmpty(height)) { + Thumbnails.of(fileIndex.inputStream()).size(width, height).outputFormat(FileConstants.IMAGE_PNG_SUFFIX).outputQuality(imageFileQuality).toFile(filePath); + } else if (GeneralUtils.isNotEmpty(width) || GeneralUtils.isNotEmpty(height)) { + if (GeneralUtils.isNotEmpty(width)) { + imageFileScale = ((double) width / (double) imageWidth >= 1.0D) ? imageFileScale : ((double) width / (double) imageWidth); + } else { + imageFileScale = ((double) height / (double) imageHeight >= 1.0D) ? imageFileScale : ((double) height / (double) imageHeight); + } + Thumbnails.of(fileIndex.inputStream()).scale(imageFileScale).outputFormat(FileConstants.IMAGE_PNG_SUFFIX).outputQuality(imageFileQuality).toFile(filePath); + } else { + Thumbnails.of(fileIndex.inputStream()).scale(imageFileScale).outputFormat(FileConstants.IMAGE_PNG_SUFFIX).outputQuality(imageFileQuality).toFile(filePath); + } + } catch (IOException exception) { + log.error("the image file has error during condensing: {}", exception.getMessage()); + throw new FileErrorException(FileErrorStatus.FILE_IMAGE_CONDENSE_ERROR); + } + imageFileSize = file.length(); + if (imageFileQuality.equals(serviceProperties.getMinImageQuality())) { + imageFileScale += -0.1d; + } else { + imageFileQuality += -0.1d; + } + } while (imageFileSize > serviceProperties.getMaxImageSize() + && imageFileQuality > serviceProperties.getMinImageQuality() + && imageFileScale > serviceProperties.getMinImageScale()); + if (GeneralUtils.isNotEmpty(width) && GeneralUtils.isNotEmpty(height)) { + fileIndex.addProperty(FileConstants.IMAGE_CONDENSE_WIDTH_PROPERTY, width); + fileIndex.addProperty(FileConstants.IMAGE_CONDENSE_HEIGHT_PROPERTY, height); + } else { + fileIndex.addProperty(FileConstants.IMAGE_CONDENSE_SCALE_PROPERTY, imageFileScale); + } + fileIndex.addProperty(FileConstants.IMAGE_CONDENSE_QUALITY_PROPERTY, imageFileQuality); + FileServiceHelper.buildProperties(filename, file.length(), FileConstants.IMAGE_PNG_SUFFIX, fileIndex); + FileServiceHelper.buildMd5(file, fileIndex); + FileUtils.delete(filePath); + FileUtils.clear(randomPath); + FileUtils.clear(cachePath); + } + + @Async + @Override + public void condenseFile(FileIndex fileIndex) throws RestException { + String tempPath = FileUtils.createPath(commonProperties.getTempPath()); + String cachePath = FileUtils.createPath(tempPath, fileIndex.getId()); + String randomPath = FileUtils.createPath(cachePath, GeneralUtils.uuid()); + String filename = fileIndex.getName(); + String zipFilename = fileIndex.getFilename().concat(FileConstants.SUFFIX_REGEX).concat(FileConstants.FILE_ZIP_SUFFIX); + String filePath = randomPath.concat(File.separator).concat(filename); + File file = FileUtils.createFile(filePath); + StreamUtils.write(file, fileIndex.inputStream()); + File zipFile = ZipUtils.zipFile(randomPath, zipFilename, file); + FileServiceHelper.buildProperties(zipFilename, zipFile.length(), FileConstants.FILE_ZIP_SUFFIX, fileIndex); + if (fileIndex.getIsMd5()) { + FileServiceHelper.buildMd5(zipFile, fileIndex); + } + FileUtils.delete(filePath); + FileUtils.clear(randomPath); + FileUtils.clear(cachePath); + } +} diff --git a/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/service/impl/FileIndexServiceImpl.java b/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/service/impl/FileIndexServiceImpl.java index a597a6e..6be4054 100644 --- a/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/service/impl/FileIndexServiceImpl.java +++ b/file-toolkit-service-starter/src/main/java/io/github/nichetoolkit/file/service/impl/FileIndexServiceImpl.java @@ -1,4 +1,4 @@ -package io.github.nichetoolkit.file.service.impl; +package io.github.nichetoolkit.file.service; import io.github.nichetoolkit.file.entity.FileIndexEntity; import io.github.nichetoolkit.file.filter.FileFilter; @@ -8,6 +8,7 @@ import io.github.nichetoolkit.file.service.FileChunkService; import io.github.nichetoolkit.file.service.FileIndexService; import io.github.nichetoolkit.rest.RestException; +import io.github.nichetoolkit.rest.actuator.BiConsumerActuator; import io.github.nichetoolkit.rest.actuator.ConsumerActuator; import io.github.nichetoolkit.rest.util.GeneralUtils; import io.github.nichetoolkit.rice.RiceInfoService; @@ -35,6 +36,16 @@ public class FileIndexServiceImpl extends RiceInfoService