-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
picture.js
185 lines (150 loc) · 5.39 KB
/
picture.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
const winston = require('winston');
const mime = require('mime');
const db = require('../database');
const file = require('../file');
const image = require('../image');
const meta = require('../meta');
const plugins = require('../plugins');
module.exports = function (User) {
User.getAllowedProfileImageExtensions = function () {
return User.getAllowedImageTypes().map(type => mime.getExtension(type));
};
User.getAllowedImageTypes = function () {
const allowedTypes = ['image/png', 'image/jpeg', 'image/bmp'];
if (plugins.hasListeners('filter:image.isFileTypeAllowed')) {
allowedTypes.push('image/gif');
}
return allowedTypes;
};
User.updateCoverPosition = async function (uid, position) {
// Reject anything that isn't two percentages
if (!/^[\d.]+%\s[\d.]+%$/.test(position)) {
winston.warn('[user/updateCoverPosition] Invalid position received: ' + position);
throw new Error('[[error:invalid-data]]');
}
await User.setUserField(uid, 'cover:position', position);
};
User.updateCoverPicture = async function (data) {
const picture = {
name: 'profileCover',
uid: data.uid,
};
try {
if (!data.imageData && data.position) {
return await User.updateCoverPosition(data.uid, data.position);
}
validateUpload(data, meta.config.maximumCoverImageSize, ['image/png', 'image/jpeg', 'image/bmp']);
picture.path = await image.writeImageDataToTempFile(data.imageData);
const extension = file.typeToExtension(image.mimeFromBase64(data.imageData));
const filename = data.uid + '-profilecover' + extension;
const uploadData = await image.uploadImage(filename, 'profile', picture);
await User.setUserField(data.uid, 'cover:url', uploadData.url);
if (data.position) {
await User.updateCoverPosition(data.uid, data.position);
}
return {
url: uploadData.url,
};
} finally {
await file.delete(picture.path);
}
};
// uploads a image file as profile picture
User.uploadCroppedPictureFile = async function (data) {
const userPhoto = data.file;
if (!meta.config.allowProfileImageUploads) {
throw new Error('[[error:profile-image-uploads-disabled]]');
}
if (userPhoto.size > meta.config.maximumProfileImageSize * 1024) {
throw new Error('[[error:file-too-big, ' + meta.config.maximumProfileImageSize + ']]');
}
if (!userPhoto.type || !User.getAllowedImageTypes().includes(userPhoto.type)) {
throw new Error('[[error:invalid-image]]');
}
const extension = file.typeToExtension(userPhoto.type);
if (!extension) {
throw new Error('[[error:invalid-image-extension]]');
}
const newPath = await convertToPNG(userPhoto.path);
await image.resizeImage({
path: newPath,
width: meta.config.profileImageDimension,
height: meta.config.profileImageDimension,
});
const filename = generateProfileImageFilename(data.uid, extension);
const uploadedImage = await image.uploadImage(filename, 'profile', {
uid: data.uid,
path: newPath,
name: 'profileAvatar',
});
await User.setUserFields(data.uid, {
uploadedpicture: uploadedImage.url,
picture: uploadedImage.url,
});
return uploadedImage;
};
// uploads image data in base64 as profile picture
User.uploadCroppedPicture = async function (data) {
const picture = {
name: 'profileAvatar',
uid: data.uid,
};
try {
if (!meta.config.allowProfileImageUploads) {
throw new Error('[[error:profile-image-uploads-disabled]]');
}
validateUpload(data, meta.config.maximumProfileImageSize, User.getAllowedImageTypes());
const extension = file.typeToExtension(image.mimeFromBase64(data.imageData));
if (!extension) {
throw new Error('[[error:invalid-image-extension]]');
}
picture.path = await image.writeImageDataToTempFile(data.imageData);
picture.path = await convertToPNG(picture.path);
await image.resizeImage({
path: picture.path,
width: meta.config.profileImageDimension,
height: meta.config.profileImageDimension,
});
const filename = generateProfileImageFilename(data.uid, extension);
const uploadedImage = await image.uploadImage(filename, 'profile', picture);
await User.setUserFields(data.uid, {
uploadedpicture: uploadedImage.url,
picture: uploadedImage.url,
});
return uploadedImage;
} finally {
await file.delete(picture.path);
}
};
function validateUpload(data, maxSize, allowedTypes) {
if (!data.imageData) {
throw new Error('[[error:invalid-data]]');
}
const size = image.sizeFromBase64(data.imageData);
if (size > maxSize * 1024) {
throw new Error('[[error:file-too-big, ' + maxSize + ']]');
}
const type = image.mimeFromBase64(data.imageData);
if (!type || !allowedTypes.includes(type)) {
throw new Error('[[error:invalid-image]]');
}
}
async function convertToPNG(path) {
const convertToPNG = meta.config['profile:convertProfileImageToPNG'] === 1;
if (!convertToPNG) {
return path;
}
const newPath = await image.normalise(path);
await file.delete(path);
return newPath;
}
function generateProfileImageFilename(uid, extension) {
const keepAllVersions = meta.config['profile:keepAllUserImages'] === 1;
const convertToPNG = meta.config['profile:convertProfileImageToPNG'] === 1;
return uid + '-profileavatar' + (keepAllVersions ? '-' + Date.now() : '') + (convertToPNG ? '.png' : extension);
}
User.removeCoverPicture = async function (data) {
await db.deleteObjectFields('user:' + data.uid, ['cover:url', 'cover:position']);
};
};