-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathfile-upload-image-rotation.ts
More file actions
133 lines (103 loc) · 5.48 KB
/
file-upload-image-rotation.ts
File metadata and controls
133 lines (103 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import path from 'path';
import type { Credentials } from '@rocket.chat/api-client';
import type { ImageAttachmentProps, IRoom, IUser, SettingValue } from '@rocket.chat/core-typings';
import { expect } from 'chai';
import { after, before, describe, it } from 'mocha';
import sharp from 'sharp';
import { getCredentials, request } from '../../data/api-data';
import { uploadFileToRC } from '../../data/file.helper';
import { getSettingValueById, updateSetting } from '../../data/permissions.helper';
import { createRoom, deleteRoom } from '../../data/rooms.helper';
import type { IRequestConfig, TestUser } from '../../data/users.helper';
import { createUser, deleteUser, login } from '../../data/users.helper';
const testImagePath = path.join(__dirname, '../../mocks/files/exif-orientation-6.jpg');
const testImageName = 'exif-orientation-6.jpg';
const downloadBuffer = async (url: string, auth: Credentials): Promise<Buffer> => {
const response = await request.get(url).set(auth).buffer(true).expect(200);
return response.body as Buffer;
};
describe('[File Upload - Image Rotation]', () => {
before((done) => getCredentials(done));
let user: TestUser<IUser>;
const userPassword = `pass${Date.now()}`;
let userCredentials: Credentials;
let testRoom: IRoom;
let rotateImagesSetting: SettingValue;
let stripExifSetting: SettingValue;
let thumbnailsEnabledSetting: SettingValue;
before(async () => {
user = await createUser({ joinDefaultChannels: false, password: userPassword });
userCredentials = await login(user.username, userPassword);
testRoom = (await createRoom({ type: 'p', name: `rotate-upload-${Date.now()}`, members: [user.username] })).body.group;
rotateImagesSetting = await getSettingValueById('FileUpload_RotateImages');
stripExifSetting = await getSettingValueById('Message_Attachments_Strip_Exif');
thumbnailsEnabledSetting = await getSettingValueById('Message_Attachments_Thumbnails_Enabled');
await updateSetting('FileUpload_RotateImages', true, false);
await updateSetting('Message_Attachments_Strip_Exif', true, false);
await updateSetting('Message_Attachments_Thumbnails_Enabled', true);
});
after(async () => {
await Promise.all([
updateSetting('FileUpload_RotateImages', rotateImagesSetting),
updateSetting('Message_Attachments_Strip_Exif', stripExifSetting),
updateSetting('Message_Attachments_Thumbnails_Enabled', thumbnailsEnabledSetting),
deleteRoom({ type: 'p', roomId: testRoom._id }),
deleteUser(user),
]);
});
it('should rotate pixels and strip EXIF orientation', async () => {
const fixtureMetadata = await sharp(testImagePath).metadata();
expect(fixtureMetadata.width).to.equal(719);
expect(fixtureMetadata.height).to.equal(479);
expect(fixtureMetadata.orientation).to.equal(6);
const requestConfig: IRequestConfig = { request, credentials: userCredentials };
const { message } = await uploadFileToRC(testRoom._id, testImagePath, 'rotation-exif-test', requestConfig);
expect(message).to.have.property('attachments');
const attachment = message.attachments?.find((item) => item.title === testImageName);
expect(attachment).to.be.an('object');
const fileUrl = (attachment as { title_link?: string }).title_link;
const thumbUrl = (attachment as ImageAttachmentProps).image_url;
expect(fileUrl).to.be.a('string');
expect(thumbUrl).to.be.a('string');
const originalBuffer = await downloadBuffer(fileUrl as string, userCredentials);
const originalMetadata = await sharp(originalBuffer).metadata();
expect(originalMetadata.width).to.equal(479);
expect(originalMetadata.height).to.equal(719);
expect(originalMetadata.exif).to.be.undefined;
});
it('should generate thumbnail from rotated image', async () => {
const requestConfig: IRequestConfig = { request, credentials: userCredentials };
const { message } = await uploadFileToRC(testRoom._id, testImagePath, 'rotation-thumb-test', requestConfig);
expect(message).to.have.property('attachments');
const attachment = message.attachments?.find((item) => item.title === testImageName);
expect(attachment).to.be.an('object');
const fileUrl = (attachment as { title_link?: string }).title_link;
const thumbUrl = (attachment as ImageAttachmentProps).image_url;
expect(fileUrl).to.be.a('string');
expect(thumbUrl).to.be.a('string');
const thumbBuffer = await downloadBuffer(thumbUrl as string, userCredentials);
const thumbMetadata = await sharp(thumbBuffer).metadata();
expect(thumbMetadata.width).to.be.lessThan(thumbMetadata.height as number);
});
describe('when FileUpload_RotateImages is disabled', () => {
before(async () => {
await updateSetting('FileUpload_RotateImages', false);
});
after(async () => {
await updateSetting('FileUpload_RotateImages', rotateImagesSetting);
});
it('should NOT rotate pixels', async () => {
const requestConfig: IRequestConfig = { request, credentials: userCredentials };
const { message } = await uploadFileToRC(testRoom._id, testImagePath, 'no-rotation-test', requestConfig);
expect(message).to.have.property('attachments');
const attachment = message.attachments?.find((item) => item.title === testImageName);
expect(attachment).to.be.an('object');
const fileUrl = (attachment as { title_link?: string }).title_link;
expect(fileUrl).to.be.a('string');
const originalBuffer = await downloadBuffer(fileUrl as string, userCredentials);
const originalMetadata = await sharp(originalBuffer).metadata();
expect(originalMetadata.width).to.equal(719);
expect(originalMetadata.height).to.equal(479);
});
});
});