package com.fdkankan.fyun.local; import cn.hutool.core.io.FileUtil; import com.fdkankan.fyun.constant.FYunConstants; import com.fdkankan.fyun.constant.FYunTypeEnum; 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, null); } @Override public String uploadFile(String bucket, String filePath, String remoteFilePath, Map headers) { FileUtil.copy(filePath, getOssPath(bucket, remoteFilePath), true); return null; } @Override public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) { try { String optType = new File(filePath).isDirectory() ? "folder" : "file"; String command = String.format(FYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType); log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath); callshell(command); } catch (Exception e) { log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath); e.printStackTrace(); } return null; } @Override public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) { try { String optType = new File(filePath).isDirectory() ? "folder" : "file"; String command = String.format(FYunConstants.DOWNLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType); log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath); callshell(command); } catch (Exception e) { log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath); e.printStackTrace(); } } @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 filepaths) { try { for (Map.Entry entry : filepaths.entrySet()) { uploadFile(bucket, entry.getKey(), entry.getValue(), null); } } catch (Exception e) { log.error("OSS批量上传文件失败!"); } } @Override public List listRemoteFiles(String bucket, String sourcePath) { return listRemoteFiles(bucket, sourcePath, true); } private List 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) { FileUtil.copyContent(new File(getOssPath(sourceBucketName, sourcePath)), new File(getOssPath(targetBucketName, targetPath)), true); } @Override public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map pathMap) { if (ObjectUtils.isEmpty(pathMap)) { return; } try { for (Map.Entry entry : pathMap.entrySet()) { copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue()); } } 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); } }