Repository navigation
Stream media downloads and add --url and --from-event #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,110 @@ | ||
| import {Args, Flags} from '@oclif/core' | ||
| import {PolymorfaValidationError} from '@polymorfa/sdk' | ||
|
|
||
| import {BaseCommand} from '../../base-command.js' | ||
| import {DEFAULT_MAX_MEDIA_BYTES, downloadMedia} from '../../services/media.js' | ||
| import { | ||
| DEFAULT_MAX_MEDIA_BYTES, | ||
| downloadMedia, | ||
| downloadMediaFromEvent, | ||
| signedMediaUrl, | ||
| } from '../../services/media.js' | ||
|
|
||
| export const SIGNED_URL_WARNING = | ||
| 'Warning: this signed link expires within minutes and grants access to the media without further authentication. Do not log, store, or share it.\n' | ||
|
|
||
| export default class MediaDownload extends BaseCommand { | ||
| static override args = { | ||
| media: Args.string({description: 'Messaging media ID.', required: true}), | ||
| media: Args.string({ | ||
| description: 'Messaging media ID. Omit it when using --from-event.', | ||
| required: false, | ||
| }), | ||
| } | ||
|
|
||
| static override description = 'Download Messaging media to a local file.' | ||
| static override description = | ||
| 'Download Messaging media to a local file, print its signed link, or decrypt it from a stored webhook event.' | ||
|
|
||
| static override examples = [ | ||
| '<%= config.bin %> media download media_123 --output ./attachment.bin', | ||
| '<%= config.bin %> media download media_123 --url', | ||
| '<%= config.bin %> media download --from-event ./message.json -o ./photo.jpg', | ||
| ] | ||
|
|
||
| static override flags = { | ||
| force: Flags.boolean({description: 'Overwrite an existing output file.'}), | ||
| 'from-event': Flags.string({ | ||
| description: | ||
| 'Decrypt media from the WhatsApp CDN using a stored message webhook JSON file. The file contains a decryption key.', | ||
| exclusive: ['url'], | ||
| }), | ||
| 'max-bytes': Flags.integer({ | ||
| default: DEFAULT_MAX_MEDIA_BYTES, | ||
| description: 'Maximum bytes to buffer in memory.', | ||
| description: 'Maximum bytes to download.', | ||
| min: 1, | ||
| }), | ||
| output: Flags.string({ | ||
| char: 'o', | ||
| description: 'Destination file path.', | ||
| required: true, | ||
| exclusive: ['url'], | ||
| }), | ||
| url: Flags.boolean({ | ||
| description: | ||
| 'Print the short-lived signed download link to stdout instead of downloading.', | ||
| exclusive: ['output', 'from-event', 'force'], | ||
| }), | ||
| } | ||
|
|
||
| async run(): Promise<void> { | ||
| const {args, flags} = await this.parse(MediaDownload) | ||
| const result = await downloadMedia( | ||
| await this.messagingClient(flags, {serverApiKeyOnly: 'Media'}), | ||
| args.media, | ||
| { | ||
| const fromEvent = flags['from-event'] | ||
|
|
||
| if (fromEvent !== undefined) { | ||
| if (args.media !== undefined) { | ||
| throw new PolymorfaValidationError( | ||
| 'Pass either a media ID or --from-event, not both.', | ||
| ) | ||
| } | ||
| const output = requireOutput(flags.output) | ||
| const result = await downloadMediaFromEvent(fromEvent, { | ||
| force: flags.force, | ||
| maxBytes: flags['max-bytes'], | ||
| output: flags.output, | ||
| }, | ||
| ) | ||
| output, | ||
| ...(flags.timeout === undefined ? {} : {timeoutMs: flags.timeout}), | ||
| }) | ||
| this.writeValue(result, result.data.path, flags) | ||
| return | ||
| } | ||
|
|
||
| if (args.media === undefined) { | ||
| throw new PolymorfaValidationError( | ||
| 'A media ID is required unless --from-event is passed.', | ||
| ) | ||
| } | ||
|
|
||
| const client = await this.messagingClient(flags, { | ||
| serverApiKeyOnly: 'Media', | ||
| }) | ||
|
|
||
| if (flags.url) { | ||
| const result = await signedMediaUrl(client, args.media) | ||
| process.stderr.write(SIGNED_URL_WARNING) | ||
| this.writeValue(result, result.data.url, flags) | ||
| return | ||
| } | ||
|
|
||
| const result = await downloadMedia(client, args.media, { | ||
| force: flags.force, | ||
| maxBytes: flags['max-bytes'], | ||
| output: requireOutput(flags.output), | ||
| }) | ||
| this.writeValue(result, result.data.path, flags) | ||
| } | ||
| } | ||
|
|
||
| function requireOutput(output: string | undefined): string { | ||
| if (output === undefined || output.length === 0) { | ||
| throw new PolymorfaValidationError( | ||
| 'Pass --output <path>, or --url to print a signed link.', | ||
| ) | ||
| } | ||
| return output | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.