|
@@ -3,15 +3,16 @@ package com.qmth.boot.core.fss.store.impl;
|
|
|
import com.qmth.boot.core.exception.NotFoundException;
|
|
|
import com.qmth.boot.core.fss.store.FileStore;
|
|
|
import com.qmth.boot.core.fss.utils.FssSigner;
|
|
|
+import com.qmth.boot.tools.crypto.AES;
|
|
|
import com.qmth.boot.tools.io.IOUtils;
|
|
|
import com.qmth.boot.tools.models.ByteArray;
|
|
|
import com.qmth.boot.tools.uuid.FastUUID;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.InputStream;
|
|
|
+import javax.crypto.CipherInputStream;
|
|
|
+import javax.crypto.CipherOutputStream;
|
|
|
+import java.io.*;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.time.Duration;
|
|
|
|
|
|
/**
|
|
@@ -27,8 +28,11 @@ public class DiskStore implements FileStore {
|
|
|
|
|
|
private FssSigner fssSigner;
|
|
|
|
|
|
- public DiskStore(String rootPath, String server, String temp, FssSigner fssSigner) {
|
|
|
+ private String cipherKey;
|
|
|
+
|
|
|
+ public DiskStore(String rootPath, String server, String temp, FssSigner fssSigner, String cipherKey) {
|
|
|
this.fssSigner = fssSigner;
|
|
|
+ this.cipherKey = cipherKey;
|
|
|
this.server = server;
|
|
|
this.rootDir = new File(rootPath);
|
|
|
this.tempDir = new File(temp);
|
|
@@ -95,8 +99,17 @@ public class DiskStore implements FileStore {
|
|
|
throw new RuntimeException("Target file is directory: " + path);
|
|
|
}
|
|
|
targetFile.getParentFile().mkdirs();
|
|
|
+ //加密模式下,正式文件写入流需要加密
|
|
|
+ if (cipherKey != null) {
|
|
|
+ OutputStream outputStream = new CipherOutputStream(new FileOutputStream(targetFile),
|
|
|
+ AES.getEncryptCipher(cipherKey));
|
|
|
+ Files.copy(tempFile.toPath(), outputStream);
|
|
|
+ IOUtils.closeQuietly(outputStream);
|
|
|
+ }
|
|
|
//临时文件拷贝到正式文件
|
|
|
- IOUtils.copy(tempFile, targetFile);
|
|
|
+ else {
|
|
|
+ IOUtils.copy(tempFile, targetFile);
|
|
|
+ }
|
|
|
} finally {
|
|
|
tempFile.delete();
|
|
|
}
|
|
@@ -107,7 +120,11 @@ public class DiskStore implements FileStore {
|
|
|
path = formatPath(path);
|
|
|
File file = new File(rootDir, path);
|
|
|
if (file.exists() && file.isFile()) {
|
|
|
- return new FileInputStream(file);
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+ if (cipherKey != null) {
|
|
|
+ inputStream = new CipherInputStream(inputStream, AES.getDecryptCipher(cipherKey));
|
|
|
+ }
|
|
|
+ return inputStream;
|
|
|
} else {
|
|
|
throw new NotFoundException("Read file unexist:" + path);
|
|
|
}
|
|
@@ -135,7 +152,9 @@ public class DiskStore implements FileStore {
|
|
|
public void copy(String source, String target) throws Exception {
|
|
|
source = formatPath(source);
|
|
|
target = formatPath(target);
|
|
|
- IOUtils.copy(new File(rootDir, source), new File(rootDir, target));
|
|
|
+ File targetFile = new File(rootDir, target);
|
|
|
+ targetFile.getParentFile().mkdirs();
|
|
|
+ IOUtils.copy(new File(rootDir, source), targetFile);
|
|
|
}
|
|
|
|
|
|
}
|