Explicitly set file name for camera snapshot (#23124)

* Explicitly set file name for camera snapshot

* Process code review
This commit is contained in:
Jan-Philipp Benecke 2024-12-04 07:52:53 +01:00 committed by Bram Kragten
parent 257cab1061
commit 6c9cfed49f

View File

@ -8,6 +8,7 @@ import "../../../components/buttons/ha-progress-button";
import { UNAVAILABLE } from "../../../data/entity";
import { fileDownload } from "../../../util/file_download";
import { showToast } from "../../../util/toast";
import { slugify } from "../../../common/string/slugify";
class MoreInfoCamera extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@ -69,9 +70,14 @@ class MoreInfoCamera extends LitElement {
throw new Error("No response from API");
}
const contentType = result.headers.get("content-type");
const ext = contentType === "image/png" ? "png" : "jpg";
const date = new Date().toLocaleString();
const filename = `snapshot_${slugify(this.stateObj!.entity_id)}_${date}.${ext}`;
const blob = await result.blob();
const url = window.URL.createObjectURL(blob);
fileDownload(url);
fileDownload(url, filename);
} catch (err) {
this._waiting = false;
button.actionError();