Skip to content
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

Feature request: Send remote attachment #150

Open
humanagent opened this issue Dec 8, 2024 · 0 comments
Open

Feature request: Send remote attachment #150

humanagent opened this issue Dec 8, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@humanagent
Copy link
Collaborator

humanagent commented Dec 8, 2024

https://docs.xmtp.org/inboxes/content-types/attachments
Can make this work given an existing URL


  async sendRemoteAttachment(url: string) {
    try {
      // Fetch the image data
      const response = await fetch(url);
      if (!response.ok) {
        throw new Error(`Failed to fetch the image: ${response.statusText}`);
      }
      const arrayBuffer = await response.arrayBuffer();

      // Create the attachment object
      const attachment = {
        filename: url.split("/").pop() || "image.jpg",
        mimeType: response.headers.get("content-type") || "image/jpeg",
        data: new Uint8Array(arrayBuffer),
      };

      // Encode and encrypt the attachment
      const encryptedEncoded = await RemoteAttachmentCodec.encodeEncrypted(
        attachment,
        new AttachmentCodec(),
      );

      // Create the remote attachment object
      const remoteAttachment: RemoteAttachment = {
        url: url, // Original URL
        contentDigest: encryptedEncoded.digest,
        salt: encryptedEncoded.salt,
        nonce: encryptedEncoded.nonce,
        secret: encryptedEncoded.secret,
        scheme: "https://",
        contentLength: attachment.data.byteLength,
        filename: attachment.filename,
      };

      // Send the remote attachment
      await this.send(remoteAttachment, ContentTypeRemoteAttachment);

      return remoteAttachment;
    } catch (error) {
      console.error("Failed to send remote attachment:", error);
      throw error;
    }
  }

@humanagent humanagent added the enhancement New feature or request label Dec 8, 2024
@humanagent humanagent reopened this Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant