Skip to content

Commit

Permalink
feat: Added downloadMedia method (close #383, close #378)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Dec 3, 2020
1 parent cdd43ef commit f3e3906
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/api/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ export class Whatsapp extends ControlsLayer {
return await this.page.evaluate((data) => WAPI.downloadFile(data), data);
}

/**
* Download and returns the media content in base64 format
* @param messageId Message ou id
* @returns Base64 of media
*/
public async downloadMedia(messageId: string | Message): Promise<string> {
if (typeof messageId !== 'string') {
messageId = messageId.id;
}

return await this.page.evaluate(
(messageId) => WAPI.downloadMedia(messageId),
messageId
);
}

/**
* Get the puppeteer page instance
* @returns The Whatsapp page
Expand Down
100 changes: 100 additions & 0 deletions src/lib/wapi/functions/download-media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mMMMMMMMMMNNNmmNNNMMNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mmNMMNMMMMNNNNNmmmddhdddNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mddNMMNy:/odNmmddmmNNmdhhddmNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
NmmdNMNd:--+dNmmddhhddmmhsy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MMMMMMMMMMMMMMMMMMMMMMNMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
export async function downloadMedia(messageId) {
const msg = Store.Msg.get(messageId);

if (!msg) {
throw {
error: true,
code: 'message_not_found',
message: 'Message not found',
};
}
if (!msg.mediaData) {
throw {
error: true,
code: 'message_not_contains_media',
message: 'Message not contains media',
};
}

await msg.downloadMedia(true, 1);

let blob = null;

if (msg.mediaData.mediaBlob) {
blob = msg.mediaData.mediaBlob.forceToBlob();
} else if (msg.mediaData.filehash) {
blob = Store.BlobCache.get(msg.mediaData.filehash);
}

if (!blob) {
throw {
error: true,
code: 'media_not_found',
message: 'Media not found',
};
}

return await new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onloadend = function (e) {
resolve(reader.result);
};
reader.onabort = reject;
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
1 change: 1 addition & 0 deletions src/lib/wapi/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,4 @@ export { pinChat } from './fix-chat';
export { getSessionTokenBrowser } from './get-session-token';
export { sendMute } from './send-mute';
export { getListMute, interfaceMute } from './get-list-mute';
export { downloadMedia } from './download-media';
5 changes: 4 additions & 1 deletion src/lib/wapi/functions/process-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export async function processFiles(chat, blobs) {
if (!Array.isArray(blobs)) {
blobs = [blobs];
}
const mediaCollection = new Store.MediaCollection(chat);
const mediaCollection = new Store.MediaCollection({
chatParticipantCount: chat.getParticipantCount(),
});

await mediaCollection.processFiles(
Debug.VERSION === '0.4.613'
? blobs
Expand Down
5 changes: 5 additions & 0 deletions src/lib/wapi/store/store-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,9 @@ export const storeObjects = [
conditions: (module) =>
module.default && module.default.destroyStorage ? module.default : null,
},
{
id: 'BlobCache',
conditions: (module) =>
module.default && module.default.getOrCreateURL ? module.default : null,
},
];
2 changes: 2 additions & 0 deletions src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ import {
sendMute,
getListMute,
interfaceMute,
downloadMedia,
} from './functions';
import {
base64ToFile,
Expand Down Expand Up @@ -341,6 +342,7 @@ if (typeof window.WAPI === 'undefined') {
window.WAPI.getCommonGroups = getCommonGroups;
window.WAPI.getProfilePicFromServer = getProfilePicFromServer;
window.WAPI.downloadFile = downloadFile;
window.WAPI.downloadMedia = downloadMedia;
window.WAPI.getNumberProfile = getNumberProfile;
window.WAPI.getMessageById = getMessageById;
window.WAPI.getNewMessageId = getNewMessageId;
Expand Down
1 change: 1 addition & 0 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ interface WAPI {
) => any;
demoteParticipant: (groupId: string, contactId: string | string[]) => void;
downloadFile: (data: string) => Promise<string | boolean>;
downloadMedia: (messageId: string) => Promise<string>;
forwardMessages: (
to: string,
messages: string | string[],
Expand Down

0 comments on commit f3e3906

Please sign in to comment.