Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[FEATURE] Handle non-image files gracefully with Blurhash.encode #189

Open
Open
[FEATURE] Handle non-image files gracefully with Blurhash.encode#189
Labels
enhancementNew feature or request

Description

Problem

If you feed a non-image file into Blurhash.encode:

const nonImage = 'file:///path/to/random.mp4`;
const blurhash = await Blurhash.encode(nonImage, 4, 3);

The app completely crashes with no error logs. Threw me for a loop for a few hours until I pinpointed the crash and problem with my code.

Solution

Ideally if you feed a non-image file into the blurhash.encode it should check first if it is a valid image and then throw a console.error if it isn't, instead of crashing the app with no logs.

Describe alternatives you've considered
I added the following function:

import * as mime from "react-native-mime-types"

export async function isValidImage(url: string) {
/*
For some reason on Android, fetch - blob doesn't work and returns no mimetype, but it works on iOS
In order to check on android, use the mime.lookup(url) which coincidentally only works on Android and not iOS
*/
try {
const response = await fetch(url)
const blob = await response.blob()
return blob.type.startsWith("image/") || mime.lookup(url).toString().startsWith("image")
} catch (error) {
console.error("Blurhash doesn't work for files that aren't images!")
return false
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions