Commit 4e699046 authored by Grant's avatar Grant
Browse files

implement authorized media fetch (fixes #14)

parent 52146346
Loading
Loading
Loading
Loading
Loading
+2 −6
Original line number Diff line number Diff line
@@ -1232,7 +1232,7 @@ class Discord_ {
      /**
       * IMPORTANT: attachment URLs are fetched and streamed to discord
       */
      attachment?: { url: string; name: string };
      attachment?: { url: MXC; name: string };
    } = {}
  ) {
    const webhook = await this.getWebhookFor(guild_id, channel_id);
@@ -1301,11 +1301,7 @@ class Discord_ {
      ).id;

      try {
        const request = await fetch(options.attachment.url);
        if (request.status > 399)
          throw new Error("Unknown status: " + request.status);

        const stream = await request.arrayBuffer();
        const stream = await Matrix.loadMediaURL(options.attachment.url);

        files.push({
          attachment: Buffer.from(stream),
+29 −2
Original line number Diff line number Diff line
@@ -77,6 +77,23 @@ export class Matrix {
    }
  }

  private static async rawFetch(
    endpoint: `/_matrix${string}`,
    request?: Partial<RequestInit>
  ) {
    console.log("[->Matrix] " + (request?.method || "GET") + " " + endpoint);

    const { headers, ...other } = request || {};

    return fetch(`${this.getHomeserver("internal")}${endpoint}`, {
      ...other,
      headers: {
        ...headers,
        Authorization: "Bearer " + Secrets.get("MATRIX_AS_TOKEN"),
      },
    });
  }

  private static async fetch<T = unknown>(
    endpoint: `/_matrix${string}`,
    method = "GET",
@@ -87,11 +104,10 @@ export class Matrix {
  > {
    console.log("[->Matrix] " + method + " " + endpoint);

    const req = await fetch(`${this.getHomeserver("internal")}${endpoint}`, {
    const req = await this.rawFetch(endpoint, {
      method,
      headers: {
        "Content-Type": "application/json",
        Authorization: "Bearer " + Secrets.get("MATRIX_AS_TOKEN"),
      },
      body: JSON.stringify(data),
    });
@@ -490,6 +506,17 @@ export class Matrix {
    );
  }

  static async loadMediaURL(mxc: MXC) {
    const request = await Matrix.rawFetch(
      `/_matrix/client/v1/media/download/${mxc.replace("mxc://", "")}`
    );

    if (request.status > 399)
      throw new Error("Unknown status: " + request.status);

    return request.arrayBuffer();
  }

  static getMatrixUserRegex() {
    const localpart_regex = /[a-z0-9\._=\-\/\+]{1,100}/;

+2 −5
Original line number Diff line number Diff line
@@ -231,7 +231,7 @@ class MatrixHandler_ {
          "",
          {
            attachment: {
              url: Matrix.getMediaURL(event.content.url, "internal") as string,
              url: event.content.url as any,
              name:
                "sticker." +
                (event.content.info.mimetype?.split("/").at(-1) || "png"),
@@ -455,10 +455,7 @@ class MatrixHandler_ {
              {
                replyTo: inReplyTo,
                attachment: {
                  url: Matrix.getMediaURL(
                    message.content.url as any,
                    "internal"
                  ) as string,
                  url: message.content.url as any,
                  name: message.content.body,
                },
              }