@@ -10,19 +10,7 @@ const file = require('../file');
1010const image = require ( '../image' ) ;
1111const meta = require ( '../meta' ) ;
1212
13- module . exports = function ( User ) {
14- User . getAllowedProfileImageExtensions = function ( ) {
15- const exts = User . getAllowedImageTypes ( ) . map ( type => mime . getExtension ( type ) ) ;
16- if ( exts . includes ( 'jpeg' ) ) {
17- exts . push ( 'jpg' ) ;
18- }
19- return exts ;
20- } ;
21-
22- User . getAllowedImageTypes = function ( ) {
23- return [ 'image/png' , 'image/jpeg' , 'image/bmp' , 'image/gif' ] ;
24- } ;
25-
13+ function initCoverMethods ( User ) {
2614 User . updateCoverPosition = async function ( uid , position ) {
2715 // Reject anything that isn't two percentages
2816 if ( ! / ^ [ \d . ] + % \s [ \d . ] + % $ / . test ( position ) ) {
@@ -52,7 +40,7 @@ module.exports = function (User) {
5240 const filename = `${ data . uid } -profilecover-${ Date . now ( ) } ${ extension } ` ;
5341 const uploadData = await image . uploadImage ( filename , `profile/uid-${ data . uid } ` , picture ) ;
5442
55- await deleteCurrentPicture ( data . uid , 'cover:url' ) ;
43+ await deleteCurrentPicture ( User , data . uid , 'cover:url' ) ;
5644 await User . setUserField ( data . uid , 'cover:url' , uploadData . url ) ;
5745
5846 if ( data . position ) {
@@ -66,7 +54,13 @@ module.exports = function (User) {
6654 await file . delete ( picture . path ) ;
6755 }
6856 } ;
57+ User . removeCoverPicture = async function ( data ) {
58+ await deletePicture ( User , data . uid , 'cover:url' ) ;
59+ await db . deleteObjectFields ( `user:${ data . uid } ` , [ 'cover:url' , 'cover:position' ] ) ;
60+ } ;
61+ }
6962
63+ function initProfileImageMethods ( User ) {
7064 // uploads a image file as profile picture
7165 User . uploadCroppedPictureFile = async function ( data ) {
7266 const userPhoto = data . file ;
@@ -102,15 +96,14 @@ module.exports = function (User) {
10296 name : 'profileAvatar' ,
10397 } ) ;
10498
105- await deleteCurrentPicture ( data . uid , 'uploadedpicture' ) ;
99+ await deleteCurrentPicture ( User , data . uid , 'uploadedpicture' ) ;
106100 await User . updateProfile ( data . callerUid , {
107101 uid : data . uid ,
108102 uploadedpicture : uploadedImage . url ,
109103 picture : uploadedImage . url ,
110104 } , [ 'uploadedpicture' , 'picture' ] ) ;
111105 return uploadedImage ;
112106 } ;
113-
114107 // uploads image data in base64 as profile picture
115108 User . uploadCroppedPicture = async function ( data ) {
116109 const picture = {
@@ -142,7 +135,7 @@ module.exports = function (User) {
142135 const filename = generateProfileImageFilename ( data . uid , extension ) ;
143136 const uploadedImage = await image . uploadImage ( filename , `profile/uid-${ data . uid } ` , picture ) ;
144137
145- await deleteCurrentPicture ( data . uid , 'uploadedpicture' ) ;
138+ await deleteCurrentPicture ( User , data . uid , 'uploadedpicture' ) ;
146139 await User . updateProfile ( data . callerUid , {
147140 uid : data . uid ,
148141 uploadedpicture : uploadedImage . url ,
@@ -153,81 +146,95 @@ module.exports = function (User) {
153146 await file . delete ( picture . path ) ;
154147 }
155148 } ;
156-
157- async function deleteCurrentPicture ( uid , field ) {
158- if ( meta . config [ 'profile:keepAllUserImages' ] ) {
159- return ;
160- }
161- await deletePicture ( uid , field ) ;
162- }
163-
164- async function deletePicture ( uid , field ) {
165- const uploadPath = await getPicturePath ( uid , field ) ;
166- if ( uploadPath ) {
167- await file . delete ( uploadPath ) ;
168- }
169- }
170-
171- function validateUpload ( data , maxSize , allowedTypes ) {
172- if ( ! data . imageData ) {
173- throw new Error ( '[[error:invalid-data]]' ) ;
174- }
175- const size = image . sizeFromBase64 ( data . imageData ) ;
176- if ( size > maxSize * 1024 ) {
177- throw new Error ( `[[error:file-too-big, ${ maxSize } ]]` ) ;
178- }
179-
180- const type = image . mimeFromBase64 ( data . imageData ) ;
181- if ( ! type || ! allowedTypes . includes ( type ) ) {
182- throw new Error ( '[[error:invalid-image]]' ) ;
183- }
184- }
185-
186- async function convertToPNG ( path ) {
187- const convertToPNG = meta . config [ 'profile:convertProfileImageToPNG' ] === 1 ;
188- if ( ! convertToPNG ) {
189- return path ;
190- }
191- const newPath = await image . normalise ( path ) ;
192- await file . delete ( path ) ;
193- return newPath ;
194- }
195-
196- function generateProfileImageFilename ( uid , extension ) {
197- const convertToPNG = meta . config [ 'profile:convertProfileImageToPNG' ] === 1 ;
198- return `${ uid } -profileavatar-${ Date . now ( ) } ${ convertToPNG ? '.png' : extension } ` ;
199- }
200-
201- User . removeCoverPicture = async function ( data ) {
202- await deletePicture ( data . uid , 'cover:url' ) ;
203- await db . deleteObjectFields ( `user:${ data . uid } ` , [ 'cover:url' , 'cover:position' ] ) ;
204- } ;
205-
206149 User . removeProfileImage = async function ( uid ) {
207150 const userData = await User . getUserFields ( uid , [ 'uploadedpicture' , 'picture' ] ) ;
208- await deletePicture ( uid , 'uploadedpicture' ) ;
151+ await deletePicture ( User , uid , 'uploadedpicture' ) ;
209152 await User . setUserFields ( uid , {
210153 uploadedpicture : '' ,
211154 // if current picture is uploaded picture, reset to user icon
212155 picture : userData . uploadedpicture === userData . picture ? '' : userData . picture ,
213156 } ) ;
214157 return userData ;
215158 } ;
159+ }
216160
161+ function initGetters ( User ) {
162+ User . getAllowedProfileImageExtensions = function ( ) {
163+ const exts = User . getAllowedImageTypes ( ) . map ( type => mime . getExtension ( type ) ) ;
164+ if ( exts . includes ( 'jpeg' ) ) {
165+ exts . push ( 'jpg' ) ;
166+ }
167+ return exts ;
168+ } ;
169+
170+ User . getAllowedImageTypes = function ( ) {
171+ return [ 'image/png' , 'image/jpeg' , 'image/bmp' , 'image/gif' ] ;
172+ } ;
173+
217174 User . getLocalCoverPath = async function ( uid ) {
218- return getPicturePath ( uid , 'cover:url' ) ;
175+ return getPicturePath ( User , uid , 'cover:url' ) ;
219176 } ;
220177
221178 User . getLocalAvatarPath = async function ( uid ) {
222- return getPicturePath ( uid , 'uploadedpicture' ) ;
179+ return getPicturePath ( User , uid , 'uploadedpicture' ) ;
223180 } ;
181+ }
224182
225- async function getPicturePath ( uid , field ) {
226- const value = await User . getUserField ( uid , field ) ;
227- if ( ! value || ! value . startsWith ( `${ nconf . get ( 'relative_path' ) } /assets/uploads/profile/uid-${ uid } ` ) ) {
228- return false ;
229- }
230- const filename = value . split ( '/' ) . pop ( ) ;
231- return path . join ( nconf . get ( 'upload_path' ) , `profile/uid-${ uid } ` , filename ) ;
183+ function validateUpload ( data , maxSize , allowedTypes ) {
184+ if ( ! data . imageData ) {
185+ throw new Error ( '[[error:invalid-data]]' ) ;
186+ }
187+ const size = image . sizeFromBase64 ( data . imageData ) ;
188+ if ( size > maxSize * 1024 ) {
189+ throw new Error ( `[[error:file-too-big, ${ maxSize } ]]` ) ;
190+ }
191+
192+ const type = image . mimeFromBase64 ( data . imageData ) ;
193+ if ( ! type || ! allowedTypes . includes ( type ) ) {
194+ throw new Error ( '[[error:invalid-image]]' ) ;
195+ }
196+ }
197+ function generateProfileImageFilename ( uid , extension ) {
198+ const convertToPNG = meta . config [ 'profile:convertProfileImageToPNG' ] === 1 ;
199+ return `${ uid } -profileavatar-${ Date . now ( ) } ${ convertToPNG ? '.png' : extension } ` ;
200+ }
201+
202+ async function getPicturePath ( User , uid , field ) {
203+ const value = await User . getUserField ( uid , field ) ;
204+ if ( ! value || ! value . startsWith ( `${ nconf . get ( 'relative_path' ) } /assets/uploads/profile/uid-${ uid } ` ) ) {
205+ return false ;
206+ }
207+ const filename = value . split ( '/' ) . pop ( ) ;
208+ return path . join ( nconf . get ( 'upload_path' ) , `profile/uid-${ uid } ` , filename ) ;
209+ }
210+
211+
212+ async function deleteCurrentPicture ( User , uid , field ) {
213+ if ( meta . config [ 'profile:keepAllUserImages' ] ) {
214+ return ;
215+ }
216+ await deletePicture ( User , uid , field ) ;
217+ }
218+
219+ async function deletePicture ( User , uid , field ) {
220+ const uploadPath = await getPicturePath ( User , uid , field ) ;
221+ if ( uploadPath ) {
222+ await file . delete ( uploadPath ) ;
232223 }
233- } ;
224+ }
225+
226+ async function convertToPNG ( path ) {
227+ const convertToPNG = meta . config [ 'profile:convertProfileImageToPNG' ] === 1 ;
228+ if ( ! convertToPNG ) {
229+ return path ;
230+ }
231+ const newPath = await image . normalise ( path ) ;
232+ await file . delete ( path ) ;
233+ return newPath ;
234+ }
235+
236+ module . exports = function ( User ) {
237+ initGetters ( User ) ;
238+ initCoverMethods ( User ) ;
239+ initProfileImageMethods ( User ) ;
240+ } ;
0 commit comments