-
Notifications
You must be signed in to change notification settings - Fork 13.8k
fix: Image orientation issues #38082
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7a2194
fix: image orientation when stripping exif
yash-rajpal 4ab0446
add changeset
yash-rajpal 5c31804
Merge branch 'develop' into fix/image-orientation
dougfabris 9852db5
Merge branch 'develop' into fix/image-orientation
yash-rajpal 48b0c00
Merge branch 'develop' into fix/image-orientation
yash-rajpal 4de70cd
use streams
yash-rajpal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fixes image orientation issues which were related to `Message_Attachments_Strip_Exif` setting. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import URL from 'url'; | |
| import { hashLoginToken } from '@rocket.chat/account-utils'; | ||
| import { Apps, AppEvents } from '@rocket.chat/apps'; | ||
| import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions'; | ||
| import { Media } from '@rocket.chat/core-services'; | ||
| import { isE2EEUpload, type IUpload } from '@rocket.chat/core-typings'; | ||
| import { Users, Avatars, UserDataFiles, Uploads, Settings, Subscriptions, Messages, Rooms } from '@rocket.chat/models'; | ||
| import type { NextFunction } from 'connect'; | ||
|
|
@@ -404,6 +405,14 @@ export const FileUpload = { | |
|
|
||
| await reorientation(); | ||
|
|
||
| const stripExif = settings.get('Message_Attachments_Strip_Exif'); | ||
|
|
||
| if (stripExif) { | ||
| const fileBuffer = fs.readFileSync(tmpFile); | ||
| const strippedFileBuffer = await Media.stripExifFromBuffer(fileBuffer); | ||
| fs.writeFileSync(tmpFile, strippedFileBuffer); | ||
| } | ||
|
Comment on lines
+410
to
+441
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clean up debug artifacts and add missing error handling. Several issues in the EXIF stripping logic:
🛠️ Proposed fix if (stripExif) {
const exifTmpPath = `${tmpFile}.exif-stripped`;
try {
await new Promise<void>((resolve, reject) => {
const readStream = fs.createReadStream(tmpFile);
const writeStream = fs.createWriteStream(exifTmpPath);
const exifTransformer = new ExifTransformer();
readStream.pipe(exifTransformer).pipe(writeStream);
writeStream.on('finish', () => resolve());
readStream.on('error', reject);
+ exifTransformer.on('error', reject);
writeStream.on('error', reject);
});
- // No need to check mime. Library will ignore any files without exif/xmp tags (like BMP, ico, PDF, etc)
- // const exifTransformer = new ExifTransformer();
- // const readStream = fs.createReadStream(tmpFile);
- // const writeStream = fs.createWriteStream(exifTmpPath);
-
- // readStream.pipe(exifTransformer).pipe(writeStream);
-
- // await pipeline(fs.createReadStream(tmpFile), exifTransformer, fs.createWriteStream(exifTmpPath));
await rename(exifTmpPath, tmpFile);
} catch (err) {
- console.log('ERROR', err);
- await unlink(exifTmpPath);
+ await unlink(exifTmpPath).catch(() => {});
SystemLogger.error(`Error stripping exif from image: ${err}`);
}
}🤖 Prompt for AI Agents |
||
|
|
||
| const { size } = await fs.lstatSync(tmpFile); | ||
| await this.getCollection().updateOne( | ||
| { _id: file._id }, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use the sync versions of the FS methods, especially when writing a potentially large amount of data, as it blocks the event loop.
Also, here the server will end up buffering the file contents 2 additional times: one reading the file, and another one with the MediaService call. You could unify this by calling
Media.stripExifFromStream, passing a read stream fromfs.createReadStream- this would prevent any buffering from happeningThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from that,
sharpalready removes exif metadata when rotating the image, so we don't need to call the media service in those cases