Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2a2f89

Browse files
committed
refactor: rewrite filename of user avatar
This change rewites filename of user avatar to username before uploading. The reason for doing this is that we don't need to deal with url reset in the cookie for the keystone-plugin when url changes.
1 parent adeb560 commit a2a2f89

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

fields/types/gcsavatar/GcsAvatarType.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,18 @@ gcsavatar.prototype.uploadFile = function(item, file, update, callback) {
236236
const publicRead = _this.options.publicRead ? _this.options.publicRead : false;
237237
let filename = file.name;
238238
const split = filename.split('.');
239-
const filenameWithoutExt = split[0];
239+
const fileExt = split[1];
240+
241+
// Overwrite filename with username (i.e. email account prefix).
242+
// e.g. When an user uploads the avatar with email '[email protected]',
243+
// the filename of the uploaded image would be 'alice.jpg'.
244+
// The reason for doing this is that we don't need to deal with url
245+
// reset in the cookie for the keystone-plugin.
246+
const email = _.get(item, '_doc.email');
247+
const username = email.match(/^([^@]*)@/)[1];
248+
const filenameWithoutExt = username;
249+
filename = `${username}.${fileExt}`;
250+
240251
const originalname = file.originalname;
241252
const filetype = file.mimetype || file.type;
242253
let gcsDir = this.options.destination ? this.options.destination : '';

0 commit comments

Comments
 (0)