Skip to content

Commit

Permalink
Fix media upload being on the wrong endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Sep 19, 2024
1 parent dc8df60 commit e31e65b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/MatrixClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1595,9 +1595,9 @@ export class MatrixClient extends EventEmitter {

private async getMediaEndpointPrefix() {
if (await this.doesServerSupportVersion('v1.11')) {
return `${this.homeserverUrl}/_matrix/client/v1/media`;
return `/_matrix/client/v1/media`;
}
return `${this.homeserverUrl}/_matrix/media/v3`;
return `/_matrix/media/v3`;
}

/**
Expand All @@ -1608,7 +1608,7 @@ export class MatrixClient extends EventEmitter {
public async mxcToHttp(mxc: string): Promise<string> {
const { domain, mediaId } = MXCUrl.parse(mxc);
const endpoint = await this.getMediaEndpointPrefix();
return `${endpoint}/download/${encodeURIComponent(domain)}/${encodeURIComponent(mediaId)}`;
return `${this.homeserverUrl}${endpoint}/download/${encodeURIComponent(domain)}/${encodeURIComponent(mediaId)}`;
}

/**
Expand Down Expand Up @@ -1636,8 +1636,7 @@ export class MatrixClient extends EventEmitter {
@timedMatrixClientFunctionCall()
public async uploadContent(data: Buffer, contentType = "application/octet-stream", filename: string = null): Promise<string> {
// TODO: Make doRequest take an object for options
const endpoint = await this.getMediaEndpointPrefix();
return this.doRequest("POST", `${endpoint}/upload`, { filename: filename }, data, 60000, false, contentType)
return this.doRequest("POST", "/_matrix/media/v3/upload", { filename: filename }, data, 60000, false, contentType)
.then(response => response["content_uri"]);
}

Expand Down
6 changes: 3 additions & 3 deletions test/MatrixClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5717,7 +5717,7 @@ describe('MatrixClient', () => {
Buffer.isBuffer = <any>(i => i === data);

// noinspection TypeScriptValidateJSTypes
http.when("POST", "/_matrix/client/v1/media/upload").respond(200, (path, content, req) => {
http.when("POST", "/_matrix/media/v3/upload").respond(200, (path, content, req) => {
expect(content).toBeDefined();
expect(req.queryParams.filename).toEqual(filename);
expect(req.headers["Content-Type"]).toEqual(contentType);
Expand All @@ -5740,7 +5740,7 @@ describe('MatrixClient', () => {
Buffer.isBuffer = <any>(i => i === data);

// noinspection TypeScriptValidateJSTypes
http.when("POST", "/_matrix/client/v1/media/upload").respond(200, (path, content, req) => {
http.when("POST", "/_matrix/media/v3/upload").respond(200, (path, content, req) => {
expect(content).toBeDefined();
expect(req.queryParams.filename).toEqual(filename);
expect(req.headers["Content-Type"]).toEqual(contentType);
Expand Down Expand Up @@ -5798,7 +5798,7 @@ describe('MatrixClient', () => {
});

// noinspection TypeScriptValidateJSTypes
http.when("POST", "/_matrix/client/v1/media/upload").respond(200, (path, content, req) => {
http.when("POST", "/_matrix/media/v3/upload").respond(200, (path, content, req) => {
expect(content).toBeDefined();
// HACK: We know the mock library will return JSON
expect(req.headers["Content-Type"]).toEqual("application/json");
Expand Down

0 comments on commit e31e65b

Please sign in to comment.