diff --git a/src/main/java/run/halo/app/model/dto/BackupDTO.java b/src/main/java/run/halo/app/model/dto/BackupDTO.java index 26684ec1d..1dbc1e5ac 100644 --- a/src/main/java/run/halo/app/model/dto/BackupDTO.java +++ b/src/main/java/run/halo/app/model/dto/BackupDTO.java @@ -14,4 +14,6 @@ public class BackupDTO { private String downloadUrl; private String filename; + + private long updateTime; } diff --git a/src/main/java/run/halo/app/model/support/HaloConst.java b/src/main/java/run/halo/app/model/support/HaloConst.java index 9cc03c22a..001762c08 100644 --- a/src/main/java/run/halo/app/model/support/HaloConst.java +++ b/src/main/java/run/halo/app/model/support/HaloConst.java @@ -22,6 +22,11 @@ public class HaloConst { */ public final static String TEMP_DIR = System.getProperties().getProperty("java.io.tmpdir"); + /** + * Halo backup prefix. + */ + public final static String HALO_BACKUP_PREFIX = "halo-backup-"; + /** * Default theme name. */ diff --git a/src/main/java/run/halo/app/service/impl/BackupServiceImpl.java b/src/main/java/run/halo/app/service/impl/BackupServiceImpl.java index a8d60e79c..fad6f3917 100644 --- a/src/main/java/run/halo/app/service/impl/BackupServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BackupServiceImpl.java @@ -15,11 +15,11 @@ import org.yaml.snakeyaml.Yaml; import run.halo.app.config.properties.HaloProperties; import run.halo.app.exception.NotFoundException; import run.halo.app.exception.ServiceException; -import run.halo.app.model.dto.AttachmentDTO; import run.halo.app.model.dto.BackupDTO; import run.halo.app.model.dto.post.BasePostDetailDTO; import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Tag; +import run.halo.app.model.support.HaloConst; import run.halo.app.service.BackupService; import run.halo.app.service.OptionService; import run.halo.app.service.PostService; @@ -148,7 +148,7 @@ public class BackupServiceImpl implements BackupService { // Zip work directory to temporary file try { // Create zip path for halo zip - String haloZipFileName = new StringBuilder().append("Halo-backup-") + String haloZipFileName = new StringBuilder().append(HaloConst.HALO_BACKUP_PREFIX) .append(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"))) .append(IdUtil.simpleUUID()) .append(".zip").toString(); @@ -158,15 +158,8 @@ public class BackupServiceImpl implements BackupService { // Zip halo run.halo.app.utils.FileUtils.zip(Paths.get(this.halo.getWorkDir()), haloZipPath); - // Build download url - String downloadUrl = buildDownloadUrl(haloZipFileName); - - // Build attachment dto - BackupDTO backup = new BackupDTO(); - backup.setDownloadUrl(downloadUrl); - backup.setFilename(haloZipFileName); - - return backup; + // Build backup dto + return buildBackupDto(haloZipPath); } catch (IOException e) { throw new ServiceException("Failed to backup halo", e); } @@ -175,19 +168,19 @@ public class BackupServiceImpl implements BackupService { @Override public List listHaloBackups() { try { - return Files.list(Paths.get(halo.getBackupDir())).map(backupPath -> { - // Get filename - String filename = backupPath.getFileName().toString(); - // Build download url - String downloadUrl = buildDownloadUrl(filename); - - // Build backup dto - BackupDTO backup = new BackupDTO(); - backup.setDownloadUrl(downloadUrl); - backup.setFilename(filename); - - return backup; - }).collect(Collectors.toList()); + // Build backup dto + return Files.list(Paths.get(halo.getBackupDir())) + .filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_BACKUP_PREFIX)) + .map(this::buildBackupDto) + .sorted((leftBackup, rightBackup) -> { + // Sort the result + if (leftBackup.getUpdateTime() < rightBackup.getUpdateTime()) { + return 1; + } else if (leftBackup.getUpdateTime() > rightBackup.getUpdateTime()) { + return -1; + } + return 0; + }).collect(Collectors.toList()); } catch (IOException e) { throw new ServiceException("Failed to fetch backups", e); } @@ -224,6 +217,28 @@ public class BackupServiceImpl implements BackupService { replaceAll("\\s", ""); } + /** + * Builds backup dto. + * + * @param backupPath backup path must not be null + * @return backup dto + */ + private BackupDTO buildBackupDto(@NonNull Path backupPath) { + Assert.notNull(backupPath, "Backup path must not be null"); + + String backupFileName = backupPath.getFileName().toString(); + BackupDTO backup = new BackupDTO(); + backup.setDownloadUrl(buildDownloadUrl(backupFileName)); + backup.setFilename(backupFileName); + try { + backup.setUpdateTime(Files.getLastModifiedTime(backupPath).toMillis()); + } catch (IOException e) { + throw new ServiceException("Failed to get last modified time of " + backupPath.toString(), e); + } + + return backup; + } + /** * Builds download url. *