Search in sources :

Example 11 with FileOperationException

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() + " 失败");
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException)

Example 12 with FileOperationException

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();
    }
}
Also used : BosClientConfiguration(com.baidubce.services.bos.BosClientConfiguration) DefaultBceCredentials(com.baidubce.auth.DefaultBceCredentials) FileOperationException(run.halo.app.exception.FileOperationException) BosClient(com.baidubce.services.bos.BosClient) FileOperationException(run.halo.app.exception.FileOperationException)

Example 13 with FileOperationException

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() + " 失败");
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException)

Example 14 with FileOperationException

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);
    }
}
Also used : Path(java.nio.file.Path) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException)

Example 15 with FileOperationException

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);
    }
}
Also used : Path(java.nio.file.Path) FileOperationException(run.halo.app.exception.FileOperationException) IOException(java.io.IOException)

Aggregations

FileOperationException (run.halo.app.exception.FileOperationException)69 IOException (java.io.IOException)39 UploadResult (run.halo.app.model.support.UploadResult)27 Path (java.nio.file.Path)24 NonNull (org.springframework.lang.NonNull)15 ServiceException (run.halo.app.exception.ServiceException)9 OSS (com.aliyun.oss.OSS)6 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)6 DefaultBceCredentials (com.baidubce.auth.DefaultBceCredentials)6 BosClient (com.baidubce.services.bos.BosClient)6 BosClientConfiguration (com.baidubce.services.bos.BosClientConfiguration)6 ObsClient (com.obs.services.ObsClient)6 COSClient (com.qcloud.cos.COSClient)6 ClientConfig (com.qcloud.cos.ClientConfig)6 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)6 COSCredentials (com.qcloud.cos.auth.COSCredentials)6 Region (com.qcloud.cos.region.Region)6 QiniuException (com.qiniu.common.QiniuException)6 Response (com.qiniu.http.Response)6 Configuration (com.qiniu.storage.Configuration)6