mirror of https://github.com/halo-dev/halo
Add updateTime property into BackupDTO
parent
079928feed
commit
ec5f8f9b07
|
@ -14,4 +14,6 @@ public class BackupDTO {
|
||||||
private String downloadUrl;
|
private String downloadUrl;
|
||||||
|
|
||||||
private String filename;
|
private String filename;
|
||||||
|
|
||||||
|
private long updateTime;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,11 @@ public class HaloConst {
|
||||||
*/
|
*/
|
||||||
public final static String TEMP_DIR = System.getProperties().getProperty("java.io.tmpdir");
|
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.
|
* Default theme name.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -15,11 +15,11 @@ import org.yaml.snakeyaml.Yaml;
|
||||||
import run.halo.app.config.properties.HaloProperties;
|
import run.halo.app.config.properties.HaloProperties;
|
||||||
import run.halo.app.exception.NotFoundException;
|
import run.halo.app.exception.NotFoundException;
|
||||||
import run.halo.app.exception.ServiceException;
|
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.BackupDTO;
|
||||||
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
||||||
import run.halo.app.model.entity.Post;
|
import run.halo.app.model.entity.Post;
|
||||||
import run.halo.app.model.entity.Tag;
|
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.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;
|
||||||
|
@ -148,7 +148,7 @@ public class BackupServiceImpl implements BackupService {
|
||||||
// Zip work directory to temporary file
|
// Zip work directory to temporary file
|
||||||
try {
|
try {
|
||||||
// Create zip path for halo zip
|
// 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(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")))
|
||||||
.append(IdUtil.simpleUUID())
|
.append(IdUtil.simpleUUID())
|
||||||
.append(".zip").toString();
|
.append(".zip").toString();
|
||||||
|
@ -158,15 +158,8 @@ public class BackupServiceImpl implements BackupService {
|
||||||
// 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.halo.getWorkDir()), haloZipPath);
|
||||||
|
|
||||||
// Build download url
|
// Build backup dto
|
||||||
String downloadUrl = buildDownloadUrl(haloZipFileName);
|
return buildBackupDto(haloZipPath);
|
||||||
|
|
||||||
// Build attachment dto
|
|
||||||
BackupDTO backup = new BackupDTO();
|
|
||||||
backup.setDownloadUrl(downloadUrl);
|
|
||||||
backup.setFilename(haloZipFileName);
|
|
||||||
|
|
||||||
return backup;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ServiceException("Failed to backup halo", e);
|
throw new ServiceException("Failed to backup halo", e);
|
||||||
}
|
}
|
||||||
|
@ -175,18 +168,18 @@ public class BackupServiceImpl implements BackupService {
|
||||||
@Override
|
@Override
|
||||||
public List<BackupDTO> listHaloBackups() {
|
public List<BackupDTO> listHaloBackups() {
|
||||||
try {
|
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
|
// Build backup dto
|
||||||
BackupDTO backup = new BackupDTO();
|
return Files.list(Paths.get(halo.getBackupDir()))
|
||||||
backup.setDownloadUrl(downloadUrl);
|
.filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_BACKUP_PREFIX))
|
||||||
backup.setFilename(filename);
|
.map(this::buildBackupDto)
|
||||||
|
.sorted((leftBackup, rightBackup) -> {
|
||||||
return backup;
|
// Sort the result
|
||||||
|
if (leftBackup.getUpdateTime() < rightBackup.getUpdateTime()) {
|
||||||
|
return 1;
|
||||||
|
} else if (leftBackup.getUpdateTime() > rightBackup.getUpdateTime()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ServiceException("Failed to fetch backups", e);
|
throw new ServiceException("Failed to fetch backups", e);
|
||||||
|
@ -224,6 +217,28 @@ public class BackupServiceImpl implements BackupService {
|
||||||
replaceAll("\\s", "");
|
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.
|
* Builds download url.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue