Search in sources :

Example 1 with LocalStorage

use of com.dimple.project.tool.domain.LocalStorage in project DimpleBlog by martin-chips.

the class LocalStorageServiceImpl method upload.

@Override
public int upload(String name, MultipartFile multipartFile) {
    // 检查大小
    // 获取后缀
    String suffix = FileUtils.getExtensionName(multipartFile.getOriginalFilename());
    String type = FileUtils.getFileType(suffix);
    File file = FileUtils.upload(multipartFile, DimpleBlogConfig.getProfile() + type + File.separator);
    if (Objects.isNull(file)) {
        throw new CustomException("上传失败");
    }
    // 防止异常出错
    try {
        name = StringUtils.isBlank(name) ? FileUtils.getFileNameNoExtension(multipartFile.getOriginalFilename()) : name;
        LocalStorage localStorage = new LocalStorage(file.getName(), name, suffix, file.getPath(), type, FileUtils.getSizeString(multipartFile.getSize()));
        localStorage.setCreateBy(SecurityUtils.getUsername());
        return localStorageMapper.insertLocalStorage(localStorage);
    } catch (Exception e) {
        FileUtils.del(file);
        throw e;
    }
}
Also used : LocalStorage(com.dimple.project.tool.domain.LocalStorage) CustomException(com.dimple.common.exception.CustomException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) CustomException(com.dimple.common.exception.CustomException)

Example 2 with LocalStorage

use of com.dimple.project.tool.domain.LocalStorage in project DimpleBlog by martin-chips.

the class LocalStorageServiceImpl method deleteLocalStorage.

@Override
public int deleteLocalStorage(Long id) {
    String username = SecurityUtils.getUsername();
    LocalStorage localStorage = localStorageMapper.selectLocalStorageById(id);
    if (Objects.isNull(localStorage)) {
        throw new CustomException("文件不存在");
    }
    // 删除文件
    String path = localStorage.getPath();
    FileUtils.del(path);
    return localStorageMapper.deleteLocalStorageById(id, username);
}
Also used : LocalStorage(com.dimple.project.tool.domain.LocalStorage) CustomException(com.dimple.common.exception.CustomException)

Aggregations

CustomException (com.dimple.common.exception.CustomException)2 LocalStorage (com.dimple.project.tool.domain.LocalStorage)2 File (java.io.File)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1