feat: support get backup file size.

pull/389/head
ruibaby 2019-11-19 12:24:08 +08:00
parent c95aee14a7
commit a182dc7de7
3 changed files with 34 additions and 33 deletions

View File

@ -24,7 +24,7 @@ import java.util.List;
* Backup controller * Backup controller
* *
* @author johnniang * @author johnniang
* @date 19-4-26 * @date 2019-04-26
*/ */
@RestController @RestController
@RequestMapping("/api/admin/backups") @RequestMapping("/api/admin/backups")

View File

@ -2,8 +2,6 @@ package run.halo.app.model.dto;
import lombok.Data; import lombok.Data;
import java.util.Date;
/** /**
* @author ryanwang * @author ryanwang
* @date 2019-05-25 * @date 2019-05-25
@ -15,5 +13,7 @@ public class BackupDTO {
private String filename; private String filename;
private long updateTime; private Long updateTime;
private Long fileSize;
} }

View File

@ -24,7 +24,6 @@ import run.halo.app.service.BackupService;
import run.halo.app.service.OptionService; import run.halo.app.service.OptionService;
import run.halo.app.service.PostService; import run.halo.app.service.PostService;
import run.halo.app.service.PostTagService; import run.halo.app.service.PostTagService;
import run.halo.app.utils.SlugUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -45,30 +44,40 @@ import java.util.stream.Collectors;
* Backup service implementation. * Backup service implementation.
* *
* @author johnniang * @author johnniang
* @author ryanwang
* @date 2019-04-26 * @date 2019-04-26
*/ */
@Service @Service
@Slf4j @Slf4j
public class BackupServiceImpl implements BackupService { public class BackupServiceImpl implements BackupService {
private final PostService postService;
private final PostTagService postTagService;
private final OptionService optionService;
private final HaloProperties halo;
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private final PostService postService;
private final PostTagService postTagService;
private final OptionService optionService;
private final HaloProperties haloProperties;
public BackupServiceImpl(PostService postService, public BackupServiceImpl(PostService postService,
PostTagService postTagService, PostTagService postTagService,
OptionService optionService, OptionService optionService,
HaloProperties halo) { HaloProperties haloProperties) {
this.postService = postService; this.postService = postService;
this.postTagService = postTagService; this.postTagService = postTagService;
this.optionService = optionService; this.optionService = optionService;
this.halo = halo; this.haloProperties = haloProperties;
}
/**
* Sanitizes the specified file name.
*
* @param unsanitized the specified file name
* @return sanitized file name
*/
public static String sanitizeFilename(final String unsanitized) {
return unsanitized.
replaceAll("[^(a-zA-Z0-9\\u4e00-\\u9fa5\\.)]", "").
replaceAll("[\\?\\\\/:|<>\\*\\[\\]\\(\\)\\$%\\{\\}@~\\.]", "").
replaceAll("\\s", "");
} }
@Override @Override
@ -154,10 +163,10 @@ public class BackupServiceImpl implements BackupService {
.append(IdUtil.simpleUUID()) .append(IdUtil.simpleUUID())
.append(".zip").toString(); .append(".zip").toString();
// Create halo zip file // Create halo zip file
Path haloZipPath = Files.createFile(Paths.get(halo.getBackupDir(), haloZipFileName)); Path haloZipPath = Files.createFile(Paths.get(haloProperties.getBackupDir(), haloZipFileName));
// Zip halo // Zip halo
run.halo.app.utils.FileUtils.zip(Paths.get(this.halo.getWorkDir()), haloZipPath); run.halo.app.utils.FileUtils.zip(Paths.get(this.haloProperties.getWorkDir()), haloZipPath);
// Build backup dto // Build backup dto
return buildBackupDto(haloZipPath); return buildBackupDto(haloZipPath);
@ -170,7 +179,7 @@ public class BackupServiceImpl implements BackupService {
public List<BackupDTO> listHaloBackups() { public List<BackupDTO> listHaloBackups() {
try { try {
// Build backup dto // Build backup dto
return Files.list(Paths.get(halo.getBackupDir())) return Files.list(Paths.get(haloProperties.getBackupDir()))
.filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_BACKUP_PREFIX)) .filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_BACKUP_PREFIX))
.map(this::buildBackupDto) .map(this::buildBackupDto)
.sorted((leftBackup, rightBackup) -> { .sorted((leftBackup, rightBackup) -> {
@ -192,7 +201,7 @@ public class BackupServiceImpl implements BackupService {
Assert.hasText(filename, "File name must not be blank"); Assert.hasText(filename, "File name must not be blank");
// Get backup path // Get backup path
Path backupPath = Paths.get(halo.getBackupDir(), filename); Path backupPath = Paths.get(haloProperties.getBackupDir(), filename);
try { try {
// Delete backup file // Delete backup file
@ -205,19 +214,6 @@ public class BackupServiceImpl implements BackupService {
} }
} }
/**
* Sanitizes the specified file name.
*
* @param unsanitized the specified file name
* @return sanitized file name
*/
public static String sanitizeFilename(final String unsanitized) {
return unsanitized.
replaceAll("[^(a-zA-Z0-9\\u4e00-\\u9fa5\\.)]", "").
replaceAll("[\\?\\\\/:|<>\\*\\[\\]\\(\\)\\$%\\{\\}@~\\.]", "").
replaceAll("\\s", "");
}
/** /**
* Builds backup dto. * Builds backup dto.
* *
@ -236,6 +232,11 @@ public class BackupServiceImpl implements BackupService {
} catch (IOException e) { } catch (IOException e) {
throw new ServiceException("Failed to get last modified time of " + backupPath.toString(), e); throw new ServiceException("Failed to get last modified time of " + backupPath.toString(), e);
} }
try {
backup.setFileSize(Files.size(backupPath));
} catch (IOException e) {
throw new ServiceException("Failed to get file size " + backupPath.toString(), e);
}
return backup; return backup;
} }
@ -251,7 +252,7 @@ public class BackupServiceImpl implements BackupService {
return StringUtils.joinWith("/", return StringUtils.joinWith("/",
optionService.getBlogBaseUrl(), optionService.getBlogBaseUrl(),
StringUtils.removeEnd(StringUtils.removeStart(halo.getBackupUrlPrefix(), "/"), "/"), StringUtils.removeEnd(StringUtils.removeStart(haloProperties.getBackupUrlPrefix(), "/"), "/"),
filename); filename);
} }
} }