Skip to content

Commit

Permalink
Save the FB profile image as a data URI
Browse files Browse the repository at this point in the history
  • Loading branch information
micahflee committed Feb 8, 2025
1 parent 84ea669 commit 0fc0e29
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/account_facebook/facebook_account_controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'

import fetch from 'node-fetch';
import { session } from 'electron'
import log from 'electron-log/main';
import Database from 'better-sqlite3'
Expand Down Expand Up @@ -210,6 +211,19 @@ export class FacebookAccountController {
return this.cookies[name] || null;
}

async getProfileImageDataURI(profilePictureURI: string): Promise<string> {
try {
const response = await fetch(profilePictureURI, {});
if (!response.ok) {
return "";
}
const buffer = await response.buffer();
return `data:${response.headers.get('content-type')};base64,${buffer.toString('base64')}`;
} catch {
return "";
}
}

async getConfig(key: string): Promise<string | null> {
return getConfig(key, this.db);
}
Expand Down
9 changes: 9 additions & 0 deletions src/account_facebook/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ export const defineIPCFacebook = () => {
}
});

ipcMain.handle('Facebook:getProfileImageDataURI', async (_, accountID: number, profilePictureURI: string): Promise<string> => {
try {
const controller = getFacebookAccountController(accountID);
return await controller.getProfileImageDataURI(profilePictureURI);
} catch (error) {
throw new Error(packageExceptionForReport(error as Error));
}
});

ipcMain.handle('Facebook:getConfig', async (_, accountID: number, key: string): Promise<string | null> => {
try {
const controller = getFacebookAccountController(accountID);
Expand Down
3 changes: 3 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ contextBridge.exposeInMainWorld('electron', {
getCookie: (accountID: number, name: string): Promise<string | null> => {
return ipcRenderer.invoke('Facebook:getCookie', accountID, name);
},
getProfileImageDataURI: (profilePictureURI: string): Promise<string> => {
return ipcRenderer.invoke('Facebook:getProfileImageDataURI', profilePictureURI);
},
getConfig: (accountID: number, key: string): Promise<string | null> => {
return ipcRenderer.invoke('Facebook:getConfig', accountID, key);
},
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ declare global {
syncProgress: (accountID: number, progressJSON: string) => Promise<void>;
getProgress: (accountID: number) => Promise<FacebookProgress>;
getCookie: (accountID: number, name: string) => Promise<string | null>;
getProfileImageDataURI: (accountID: number, profilePictureURI: string) => Promise<string>;
getConfig: (accountID: number, key: string) => Promise<string | null>;
setConfig: (accountID: number, key: string, value: string) => Promise<void>;
},
Expand Down
11 changes: 7 additions & 4 deletions src/renderer/src/view_models/FacebookViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,17 @@ export class FacebookViewModel extends BaseViewModel {
this.account.facebookAccount.name = currentUserInitialData.NAME;
this.account.facebookAccount.accountID = currentUserInitialData.ACCOUNT_ID;
}
await window.electron.database.saveAccount(JSON.stringify(this.account));
this.log("login", "saved name and account ID");

// Get the user's profile image
const profilePictureURI = findProfilePictureURI(facebookData);
console.log('profilePictureURI', profilePictureURI);
if (profilePictureURI && this.account && this.account.facebookAccount) {
this.account.facebookAccount.profileImageDataURI = await window.electron.Facebook.getProfileImageDataURI(this.account.id, profilePictureURI);
}

this.pause();
await this.waitForPause();
await window.electron.database.saveAccount(JSON.stringify(this.account));

await this.waitForPause();
}

async runJobLogin(jobIndex: number): Promise<boolean> {
Expand Down

0 comments on commit 0fc0e29

Please sign in to comment.