LocalFileService.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.fdkankan.fyun.local;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.fdkankan.common.util.FileUtils;
  4. import com.fdkankan.fyun.constant.FYunConstants;
  5. import com.fdkankan.fyun.constant.FYunTypeEnum;
  6. import com.fdkankan.fyun.face.AbstractFYunFileService;
  7. import com.fdkankan.fyun.local.constant.LocalConstants;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.net.URL;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.stream.Collectors;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.util.ObjectUtils;
  20. @Component
  21. @ConditionalOnProperty(name = "fyun.type", havingValue = "local")
  22. public class LocalFileService extends AbstractFYunFileService {
  23. private Logger log = LoggerFactory.getLogger(this.getClass().getName());
  24. @Override
  25. public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
  26. try {
  27. FileUtil.writeBytes(data, getOssPath(bucket, remoteFilePath));
  28. }catch (Exception e){
  29. log.error("oss上传文件失败,remoteFilePath:" + remoteFilePath, e);
  30. }
  31. return null;
  32. }
  33. @Override
  34. public String uploadFile(String bucket, String filePath, String remoteFilePath) {
  35. return uploadFile(bucket, filePath, remoteFilePath, null);
  36. }
  37. @Override
  38. public String uploadFile(String bucket, InputStream inputStream, String remoteFilePath) {
  39. try {
  40. FileUtil.writeFromStream(inputStream,getOssPath(bucket, remoteFilePath));
  41. }catch (Exception e){
  42. log.error("上传文件失败,remoteFilePath:{}", remoteFilePath);
  43. log.error("上传文件失败", e);
  44. }
  45. return null;
  46. }
  47. @Override
  48. public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
  49. try {
  50. if (!new File(filePath).exists()) {
  51. log.warn("文件不存在:{}", filePath);
  52. return null;
  53. }
  54. FileUtil.copy(filePath, getOssPath(bucket, remoteFilePath), true);
  55. }catch (Exception e){
  56. log.error("上传文件失败,filePath:{},remoteFilePath:{}", filePath, remoteFilePath);
  57. log.error("上传文件失败", e);
  58. }
  59. return null;
  60. }
  61. @Override
  62. public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
  63. try {
  64. String optType = new File(filePath).isDirectory() ? "folder" : "file";
  65. String command = String.format(fYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
  66. log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  67. callshell(command);
  68. } catch (Exception e) {
  69. log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  70. }
  71. return null;
  72. }
  73. @Override
  74. public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
  75. try {
  76. String optType = remoteFilePath.contains(".") ? "file" : "folder";
  77. String command = String.format(fYunConstants.DOWNLOAD_SH, bucket, remoteFilePath, filePath, FYunTypeEnum.LOCAL.code(), optType);
  78. log.info("开始下载文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  79. callshell(command);
  80. } catch (Exception e) {
  81. log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
  82. }
  83. }
  84. @Override
  85. public void deleteFile(String bucket, String remoteFilePath) throws IOException {
  86. FileUtil.del(getOssPath(bucket, remoteFilePath));
  87. }
  88. @Override
  89. public void deleteFolder(String bucket, String remoteFolderPath) {
  90. FileUtil.del(getOssPath(bucket, remoteFolderPath));
  91. }
  92. @Override
  93. public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
  94. for (Map.Entry<String, String> entry : filepaths.entrySet()) {
  95. uploadFile(bucket, entry.getKey(), entry.getValue(), null);
  96. }
  97. }
  98. @Override
  99. public List<String> listRemoteFiles(String bucket, String sourcePath) {
  100. return listRemoteFiles(bucket, sourcePath, true);
  101. }
  102. private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) {
  103. return FileUtil.loopFiles(getOssPath(bucket, sourcePath)).stream()
  104. .map(f -> f.getAbsolutePath().replace(LocalConstants.BASE_PATH.concat(bucket).concat(File.separator), ""))
  105. .collect(Collectors.toList());
  106. }
  107. @Override
  108. public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
  109. try {
  110. FileUtil.copyContent(new File(getOssPath(sourceBucketName, sourcePath)), new File(getOssPath(targetBucketName, targetPath)), true);
  111. }catch (Exception e){
  112. log.error("复制文件失败,sourcePath:{},targetPath:{}",sourcePath,targetPath);
  113. log.error("复制文件失败", e);
  114. }
  115. }
  116. @Override
  117. public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
  118. if (ObjectUtils.isEmpty(pathMap)) {
  119. return;
  120. }
  121. for (Map.Entry<String, String> entry : pathMap.entrySet()) {
  122. copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue());
  123. }
  124. }
  125. @Override
  126. public String getFileContent(String bucketName, String remoteFilePath) {
  127. try {
  128. return FileUtil.readUtf8String(getOssPath(bucketName, remoteFilePath));
  129. }catch (Exception e){
  130. log.error("读取文件失败,remoteFilePath:{}", remoteFilePath);
  131. log.error("读取文件失败", e);
  132. return null;
  133. }
  134. }
  135. @Override
  136. public boolean fileExist(String bucket, String objectName) {
  137. return FileUtil.exist(getOssPath(bucket, objectName));
  138. }
  139. @Override
  140. public void downloadFile(String bucket, String remoteFilePath, String localPath) {
  141. try {
  142. FileUtil.copy(getOssPath(bucket, remoteFilePath), localPath, true);
  143. }catch (Exception e){
  144. log.error("下载文件失败,remoteFilePath:{},localPath:{}", remoteFilePath, localPath);
  145. log.error("下载文件失败", e);
  146. }
  147. }
  148. @Override
  149. public URL getPresignedUrl(String bucket, String url) {
  150. return null;
  151. }
  152. @Override
  153. public long getSubFileNums(String bucket, String url) {
  154. return FileUtils.getSubFileNums(new File(getOssPath(bucket, url)));
  155. }
  156. @Override
  157. public void restoreFolder(String bucket, String folderName, Integer priority) {
  158. }
  159. @Override
  160. public void restoreFile(String bucket, String objectName, Integer priority) {
  161. }
  162. private String getOssPath(String bucket, String filePath) {
  163. return LocalConstants.BASE_PATH.concat(bucket).concat(File.separator).concat(filePath);
  164. }
  165. @Override
  166. public Boolean checkStore(String bucket, String url) {
  167. return null;
  168. }
  169. @Override
  170. public void restoreFolder(String bucket, String url) {
  171. }
  172. @Override
  173. public Integer getRestoreFolderProcess(String bucket, String url) {
  174. return null;
  175. }
  176. @Override
  177. public Long getSpace(String bucket, String key) {
  178. String ossPath = getOssPath(bucket, key);
  179. if(!FileUtil.exist(ossPath)){
  180. return 0L;
  181. }
  182. if(FileUtil.isFile(ossPath)){
  183. return FileUtil.size(new File(ossPath));
  184. }
  185. return FileUtil.loopFiles(ossPath).stream().mapToLong(File::length).sum();
  186. }
  187. }