Skip to content

Commit

Permalink
Explicitly set file name for camera snapshot (#23124)
Browse files Browse the repository at this point in the history
* Explicitly set file name for camera snapshot

* Process code review
jpbede authored Dec 4, 2024
1 parent 6126280 commit afcfdb5
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/dialogs/more-info/controls/more-info-camera.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit afcfdb5

Please sign in to comment.