use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
the class StaticStorageServiceImpl method rename.
@Override
public void rename(String basePath, String newName) {
Assert.notNull(basePath, "Base path must not be null");
Assert.notNull(newName, "New name must not be null");
Path pathToRename;
if (StringUtils.startsWith(newName, API_FOLDER_NAME)) {
throw new FileOperationException("重命名名称 " + newName + " 不合法");
}
pathToRename = Paths.get(staticDir.toString(), basePath);
// check if the path is valid (not outside staticDir)
FileUtils.checkDirectoryTraversal(staticDir.toString(), pathToRename.toString());
try {
FileUtils.rename(pathToRename, newName);
onChange();
} catch (FileAlreadyExistsException e) {
throw new FileOperationException("该路径下名称 " + newName + " 已存在");
} catch (IOException e) {
throw new FileOperationException("重命名 " + pathToRename.toString() + " 失败");
}
}
use of run.halo.app.exception.FileOperationException in project halo by ruibaby.
the class BaiduBosFileHandler method delete.
@Override
public void delete(String key) {
Assert.notNull(key, "File key must not be blank");
// Get config
String endPoint = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ENDPOINT).toString();
String accessKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_ACCESS_KEY).toString();
String secretKey = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_SECRET_KEY).toString();
String bucketName = optionService.getByPropertyOfNonNull(BaiduBosProperties.BOS_BUCKET_NAME).toString();
BosClientConfiguration config = new BosClientConfiguration();
config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
config.setEndpoint(endPoint);
// Init OSS client
BosClient client = new BosClient(config);
try {
client.deleteObject(bucketName, key);
} catch (Exception e) {
throw new FileOperationException("附件 " + key + " 从百度云删除失败", e);
} finally {
client.shutdown();
}
}
use of run.halo.app.exception.FileOperationException in project halo by halo-dev.
the class StaticStorageServiceImpl method rename.
@Override
public void rename(String basePath, String newName) {
Assert.notNull(basePath, "Base path must not be null");
Assert.notNull(newName, "New name must not be null");
Path pathToRename;
if (StringUtils.startsWith(newName, API_FOLDER_NAME)) {
throw new FileOperationException("重命名名称 " + newName + " 不合法");
}
pathToRename = Paths.get(staticDir.toString(), basePath);
// check if the path is valid (not outside staticDir)
FileUtils.checkDirectoryTraversal(staticDir.toString(), pathToRename.toString());
try {
FileUtils.rename(pathToRename, newName);
onChange();
} catch (FileAlreadyExistsException e) {
throw new FileOperationException("该路径下名称 " + newName + " 已存在");
} catch (IOException e) {
throw new FileOperationException("重命名 " + pathToRename.toString() + " 失败");
}
}
use of run.halo.app.exception.FileOperationException in project halo by halo-dev.
the class StaticStorageServiceImpl method delete.
@Override
public void delete(String relativePath) {
Assert.notNull(relativePath, "Relative path must not be null");
Path path = Paths.get(staticDir.toString(), relativePath);
// check if the path is valid (not outside staticDir)
FileUtils.checkDirectoryTraversal(staticDir.toString(), path.toString());
log.debug(path.toString());
try {
if (path.toFile().isDirectory()) {
FileUtils.deleteFolder(path);
} else {
Files.deleteIfExists(path);
}
onChange();
} catch (IOException e) {
throw new FileOperationException("文件 " + relativePath + " 删除失败", e);
}
}
use of run.halo.app.exception.FileOperationException in project halo by halo-dev.
the class StaticStorageServiceImpl method createFolder.
@Override
public void createFolder(String basePath, String folderName) {
Assert.notNull(folderName, "Folder name path must not be null");
Path path;
if (StringUtils.startsWith(folderName, API_FOLDER_NAME)) {
throw new FileOperationException("目录名称 " + folderName + " 不合法");
}
if (StringUtils.isEmpty(basePath)) {
path = Paths.get(staticDir.toString(), folderName);
} else {
path = Paths.get(staticDir.toString(), basePath, folderName);
}
// check if the path is valid (not outside staticDir)
FileUtils.checkDirectoryTraversal(staticDir.toString(), path.toString());
if (path.toFile().exists()) {
throw new FileOperationException("目录 " + path.toString() + " 已存在").setErrorData(path);
}
try {
FileUtils.createIfAbsent(path);
} catch (IOException e) {
throw new FileOperationException("目录 " + path.toString() + " 创建失败", e);
}
}
Aggregations