forked from turt2live/matrix-bot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for some content scanner endpoints. (#45)
* Add support for some content scanner endpoints. * fix tests * fix empty media id being allowed
- Loading branch information
Showing
6 changed files
with
136 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { EncryptedFile, MatrixClient } from "."; | ||
import { MXCUrl } from "./models/MXCUrl"; | ||
|
||
export interface ContentScannerResult { | ||
info: string; | ||
clean: boolean; | ||
} | ||
export interface ContentScannerErrorResult { | ||
info: string; | ||
reason: string; | ||
} | ||
|
||
export class MatrixContentScannerError extends Error { | ||
constructor(public readonly body: ContentScannerErrorResult) { | ||
super(`Encountered error scanning content (${body.reason}): ${body.info}`); | ||
} | ||
} | ||
|
||
const errorHandler = (_response, errBody) => { | ||
return typeof (errBody) === "object" && 'reason' in errBody ? | ||
new MatrixContentScannerError(errBody as ContentScannerErrorResult) : undefined; | ||
}; | ||
|
||
/** | ||
* API client for https://github.com/element-hq/matrix-content-scanner-python. | ||
*/ | ||
export class MatrixContentScannerClient { | ||
constructor(public readonly client: MatrixClient) { | ||
|
||
} | ||
|
||
public async scanContent(mxcUrl: string): Promise<ContentScannerResult> { | ||
const { domain, mediaId } = MXCUrl.parse(mxcUrl); | ||
const path = `/_matrix/media_proxy/unstable/scan/${domain}/${mediaId}`; | ||
const res = await this.client.doRequest("GET", path, null, null, null, false, null, false, { errorHandler }); | ||
return res; | ||
} | ||
|
||
public async scanContentEncrypted(file: EncryptedFile): Promise<ContentScannerResult> { | ||
// Sanity check. | ||
MXCUrl.parse(file.url); | ||
const path = `/_matrix/media_proxy/unstable/scan_encrypted`; | ||
const res = await this.client.doRequest("POST", path, null, { file }, null, false, null, false, { errorHandler }); | ||
return res; | ||
} | ||
|
||
public async downloadContent(mxcUrl: string, allowRemote = true): ReturnType<MatrixClient["downloadContent"]> { | ||
const { domain, mediaId } = MXCUrl.parse(mxcUrl); | ||
const path = `/_matrix/media_proxy/unstable/download/${encodeURIComponent(domain)}/${encodeURIComponent(mediaId)}`; | ||
const res = await this.client.doRequest("GET", path, null, null, null, true, null, true, { errorHandler }); | ||
return { | ||
data: res.body, | ||
contentType: res.headers["content-type"], | ||
}; | ||
} | ||
|
||
public async downloadEncryptedContent(file: EncryptedFile): Promise<Buffer> { | ||
// Sanity check. | ||
MXCUrl.parse(file.url); | ||
const path = `/_matrix/media_proxy/unstable/download_encrypted`; | ||
const res = await this.client.doRequest("POST", path, undefined, { | ||
file, | ||
}, null, true, null, true, { errorHandler }); | ||
return res.data; | ||
} | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export class MXCUrl { | ||
static parse(mxcUrl: string): MXCUrl { | ||
if (!mxcUrl?.toLowerCase()?.startsWith("mxc://")) { | ||
throw Error("Not a MXC URI"); | ||
} | ||
const [domain, ...mediaIdParts] = mxcUrl.slice("mxc://".length).split("/"); | ||
if (!domain) { | ||
throw Error("missing domain component"); | ||
} | ||
const mediaId = mediaIdParts?.join('/') ?? undefined; | ||
if (!mediaId) { | ||
throw Error("missing mediaId component"); | ||
} | ||
return new MXCUrl(domain, mediaId); | ||
} | ||
|
||
constructor(public domain: string, public mediaId: string) { } | ||
|
||
public toString() { | ||
return `mxc://${this.domain}/${this.mediaId}`; | ||
} | ||
} |