|
@@ -0,0 +1,196 @@
|
|
|
|
+package cn.com.qmth.stmms.biz.file.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.stmms.biz.file.enums.FileType;
|
|
|
|
+import cn.com.qmth.stmms.biz.file.enums.FormatType;
|
|
|
|
+import cn.com.qmth.stmms.biz.file.service.FileService;
|
|
|
|
+import cn.com.qmth.stmms.biz.file.store.FileStore;
|
|
|
|
+import cn.com.qmth.stmms.biz.file.store.impl.DiskStore;
|
|
|
|
+import cn.com.qmth.stmms.biz.file.store.impl.OssStore;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.FileNotFoundException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.util.LinkedList;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+@Service("fileService")
|
|
|
|
+public class FileServiceImpl implements FileService, InitializingBean {
|
|
|
|
+
|
|
|
|
+ private static final String EMPTY_SUBJECT_CODE_PLACEHOLDER = "common";
|
|
|
|
+
|
|
|
|
+ private static final int DEFAULT_SUFFIX_LENGTH = 3;
|
|
|
|
+
|
|
|
|
+ @Value("${file.server}")
|
|
|
|
+ private String fileServer;
|
|
|
|
+
|
|
|
|
+ @Value("${file.store}")
|
|
|
|
+ private String fileStore;
|
|
|
|
+
|
|
|
|
+ private FileStore store;
|
|
|
|
+
|
|
|
|
+ private String getSuffix(String input) {
|
|
|
|
+ return StringUtils.trimToEmpty(input).substring(Math.max(0, input.length() - DEFAULT_SUFFIX_LENGTH));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean checkFormat(FormatType input, FormatType... types) {
|
|
|
|
+ for (FormatType type : types) {
|
|
|
|
+ if (type.equals(input)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getFileServer() {
|
|
|
|
+ return fileServer;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setFileServer(String fileServer) {
|
|
|
|
+ this.fileServer = fileServer;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getFileStore() {
|
|
|
|
+ return fileStore;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setFileStore(String fileStore) {
|
|
|
|
+ this.fileStore = fileStore;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean uploadSheet(InputStream ins, String md5, int examId, String examNumber, int index) {
|
|
|
|
+ return store.write(getSheetUri(examId, examNumber, index), ins, md5);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean uploadSlice(InputStream ins, String md5, int examId, String secretNumber, int index) {
|
|
|
|
+ return store.write(getSliceUri(examId, secretNumber, index), ins, md5);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean uploadJson(InputStream ins, String md5, int examId, String secretNumber) {
|
|
|
|
+ return store.write(getJsonUri(examId, secretNumber), ins, md5);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean uploadPackage(InputStream ins, String md5, int examId, String packageCode, int index) {
|
|
|
|
+ return store.write(getPackageUri(examId, packageCode, index), ins, md5);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean uploadPaper(InputStream ins, String md5, int examId, String subjectCode, FormatType type) {
|
|
|
|
+ return checkFormat(type, FormatType.PDF, FormatType.JSON) && store
|
|
|
|
+ .write(getPaperUri(examId, subjectCode, type), ins, md5);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean uploadAnswer(InputStream ins, String md5, int examId, String subjectCode, FormatType type) {
|
|
|
|
+ return checkFormat(type, FormatType.PDF, FormatType.JSON) && store
|
|
|
|
+ .write(getAnswerUri(examId, subjectCode, type), ins, md5);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean uploadCard(InputStream ins, String md5, int examId, String subjectCode, FormatType type) {
|
|
|
|
+ return checkFormat(type, FormatType.ZIP, FormatType.JSON) && store
|
|
|
|
+ .write(getCardUri(examId, subjectCode != null ? subjectCode : EMPTY_SUBJECT_CODE_PLACEHOLDER, type),
|
|
|
|
+ ins, md5);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getSheetUri(int examId, String examNumber, int index) {
|
|
|
|
+ return FileType.SHEET.getPath(examId, getSuffix(examNumber), examNumber, index, FormatType.JPG.getExtName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<String> getSheetUris(int examId, String examNumber, int start, int end) {
|
|
|
|
+ List<String> list = new LinkedList<>();
|
|
|
|
+ for (int i = start; i <= end; i++) {
|
|
|
|
+ list.add(getSheetUri(examId, examNumber, i));
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getSliceUri(int examId, String secretNumber, int index) {
|
|
|
|
+ return FileType.SLICE
|
|
|
|
+ .getPath(examId, getSuffix(secretNumber), secretNumber, index, FormatType.JPG.getExtName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<String> getSliceUris(int examId, String secretNumber, int start, int end) {
|
|
|
|
+ List<String> list = new LinkedList<>();
|
|
|
|
+ for (int i = start; i <= end; i++) {
|
|
|
|
+ list.add(getSliceUri(examId, secretNumber, i));
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getJsonUri(int examId, String secretNumber) {
|
|
|
|
+ return FileType.JSON.getPath(examId, getSuffix(secretNumber), secretNumber, FormatType.JSON.getExtName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getPackageUri(int examId, String packageCode, int index) {
|
|
|
|
+ return FileType.PACKAGE.getPath(examId, packageCode, index, FormatType.JPG.getExtName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<String> getPackageUris(int examId, String packageCode, int start, int end) {
|
|
|
|
+ List<String> list = new LinkedList<>();
|
|
|
|
+ for (int i = start; i <= end; i++) {
|
|
|
|
+ list.add(getPackageUri(examId, packageCode, i));
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getPaperUri(int examId, String subjectCode, FormatType type) {
|
|
|
|
+ return FileType.PAPER.getPath(examId, subjectCode, type.getExtName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getAnswerUri(int examId, String subjectCode, FormatType type) {
|
|
|
|
+ return FileType.ANSWER.getPath(examId, subjectCode, type.getExtName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getCardUri(int examId, String subjectCode, FormatType type) {
|
|
|
|
+ return FileType.ANSWER
|
|
|
|
+ .getPath(examId, subjectCode != null ? subjectCode : EMPTY_SUBJECT_CODE_PLACEHOLDER, type.getExtName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void afterPropertiesSet() {
|
|
|
|
+ fileServer = StringUtils.trimToNull(fileServer);
|
|
|
|
+ fileStore = StringUtils.trimToNull(fileStore);
|
|
|
|
+ if (fileServer == null) {
|
|
|
|
+ throw new RuntimeException("invald property: ${file.server} should not be empty");
|
|
|
|
+ }
|
|
|
|
+ if (fileStore == null) {
|
|
|
|
+ throw new RuntimeException("invald property: ${file.store} should not be empty");
|
|
|
|
+ }
|
|
|
|
+ if (fileStore.startsWith("oss")) {
|
|
|
|
+ store = new OssStore(fileStore);
|
|
|
|
+ } else {
|
|
|
|
+ store = new DiskStore(fileStore);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) throws FileNotFoundException {
|
|
|
|
+ FileServiceImpl service = new FileServiceImpl();
|
|
|
|
+ service.fileServer = "123";
|
|
|
|
+ service.fileStore = "oss://LTAI4FnJ2pgV6aGceYcCkeEi:ktrMEVE7PfoxRPeJUPDFeygOIH4aU7@qmth-test.oss-cn-shenzhen.aliyuncs.com";
|
|
|
|
+ service.afterPropertiesSet();
|
|
|
|
+
|
|
|
|
+ //DiskStore ds = new DiskStore("/Users/luoshi/Downloads");
|
|
|
|
+ //String md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(ds.read("123456.jpg")));
|
|
|
|
+ System.out.println(service.uploadSheet(new FileInputStream("/Users/luoshi/Downloads/123456.jpg"),
|
|
|
|
+ "7e9b368ff5da88ff2413c2c9083c481d", 1, "123456", 1));
|
|
|
|
+ }
|
|
|
|
+}
|