package cn.iocoder.yudao.module.infra.framework.file.core.client.local;
|
|
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.IORuntimeException;
|
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
|
import cn.iocoder.yudao.module.infra.framework.file.core.utils.FilePathUtils;
|
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_PATH_INVALID;
|
|
/**
|
* 本地文件客户端
|
*
|
* @author 芋道源码
|
*/
|
public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> {
|
|
public LocalFileClient(Long id, LocalFileClientConfig config) {
|
super(id, config);
|
}
|
|
@Override
|
protected void doInit() {
|
}
|
|
@Override
|
public String upload(byte[] content, String path, String type) {
|
// 执行写入
|
String filePath = getFilePath(path);
|
FileUtil.writeBytes(content, filePath);
|
// 拼接返回路径
|
return super.formatFileUrl(config.getDomain(), path);
|
}
|
|
@Override
|
public void delete(String path) {
|
String filePath = getFilePath(path);
|
FileUtil.del(filePath);
|
}
|
|
@Override
|
public byte[] getContent(String path) {
|
String filePath = getFilePath(path);
|
try {
|
return FileUtil.readBytes(filePath);
|
} catch (IORuntimeException ex) {
|
if (ex.getMessage().startsWith("File not exist:")) {
|
return null;
|
}
|
throw ex;
|
}
|
}
|
|
private String getFilePath(String path) {
|
FilePathUtils.validatePath(path);
|
Path basePath = Paths.get(config.getBasePath()).toAbsolutePath().normalize();
|
Path filePath = basePath.resolve(path).normalize();
|
if (!filePath.startsWith(basePath)) {
|
throw exception(FILE_PATH_INVALID);
|
}
|
return filePath.toString();
|
}
|
|
}
|