Catch blurhash decoding errors

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-08-12 10:11:46 +02:00
parent 70bb3f3dd7
commit 1280b66f41
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
3 changed files with 13 additions and 7 deletions

View file

@ -5,6 +5,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { IMedia } from "@/types/media.model"; import { IMedia } from "@/types/media.model";
import { PropType } from "vue";
import { Component, Prop, Vue } from "vue-property-decorator"; import { Component, Prop, Vue } from "vue-property-decorator";
import LazyImageWrapper from "../Image/LazyImageWrapper.vue"; import LazyImageWrapper from "../Image/LazyImageWrapper.vue";
@ -14,7 +15,7 @@ import LazyImageWrapper from "../Image/LazyImageWrapper.vue";
}, },
}) })
export default class EventBanner extends Vue { export default class EventBanner extends Vue {
@Prop({ required: true, default: null }) @Prop({ required: true, default: null, type: Object as PropType<IMedia> })
picture!: IMedia | null; picture!: IMedia | null;
} }
</script> </script>

View file

@ -7,17 +7,21 @@ import { decode } from "blurhash";
import { Component, Prop, Ref, Vue } from "vue-property-decorator"; import { Component, Prop, Ref, Vue } from "vue-property-decorator";
@Component @Component
export default class extends Vue { export default class BlurhashImg extends Vue {
@Prop({ type: String, required: true }) hash!: string; @Prop({ type: String, required: true }) hash!: string;
@Prop({ type: Number, default: 1 }) aspectRatio!: string; @Prop({ type: Number, default: 1 }) aspectRatio!: string;
@Ref("canvas") readonly canvas!: any; @Ref("canvas") readonly canvas!: any;
mounted(): void { mounted(): void {
try {
const pixels = decode(this.hash, 32, 32); const pixels = decode(this.hash, 32, 32);
const imageData = new ImageData(pixels, 32, 32); const imageData = new ImageData(pixels, 32, 32);
const context = this.canvas.getContext("2d"); const context = this.canvas.getContext("2d");
context.putImageData(imageData, 0, 0); context.putImageData(imageData, 0, 0);
} catch (e) {
console.error(e);
}
} }
} }
</script> </script>

View file

@ -9,6 +9,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { IMedia } from "@/types/media.model"; import { IMedia } from "@/types/media.model";
import { PropType } from "vue";
import { Component, Prop, Vue } from "vue-property-decorator"; import { Component, Prop, Vue } from "vue-property-decorator";
import LazyImage from "../Image/LazyImage.vue"; import LazyImage from "../Image/LazyImage.vue";
@ -31,7 +32,7 @@ const DEFAULT_PICTURE = {
}, },
}) })
export default class LazyImageWrapper extends Vue { export default class LazyImageWrapper extends Vue {
@Prop({ required: true }) @Prop({ required: false, type: Object as PropType<IMedia | null> })
picture!: IMedia | null; picture!: IMedia | null;
get pictureOrDefault(): Partial<IMedia> { get pictureOrDefault(): Partial<IMedia> {