Improve download backup (#22905)

This commit is contained in:
Paul Bottein 2024-11-20 12:34:30 +01:00 committed by GitHub
parent 918fca4d0a
commit 67a5152c36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 6 deletions

View File

@ -35,8 +35,8 @@ export type GenerateBackupParams = {
password?: string;
};
export const getBackupDownloadUrl = (id: string) =>
`/api/backup/download/${id}`;
export const getBackupDownloadUrl = (id: string, agentId: string) =>
`/api/backup/download/${id}?agent_id=${agentId}`;
export const fetchBackupInfo = (hass: HomeAssistant): Promise<BackupInfo> =>
hass.callWS({
@ -89,3 +89,10 @@ export const uploadBackup = async (
throw new Error(`${resp.status} ${resp.statusText}`);
}
};
export const getPreferredAgentForDownload = (agents: string[]) => {
const localAgents = agents.filter(
(agent) => agent.split(".")[0] === "backup"
);
return localAgents[0] || agents[0];
};

View File

@ -26,6 +26,7 @@ import {
getBackupDownloadUrl,
removeBackup,
type BackupContent,
getPreferredAgentForDownload,
} from "../../../data/backup";
import { extractApiErrorMessage } from "../../../data/hassio/common";
import {
@ -290,9 +291,10 @@ class HaConfigBackupDashboard extends SubscribeMixin(LitElement) {
}
private async _downloadBackup(backup: BackupContent): Promise<void> {
const preferedAgent = getPreferredAgentForDownload(backup!.agent_ids!);
const signedUrl = await getSignedPath(
this.hass,
getBackupDownloadUrl(backup.backup_id)
getBackupDownloadUrl(backup.backup_id, preferedAgent)
);
fileDownload(signedUrl.path);
}

View File

@ -1,7 +1,7 @@
import type { ActionDetail } from "@material/mwc-list";
import { mdiDelete, mdiDotsVertical, mdiDownload } from "@mdi/js";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import type { ActionDetail } from "@material/mwc-list";
import { formatDateTime } from "../../../common/datetime/format_date_time";
import { navigate } from "../../../common/navigate";
import "../../../components/ha-alert";
@ -17,6 +17,7 @@ import type { BackupContent } from "../../../data/backup";
import {
fetchBackupDetails,
getBackupDownloadUrl,
getPreferredAgentForDownload,
removeBackup,
} from "../../../data/backup";
import { domainToName } from "../../../data/integration";
@ -130,6 +131,25 @@ class HaConfigBackupDetails extends LitElement {
slot="start"
/>
<div slot="headline">${domainName}: ${name}</div>
<ha-button-menu
slot="end"
@action=${this._handleAgentAction}
.agent=${agent}
fixed
>
<ha-icon-button
slot="trigger"
.label=${this.hass.localize("ui.common.menu")}
.path=${mdiDotsVertical}
></ha-icon-button>
<ha-list-item graphic="icon">
<ha-svg-icon
slot="graphic"
.path=${mdiDownload}
></ha-svg-icon>
Download from this location
</ha-list-item>
</ha-button-menu>
</ha-md-list-item>
`;
})}
@ -162,10 +182,18 @@ class HaConfigBackupDetails extends LitElement {
}
}
private async _downloadBackup(): Promise<void> {
private _handleAgentAction(ev: CustomEvent<ActionDetail>) {
const button = ev.currentTarget;
const agentId = (button as any).agent;
this._downloadBackup(agentId);
}
private async _downloadBackup(agentId?: string): Promise<void> {
const preferedAgent =
agentId ?? getPreferredAgentForDownload(this._backup!.agent_ids!);
const signedUrl = await getSignedPath(
this.hass,
getBackupDownloadUrl(this._backup!.backup_id)
getBackupDownloadUrl(this._backup!.backup_id, preferedAgent)
);
fileDownload(signedUrl.path);
}