LocalFileService.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. FileUtil.copy(filePath, getOssPath(bucket, remoteFilePath), true);
  33. return null;
  34. }
  35. @Override
  36. public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
  37. try {
  38. String optType = new File(filePath).isDirectory() ? "folder" : "file";
  39. String command = String.format(FYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
  40. log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  41. callshell(command);
  42. } catch (Exception e) {
  43. log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  44. e.printStackTrace();
  45. }
  46. return null;
  47. }
  48. @Override
  49. public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
  50. try {
  51. String optType = new File(filePath).isDirectory() ? "folder" : "file";
  52. String command = String.format(FYunConstants.DOWNLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
  53. log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  54. callshell(command);
  55. } catch (Exception e) {
  56. log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  57. e.printStackTrace();
  58. }
  59. }
  60. @Override
  61. public void deleteFile(String bucket, String remoteFilePath) throws IOException {
  62. FileUtil.del(getOssPath(bucket, remoteFilePath));
  63. }
  64. @Override
  65. public void deleteFolder(String bucket, String remoteFolderPath) {
  66. FileUtil.del(getOssPath(bucket, remoteFolderPath));
  67. }
  68. @Override
  69. public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
  70. try {
  71. for (Map.Entry<String, String> entry : filepaths.entrySet()) {
  72. uploadFile(bucket, entry.getKey(), entry.getValue(), null);
  73. }
  74. } catch (Exception e) {
  75. log.error("OSS批量上传文件失败!");
  76. }
  77. }
  78. @Override
  79. public List<String> listRemoteFiles(String bucket, String sourcePath) {
  80. return listRemoteFiles(bucket, sourcePath, true);
  81. }
  82. private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) {
  83. return FileUtil.loopFiles(getOssPath(bucket, sourcePath)).stream()
  84. .map(f -> f.getAbsolutePath().replace(LocalConstants.BASE_PATH.concat(bucket).concat(File.separator), ""))
  85. .collect(Collectors.toList());
  86. }
  87. @Override
  88. public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
  89. FileUtil.copyContent(new File(getOssPath(sourceBucketName, sourcePath)), new File(getOssPath(targetBucketName, targetPath)), true);
  90. }
  91. @Override
  92. public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
  93. if (ObjectUtils.isEmpty(pathMap)) {
  94. return;
  95. }
  96. try {
  97. for (Map.Entry<String, String> entry : pathMap.entrySet()) {
  98. copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue());
  99. }
  100. } catch (Exception e) {
  101. log.error("批量复制文件失败!");
  102. }
  103. }
  104. @Override
  105. public String getFileContent(String bucketName, String remoteFilePath) {
  106. return FileUtil.readUtf8String(getOssPath(bucketName, remoteFilePath));
  107. }
  108. @Override
  109. public boolean fileExist(String bucket, String objectName) {
  110. return FileUtil.exist(getOssPath(bucket, objectName));
  111. }
  112. @Override
  113. public void downloadFile(String bucket, String remoteFilePath, String localPath) {
  114. FileUtil.copy(getOssPath(bucket, remoteFilePath), localPath, true);
  115. }
  116. private String getOssPath(String bucket, String filePath) {
  117. return LocalConstants.BASE_PATH.concat(bucket).concat(File.separator).concat(filePath);
  118. }
  119. }