利用minio 进行word转PDF
@Override
public CommonResult<String> wordToPdf(String minioPath, String from) {
try {
if(lock(minioPath)){
S3FileClient fileClient = new S3FileClient(1L, s3FileClientConfig);
fileClient.init();
byte[] content = fileClient.getContent(minioPath);
File tmpFile = File.createTempFile("pdf", ".pdf");
documentConverter.convert(new ByteArrayInputStream(content)).as(DefaultDocumentFormatRegistry.DOCX).to(tmpFile).as(DefaultDocumentFormatRegistry.PDF).execute();
InputStream in = new FileInputStream(tmpFile);
content = in.readAllBytes();
String originalFilename = tmpFile.getName();
if (Objects.equals(originalFilename, "")) {
throw exception(FILE_NOT_EXISTS);
}
String path = FileUtils.generatePath(content, originalFilename);
String currentDate = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
path = from + "/" + currentDate + "/" + path;
String type = FileTypeUtils.getMineType(content, originalFilename);
in.close();
String url = fileClient.upload(content, path, type);
tmpFile.delete();
return CommonResult.success(url);
}
} catch (Exception e) {
log.error(e.getMessage());
}finally {
unLock(minioPath);
}
return CommonResult.error();
}
@Data
public class S3FileClientConfig implements FileClientConfig {
public static final String ENDPOINT_QINIU = "qiniucs.com";
public static final String ENDPOINT_ALIYUN = "aliyuncs.com";
public static final String ENDPOINT_TENCENT = "myqcloud.com";
@NotNull(message = "endpoint 不能为空")
private String endpoint;
@URL(message = "domain 必须是 URL 格式")
private String domain;
@NotNull(message = "bucket 不能为空")
private String bucket;
@NotNull(message = "accessKey 不能为空")
private String accessKey;
@NotNull(message = "accessSecret 不能为空")
private String accessSecret;
@SuppressWarnings("RedundantIfStatement")
@AssertTrue(message = "domain 不能为空")
@JsonIgnore
public boolean isDomainValid() {
if (StrUtil.contains(endpoint, ENDPOINT_QINIU) && StrUtil.isEmpty(domain)) {
return false;
}
return true;
}
private String address;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
public interface FileClientConfig {
}
public class FileTypeUtils {
private static final ThreadLocal<Tika> TIKA = TransmittableThreadLocal.withInitial(Tika::new);
@SneakyThrows
public static String getMineType(byte[] data) {
return TIKA.get().detect(data);
}
public static String getMineType(String name) {
return TIKA.get().detect(name);
}
public static String getMineType(byte[] data, String name) {
return TIKA.get().detect(data, name);
}
}
public class FileUtils {
@SneakyThrows
public static File createTempFile(String data) {
File file = createTempFile();
FileUtil.writeUtf8String(data, file);
return file;
}
@SneakyThrows
public static File createTempFile(byte[] data) {
File file = createTempFile();
FileUtil.writeBytes(data, file);
return file;
}
@SneakyThrows
public static File createTempFile() {
File file = File.createTempFile(IdUtil.simpleUUID(), null);
file.deleteOnExit();
return file;
}
public static String generatePath(byte[] content, String originalName) {
String sha256Hex = DigestUtil.sha256Hex(content);
if (StrUtil.isNotBlank(originalName)) {
String extName = FileNameUtil.extName(originalName);
return StrUtil.isBlank(extName) ? sha256Hex : sha256Hex + "." + extName;
}
return sha256Hex + '.' + FileTypeUtil.getType(new ByteArrayInputStream(content));
}
}