|
@@ -0,0 +1,135 @@
|
|
|
+package com.fdkankan.fyun.local;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.fdkankan.fyun.constant.FYunConstants;
|
|
|
+import com.fdkankan.fyun.face.AbstractFYunFileService;
|
|
|
+import com.fdkankan.fyun.local.constant.LocalConstants;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Component
|
|
|
+@ConditionalOnProperty(name = "fyun.type",havingValue = "local")
|
|
|
+public class LocalFileService extends AbstractFYunFileService {
|
|
|
+
|
|
|
+ private Logger log = LoggerFactory.getLogger(this.getClass().getName());
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
|
|
|
+ FileUtil.writeBytes(data, getOssPath(bucket,remoteFilePath));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadFile(String bucket, String filePath, String remoteFilePath) {
|
|
|
+ return uploadFile(bucket, filePath, remoteFilePath, true, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
|
|
|
+ return uploadFile(bucket, filePath, remoteFilePath, true, headers);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown,Map<String, String> headers) {
|
|
|
+ FileUtil.copy(filePath, getOssPath(bucket,remoteFilePath),true);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
|
|
|
+ String ossPath = bucket + "/" + remoteFilePath;
|
|
|
+ try {
|
|
|
+ String command = String.format(FYunConstants.UPLOAD_SH, ossPath, filePath);
|
|
|
+ log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
|
|
|
+ callshell(command);
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteFile(String bucket, String remoteFilePath) throws IOException {
|
|
|
+ FileUtil.del(getOssPath(bucket,remoteFilePath));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteFolder(String bucket, String remoteFolderPath) {
|
|
|
+ FileUtil.del(getOssPath(bucket,remoteFolderPath));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
|
|
|
+ try {
|
|
|
+ for (Map.Entry<String, String> entry : filepaths.entrySet()) {
|
|
|
+ uploadFile(bucket, entry.getKey(), entry.getValue(), false,null);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("OSS批量上传文件失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> listRemoteFiles(String bucket, String sourcePath) {
|
|
|
+ return listRemoteFiles(bucket, sourcePath, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) {
|
|
|
+ return FileUtil.loopFiles(getOssPath(bucket,sourcePath)).stream()
|
|
|
+ .map(f->f.getAbsolutePath().replace(LocalConstants.BASE_PATH.concat(bucket).concat(File.separator),""))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
|
|
|
+ copyFileBetweenBucket(sourceBucketName, sourcePath, targetBucketName, targetPath, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath, Boolean shutdown) {
|
|
|
+ FileUtil.copyContent(new File(getOssPath(sourceBucketName, sourcePath)), new File(getOssPath(targetBucketName, targetPath)), true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
|
|
|
+ if (ObjectUtils.isEmpty(pathMap)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ for (Map.Entry<String, String> entry : pathMap.entrySet()) {
|
|
|
+ copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue(), false);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("批量复制文件失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getFileContent(String bucketName, String remoteFilePath) {
|
|
|
+ return FileUtil.readUtf8String(getOssPath(bucketName,remoteFilePath));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean fileExist(String bucket, String objectName) {
|
|
|
+ return FileUtil.exist(getOssPath(bucket,objectName));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void downloadFile(String bucket, String remoteFilePath, String localPath) {
|
|
|
+ FileUtil.copy(getOssPath(bucket, remoteFilePath), localPath, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getOssPath(String bucket, String filePath) {
|
|
|
+ return LocalConstants.BASE_PATH.concat(bucket).concat(File.separator).concat(filePath);
|
|
|
+ }
|
|
|
+}
|