Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement]: Resources Gallery View: Respect orientation embedded in image files #13

Open
pkerschbaum opened this issue Jan 13, 2022 · 0 comments
Labels
good first issue Good for newcomers kind/enhancement New feature or request

Comments

@pkerschbaum
Copy link
Owner

Current Behavior

Image thumbnails shown in the Resources Gallery View are shown in the orientation as read from the disk.

Desired Behavior

Image files might contain a orientation in their EXIF metadata. Read the orientation from that metadata an display the image thumbnail rotated accordingly.
There are many options for an EXIF image parser library to choose from: https://www.npmjs.com/search?ranking=optimal&q=exif

Implementation Notes

  • The image thumbnails are retrieved by the UI via a custom electron protocol. I think we just should read and rotate the image thumbnail here:
    const THUMBNAIL_RESIZE_BLOCKLIST = ['image/svg+xml'];
    async function getThumbnail(request: electron.ProtocolRequest): Promise<ProtocolStreamResponse> {
    const parsed = new URL(request.url);
    // property "pathname" has a leading slash --> remove that (https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname)
    const path = parsed.pathname.substring(1);
    const uri = URI.parse(decodeURIComponent(path));
    const requestedHeight = parsed.searchParams.get('height');
    const parsedHeight = numbers.convert(requestedHeight);
    invariant(parsedHeight !== undefined);
    const mimeTypeBasedOnContent = (await FileType.fromFile(uri.fsPath))?.mime;
    const extension = uriHelper.extractExtension(uri);
    const mimeTypeBasedOnExtension = check.isNullishOrEmptyString(extension)
    ? undefined
    : mime.getType(extension);
    const finalMimeType = mimeTypeBasedOnContent ?? mimeTypeBasedOnExtension ?? undefined;
    let thumbnailStream;
    if (finalMimeType === undefined || THUMBNAIL_RESIZE_BLOCKLIST.includes(finalMimeType)) {
    thumbnailStream = fs.createReadStream(uri.fsPath);
    } else {
    thumbnailStream = fs
    .createReadStream(uri.fsPath)
    .pipe(sharp().resize({ height: parsedHeight }));
    }
    return {
    data: thumbnailStream,
    mimeType: finalMimeType,
    headers: {
    'cache-control': `max-age=${60 * 60 * 24}`, // cache for 1 day
    },
    };
    }
@pkerschbaum pkerschbaum added the kind/enhancement New feature or request label Jan 13, 2022
@pkerschbaum pkerschbaum changed the title [Enhancement] Resources Gallery View: Respect orientation embedded in image files [Enhancement]: Resources Gallery View: Respect orientation embedded in image files Jan 14, 2022
@pkerschbaum pkerschbaum added the good first issue Good for newcomers label Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant