LocalFileService.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.fdkankan.fyun.local;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.fdkankan.fyun.constant.FYunConstants;
  4. import com.fdkankan.fyun.constant.FYunTypeEnum;
  5. import com.fdkankan.fyun.face.AbstractFYunFileService;
  6. import com.fdkankan.fyun.local.constant.LocalConstants;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.util.ObjectUtils;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.stream.Collectors;
  17. @Component
  18. @ConditionalOnProperty(name = "fyun.type", havingValue = "local")
  19. public class LocalFileService extends AbstractFYunFileService {
  20. private Logger log = LoggerFactory.getLogger(this.getClass().getName());
  21. @Override
  22. public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
  23. FileUtil.writeBytes(data, getOssPath(bucket, remoteFilePath));
  24. return null;
  25. }
  26. @Override
  27. public String uploadFile(String bucket, String filePath, String remoteFilePath) {
  28. return uploadFile(bucket, filePath, remoteFilePath, null);
  29. }
  30. @Override
  31. public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
  32. if (!new File(filePath).exists()) {
  33. log.error("文件不存在,不予上传:{}", filePath);
  34. return null;
  35. }
  36. FileUtil.copy(filePath, getOssPath(bucket, remoteFilePath), true);
  37. return null;
  38. }
  39. @Override
  40. public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
  41. try {
  42. String optType = new File(filePath).isDirectory() ? "folder" : "file";
  43. String command = String.format(FYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
  44. log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  45. callshell(command);
  46. } catch (Exception e) {
  47. log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  48. e.printStackTrace();
  49. }
  50. return null;
  51. }
  52. @Override
  53. public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
  54. try {
  55. String optType = new File(filePath).isDirectory() ? "folder" : "file";
  56. String command = String.format(FYunConstants.DOWNLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
  57. log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  58. callshell(command);
  59. } catch (Exception e) {
  60. log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  61. e.printStackTrace();
  62. }
  63. }
  64. @Override
  65. public void deleteFile(String bucket, String remoteFilePath) throws IOException {
  66. FileUtil.del(getOssPath(bucket, remoteFilePath));
  67. }
  68. @Override
  69. public void deleteFolder(String bucket, String remoteFolderPath) {
  70. FileUtil.del(getOssPath(bucket, remoteFolderPath));
  71. }
  72. @Override
  73. public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
  74. try {
  75. for (Map.Entry<String, String> entry : filepaths.entrySet()) {
  76. uploadFile(bucket, entry.getKey(), entry.getValue(), null);
  77. }
  78. } catch (Exception e) {
  79. log.error("OSS批量上传文件失败!");
  80. }
  81. }
  82. @Override
  83. public List<String> listRemoteFiles(String bucket, String sourcePath) {
  84. return listRemoteFiles(bucket, sourcePath, true);
  85. }
  86. private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) {
  87. return FileUtil.loopFiles(getOssPath(bucket, sourcePath)).stream()
  88. .map(f -> f.getAbsolutePath().replace(LocalConstants.BASE_PATH.concat(bucket).concat(File.separator), ""))
  89. .collect(Collectors.toList());
  90. }
  91. @Override
  92. public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
  93. FileUtil.copyContent(new File(getOssPath(sourceBucketName, sourcePath)), new File(getOssPath(targetBucketName, targetPath)), true);
  94. }
  95. @Override
  96. public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
  97. if (ObjectUtils.isEmpty(pathMap)) {
  98. return;
  99. }
  100. try {
  101. for (Map.Entry<String, String> entry : pathMap.entrySet()) {
  102. copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue());
  103. }
  104. } catch (Exception e) {
  105. log.error("批量复制文件失败!");
  106. }
  107. }
  108. @Override
  109. public String getFileContent(String bucketName, String remoteFilePath) {
  110. return FileUtil.readUtf8String(getOssPath(bucketName, remoteFilePath));
  111. }
  112. @Override
  113. public boolean fileExist(String bucket, String objectName) {
  114. return FileUtil.exist(getOssPath(bucket, objectName));
  115. }
  116. @Override
  117. public void downloadFile(String bucket, String remoteFilePath, String localPath) {
  118. FileUtil.copy(getOssPath(bucket, remoteFilePath), localPath, true);
  119. }
  120. private String getOssPath(String bucket, String filePath) {
  121. return LocalConstants.BASE_PATH.concat(bucket).concat(File.separator).concat(filePath);
  122. }
  123. }