Skip to content

exif-js -> exifreader #117

@ornic

Description

@ornic

There is an example for those who want to move from buggy exif-js.

Current code: https://github.com/kirill3333/react-avatar/blob/master/src/avatar.jsx#L198-L209

import EXIF from "exif-js";
...
  onFileLoad(e) {
    e.preventDefault();

    this.onBeforeFileLoadCallback(e);
    if (!e.target.value) return;

    let file = e.target.files[0];

    this.onFileLoadCallback(file);

    const ref = this;
    EXIF.getData(file, function () {
      let exifOrientation = EXIF.getTag(this, "Orientation");
      LoadImage(
        file,
        function (image, data) {
          ref.setState({ image, file, showLoader: false }, () => {
            ref.init();
          });
        },
        { orientation: exifOrientation, meta: true }
      );
    });
  }

Code with exifreader 4.13.0:

import ExifReader from "exifreader";
...
  onFileLoad(e) {
    e.preventDefault();

    this.onBeforeFileLoadCallback(e);
    if (!e.target.value) return;

    let file = e.target.files[0];

    this.onFileLoadCallback(file);

    const ref = this;
    let exifOrientation = 1;
    ExifReader.load(file)
      .then(
        tags => exifOrientation = tags["Orientation"]?.value || 1,
        error => console.warn('Error reading exif data, using defaults', error)
      )
      .finally(
        () => {
          LoadImage(
            file,
            function (image, data) {
              ref.setState({ image, file, showLoader: false }, () => {
                ref.init();
              });
            },
            { orientation: exifOrientation, meta: true }
          )
        }
      );
  }

Changes begin after const ref = this;: we do 2 steps on the stairs to the promise hell and voi la!

P.S. LoadImage call is unchanged, just moved inside .finally callback to wait for promise from ExifReader.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions