|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// @ts-check |
| 4 | + |
| 5 | +import fs from "node:fs/promises"; |
| 6 | +import path from "node:path"; |
| 7 | +import process from "node:process"; |
| 8 | + |
| 9 | +import { defineCommand, runMain } from "citty"; |
| 10 | +import { PangeaConfig, Share, ShareService } from "pangea-node-sdk"; |
| 11 | + |
| 12 | +const main = defineCommand({ |
| 13 | + meta: { |
| 14 | + name: "share", |
| 15 | + version: "0.0.0", |
| 16 | + description: |
| 17 | + "An example command line utility that creates an email+code, SMS+code, " + |
| 18 | + "or password-secured download/upload/editor share-link for a given " + |
| 19 | + "file or for each file in a given directory.", |
| 20 | + }, |
| 21 | + args: { |
| 22 | + input: { |
| 23 | + type: "string", |
| 24 | + required: true, |
| 25 | + alias: "i", |
| 26 | + description: "Local path to upload.", |
| 27 | + }, |
| 28 | + dest: { |
| 29 | + type: "string", |
| 30 | + default: "/", |
| 31 | + description: "Destination path in Share.", |
| 32 | + }, |
| 33 | + email: { |
| 34 | + type: "string", |
| 35 | + description: "Email address to protect the share link with.", |
| 36 | + }, |
| 37 | + phone: { |
| 38 | + type: "string", |
| 39 | + description: "Phone number to protect the share link with.", |
| 40 | + }, |
| 41 | + password: { |
| 42 | + type: "string", |
| 43 | + description: "Password to protect the share link with.", |
| 44 | + }, |
| 45 | + link_type: { |
| 46 | + type: "string", |
| 47 | + default: Share.LinkType.DOWNLOAD, |
| 48 | + description: "Type of link.", |
| 49 | + }, |
| 50 | + }, |
| 51 | + async run({ args }) { |
| 52 | + if (!args.email && !args.phone && !args.password) { |
| 53 | + throw new Error( |
| 54 | + "At least one of --email, --phone, or --password must be provided." |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** @type {Set<Share.Authenticator>} */ |
| 59 | + const authenticators = new Set(); |
| 60 | + if (args.password) { |
| 61 | + authenticators.add({ |
| 62 | + auth_type: Share.AuthenticatorType.PASSWORD, |
| 63 | + auth_context: args.password, |
| 64 | + }); |
| 65 | + } |
| 66 | + if (args.email) { |
| 67 | + authenticators.add({ |
| 68 | + auth_type: Share.AuthenticatorType.EMAIL_OTP, |
| 69 | + auth_context: args.email, |
| 70 | + }); |
| 71 | + } |
| 72 | + if (args.phone) { |
| 73 | + authenticators.add({ |
| 74 | + auth_type: Share.AuthenticatorType.SMS_OTP, |
| 75 | + auth_context: args.phone, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + const share = new ShareService( |
| 80 | + // @ts-expect-error |
| 81 | + process.env.PANGEA_SHARE_TOKEN, |
| 82 | + new PangeaConfig({ domain: process.env.PANGEA_DOMAIN }) |
| 83 | + ); |
| 84 | + |
| 85 | + // Upload files. |
| 86 | + const files = (await fs.lstat(args.input)).isDirectory() |
| 87 | + ? (await fs.readdir(args.input)).map((x) => path.resolve(args.input, x)) |
| 88 | + : [args.input]; |
| 89 | + const objectIds = new Set(); |
| 90 | + for (const file of files) { |
| 91 | + const uploadResponse = await share.put( |
| 92 | + { |
| 93 | + path: `${args.dest}/${path.basename(file)}`, |
| 94 | + }, |
| 95 | + { file, name: "file" } |
| 96 | + ); |
| 97 | + objectIds.add(uploadResponse.result.object.id); |
| 98 | + } |
| 99 | + |
| 100 | + // Create share link. |
| 101 | + const linkResponse = await share.shareLinkCreate({ |
| 102 | + links: [ |
| 103 | + { |
| 104 | + targets: Array.from(objectIds), |
| 105 | + // @ts-expect-error |
| 106 | + link_type: args.link_type, |
| 107 | + authenticators: Array.from(authenticators), |
| 108 | + }, |
| 109 | + ], |
| 110 | + }); |
| 111 | + console.log(linkResponse.result.share_link_objects[0].link); |
| 112 | + }, |
| 113 | +}); |
| 114 | + |
| 115 | +runMain(main); |
0 commit comments