Skip to content

Commit d87c7ea

Browse files
committed
fix: add serverFetchUrl method and update avatar upload logic to use Buffer
1 parent dfd3842 commit d87c7ea

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export default class OAuthPlugin extends AdminForthPlugin {
134134
return `single`;
135135
}
136136

137+
private serverFetchUrl(url: string, internalApiOrigin: string): string {
138+
return new URL(url, internalApiOrigin).toString();
139+
}
140+
137141

138142
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
139143
if (this.options.userAvatarField) {
@@ -252,30 +256,31 @@ export default class OAuthPlugin extends AdminForthPlugin {
252256
});
253257
}
254258
}
255-
256259
if ( this.options.userAvatarField && userInfo.profilePictureUrl ) {
257260
const user = await this.adminforth.resource(this.resource.resourceId).get(Filters.EQ(this.options.emailField, userInfo.email));
258261
if (user && user[this.options.userAvatarField] === null) {
259262
const avatarResponse = await fetch(userInfo.profilePictureUrl);
260-
const blob = await avatarResponse.blob();
261-
const fileType = blob.type;
263+
const fileType = avatarResponse.headers.get('content-type');
262264
const fileExtension = fileType.split('/')[1];
263265
const fileName=`avatar_${user[this.options.emailField]}_${randomUUID()}`;
264-
const file = new File([blob], fileName, { type: fileType });
266+
const avatarBuffer = Buffer.from(await avatarResponse.arrayBuffer());
265267
const { uploadUrl, uploadExtraParams, filePath, error } = await this.avatarUploadPlugin.getFileUploadUrl(
266268
fileName,
267269
fileType,
268270
null,
269271
fileExtension,
270272
null
271273
)
272-
const res = await fetch(uploadUrl, {
274+
if (error) {
275+
throw new Error(error);
276+
}
277+
const res = await fetch(this.serverFetchUrl(uploadUrl, (server as any).getInternalApiOrigin()), {
273278
method: 'PUT',
274279
headers: {
275280
'Content-Type': fileType,
276281
...uploadExtraParams
277282
},
278-
body: file
283+
body: avatarBuffer
279284
});
280285

281286
const success = res.ok;
@@ -284,11 +289,10 @@ export default class OAuthPlugin extends AdminForthPlugin {
284289
} else {
285290
await this.avatarUploadPlugin.markKeyForNotDeletion(filePath);
286291
const userResourcePrimaryKey = this.resource.columns.find(col => col.primaryKey)?.name;
287-
this.adminforth.resource(this.resource.resourceId).update(user[userResourcePrimaryKey], {[this.options.userAvatarField]: filePath} )
292+
await this.adminforth.resource(this.resource.resourceId).update(user[userResourcePrimaryKey], {[this.options.userAvatarField]: filePath} )
288293
}
289294
}
290295
}
291-
292296
return await this.doLogin(userInfo.email, response, {
293297
headers,
294298
cookies,

0 commit comments

Comments
 (0)