LocalFileService.java 8.9 KB

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