Skip to content

Commit

Permalink
feat: Added WPP.chat.downloadMedia function
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Dec 30, 2021
1 parent da30052 commit 712095a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
62 changes: 62 additions & 0 deletions src/chat/functions/downloadMedia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*!
* Copyright 2021 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { WPPError } from '../../util';
import { MediaBlobCache } from '../../whatsapp';
import { getMessageById } from '.';

/**
* Download the blob of a media message
*
* @category Message
*/
export async function downloadMedia(id: string): Promise<any> {
const msg = await getMessageById(id);

if (!msg.mediaData) {
throw new WPPError(
'message_not_contains_media',
`Message ${id} not contains media`,
{
id,
}
);
}

await msg.downloadMedia({
downloadEvenIfExpensive: true,
rmrReason: 1,
isUserInitiated: true,
});

let blob = null;

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

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

return blob;
}
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export { clear } from './clear';
export { delete } from './delete';
export { deleteMessage } from './deleteMessage';
export { downloadMedia } from './downloadMedia';
export { find } from './find';
export { generateMessageID } from './generateMessageID';
export { get } from './get';
Expand Down
6 changes: 4 additions & 2 deletions src/whatsapp/models/MsgModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import { TextFontStyle } from '../../enums';
import { ButtonCollection, MsgCollection } from '../collections';
import { exportProxyModel } from '../exportModule';
import { MsgKey, Wid } from '../misc';
import { MediaObject, MsgKey, Wid } from '../misc';
import { MediaDataModel } from '.';
import {
Model,
ModelOptions,
Expand Down Expand Up @@ -226,7 +227,7 @@ interface Session {
startOfDaySkew?: any;
isQuotedMsgAvailable: boolean;
senderObj?: any;
mediaData?: any;
mediaData?: MediaDataModel;
forwardedFromWeb?: any;
linksIndexParsed?: any;
}
Expand Down Expand Up @@ -304,6 +305,7 @@ export declare class MsgModel extends Model {
proterties: ModelPropertiesContructor<MsgModel, 'id'>,
options?: ModelOptions
);
mediaObject?: MediaObject;
getLinks(e?: number): any;
getHeaderLinks(): any;
getFooterLinks(): any;
Expand Down

0 comments on commit 712095a

Please sign in to comment.