react-nativecamera-roll

Determining file type from react-native-cameraroll on ios


I am using react-native-cameraroll to retrieve images/gifs from an iOS device like so:

const request: GetPhotosParams = {
    first: amountOfPhotos,
    groupTypes,
    assetType: 'All'
};

if (albumTitle !== null) {
    request.groupName = albumTitle;
}

if (after !== null) {
    request.after = after;
}
request.include = ['fileSize', 'playableDuration'];

const result = await CameraRoll.getPhotos(request);

This request returns data that looks like:

"node": {
    "timestamp": 1648482721.502593,
        "type": "image",
        "group_name": "All Photos",
        "location": null,
        "image": {
        "width": null,
            "height": null,
            "filename": null,
            "fileSize": 878850,
            "playableDuration": null,
            "uri": "ph://7CC97C49-2E25-4578-9DE8-92F81821B17A/L0/001"
    }
}

I need a way to determine if whatever is at that uri is an image or a gif. Is this possible?


Solution

  • If I was to do it, I would try to get the mime-type of the resource.

    You can do something like this to get the mime-type using fetch.

    fetch("https://mir-s3-cdn-cf.behance.net/project_modules/max_1200/5eeea355389655.59822ff824b72.gif").then((res) => console.log(res.headers.get("content-type")));

    I had the same issue in a project where I ended up using "react-native-image-picker" instead. (https://github.com/react-native-image-picker/react-native-image-picker)