diff --git a/spec/unit/content-repo.spec.js b/spec/unit/content-repo.spec.js index db96728e6..fbb6f6784 100644 --- a/spec/unit/content-repo.spec.js +++ b/spec/unit/content-repo.spec.js @@ -1,4 +1,4 @@ -import * as ContentRepo from "../../src/content-repo"; +import {ContentRepo} from "../../src/content-repo"; describe("ContentRepo", function() { const baseUrl = "https://my.home.server"; diff --git a/src/client.js b/src/client.js index 04158dd64..b8f5bf3e1 100644 --- a/src/client.js +++ b/src/client.js @@ -36,7 +36,7 @@ import {createNewMatrixCall} from "./webrtc/call"; import * as utils from './utils'; import {sleep} from './utils'; import {MatrixError, PREFIX_MEDIA_R0, PREFIX_UNSTABLE} from "./http-api"; -import * as contentRepo from "./content-repo"; +import {ContentRepo} from "./content-repo"; import * as ContentHelpers from "./content-helpers"; import * as olmlib from "./crypto/olmlib"; import {ReEmitter} from './ReEmitter'; @@ -3132,7 +3132,7 @@ MatrixClient.prototype.setAvatarUrl = function(url, callback) { */ MatrixClient.prototype.mxcUrlToHttp = function(mxcUrl, width, height, resizeMethod, allowDirectLinks) { - return contentRepo.getHttpUriForMxc( + return ContentRepo.getHttpUriForMxc( this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks, ); }; diff --git a/src/content-repo.js b/src/content-repo.js index 4bc4ca688..7ac25d461 100644 --- a/src/content-repo.js +++ b/src/content-repo.js @@ -20,90 +20,92 @@ limitations under the License. import * as utils from "./utils"; -/** - * Get the HTTP URL for an MXC URI. - * @param {string} baseUrl The base homeserver url which has a content repo. - * @param {string} mxc The mxc:// URI. - * @param {Number} width The desired width of the thumbnail. - * @param {Number} height The desired height of the thumbnail. - * @param {string} resizeMethod The thumbnail resize method to use, either - * "crop" or "scale". - * @param {Boolean} allowDirectLinks If true, return any non-mxc URLs - * directly. Fetching such URLs will leak information about the user to - * anyone they share a room with. If false, will return the emptry string - * for such URLs. - * @return {string} The complete URL to the content. - */ -export function getHttpUriForMxc(baseUrl, mxc, width, height, - resizeMethod, allowDirectLinks) { - if (typeof mxc !== "string" || !mxc) { - return ''; - } - if (mxc.indexOf("mxc://") !== 0) { - if (allowDirectLinks) { - return mxc; - } else { +export class ContentRepo { + /** + * Get the HTTP URL for an MXC URI. + * @param {string} baseUrl The base homeserver url which has a content repo. + * @param {string} mxc The mxc:// URI. + * @param {Number} width The desired width of the thumbnail. + * @param {Number} height The desired height of the thumbnail. + * @param {string} resizeMethod The thumbnail resize method to use, either + * "crop" or "scale". + * @param {Boolean} allowDirectLinks If true, return any non-mxc URLs + * directly. Fetching such URLs will leak information about the user to + * anyone they share a room with. If false, will return the emptry string + * for such URLs. + * @return {string} The complete URL to the content. + */ + static getHttpUriForMxc(baseUrl, mxc, width, height, + resizeMethod, allowDirectLinks) { + if (typeof mxc !== "string" || !mxc) { return ''; } - } - let serverAndMediaId = mxc.slice(6); // strips mxc:// - let prefix = "/_matrix/media/r0/download/"; - const params = {}; + if (mxc.indexOf("mxc://") !== 0) { + if (allowDirectLinks) { + return mxc; + } else { + return ''; + } + } + let serverAndMediaId = mxc.slice(6); // strips mxc:// + let prefix = "/_matrix/media/r0/download/"; + const params = {}; - if (width) { - params.width = Math.round(width); - } - if (height) { - params.height = Math.round(height); - } - if (resizeMethod) { - params.method = resizeMethod; - } - if (utils.keys(params).length > 0) { - // these are thumbnailing params so they probably want the - // thumbnailing API... - prefix = "/_matrix/media/r0/thumbnail/"; + if (width) { + params.width = Math.round(width); + } + if (height) { + params.height = Math.round(height); + } + if (resizeMethod) { + params.method = resizeMethod; + } + if (utils.keys(params).length > 0) { + // these are thumbnailing params so they probably want the + // thumbnailing API... + prefix = "/_matrix/media/r0/thumbnail/"; + } + + const fragmentOffset = serverAndMediaId.indexOf("#"); + let fragment = ""; + if (fragmentOffset >= 0) { + fragment = serverAndMediaId.substr(fragmentOffset); + serverAndMediaId = serverAndMediaId.substr(0, fragmentOffset); + } + return baseUrl + prefix + serverAndMediaId + + (utils.keys(params).length === 0 ? "" : + ("?" + utils.encodeParams(params))) + fragment; } - const fragmentOffset = serverAndMediaId.indexOf("#"); - let fragment = ""; - if (fragmentOffset >= 0) { - fragment = serverAndMediaId.substr(fragmentOffset); - serverAndMediaId = serverAndMediaId.substr(0, fragmentOffset); + /** + * Get an identicon URL from an arbitrary string. + * @param {string} baseUrl The base homeserver url which has a content repo. + * @param {string} identiconString The string to create an identicon for. + * @param {Number} width The desired width of the image in pixels. Default: 96. + * @param {Number} height The desired height of the image in pixels. Default: 96. + * @return {string} The complete URL to the identicon. + * @deprecated This is no longer in the specification. + */ + static getIdenticonUri(baseUrl, identiconString, width, height) { + if (!identiconString) { + return null; + } + if (!width) { + width = 96; + } + if (!height) { + height = 96; + } + const params = { + width: width, + height: height, + }; + + const path = utils.encodeUri("/_matrix/media/unstable/identicon/$ident", { + $ident: identiconString, + }); + return baseUrl + path + + (utils.keys(params).length === 0 ? "" : + ("?" + utils.encodeParams(params))); } - return baseUrl + prefix + serverAndMediaId + - (utils.keys(params).length === 0 ? "" : - ("?" + utils.encodeParams(params))) + fragment; -} - -/** - * Get an identicon URL from an arbitrary string. - * @param {string} baseUrl The base homeserver url which has a content repo. - * @param {string} identiconString The string to create an identicon for. - * @param {Number} width The desired width of the image in pixels. Default: 96. - * @param {Number} height The desired height of the image in pixels. Default: 96. - * @return {string} The complete URL to the identicon. - * @deprecated This is no longer in the specification. - */ -export function getIdenticonUri(baseUrl, identiconString, width, height) { - if (!identiconString) { - return null; - } - if (!width) { - width = 96; - } - if (!height) { - height = 96; - } - const params = { - width: width, - height: height, - }; - - const path = utils.encodeUri("/_matrix/media/unstable/identicon/$ident", { - $ident: identiconString, - }); - return baseUrl + path + - (utils.keys(params).length === 0 ? "" : - ("?" + utils.encodeParams(params))); } diff --git a/src/matrix.js b/src/matrix.js index f611d1d7c..32892d650 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -44,8 +44,8 @@ export * from "./store/indexeddb"; export * from "./store/session/webstorage"; export * from "./crypto/store/memory-crypto-store"; export * from "./crypto/store/indexeddb-crypto-store"; +export * from "./content-repo"; export const ContentHelpers = import("./content-helpers"); -export const ContentRepo = import("./content-repo"); export { createNewMatrixCall, setAudioOutput as setMatrixCallAudioOutput, diff --git a/src/models/room-member.js b/src/models/room-member.js index fb53f5614..db3b85946 100644 --- a/src/models/room-member.js +++ b/src/models/room-member.js @@ -20,7 +20,7 @@ limitations under the License. */ import {EventEmitter} from "events"; -import * as ContentRepo from "../content-repo"; +import {ContentRepo} from "../content-repo"; import * as utils from "../utils"; /** diff --git a/src/models/room.js b/src/models/room.js index fae36d2ca..77a6f370e 100644 --- a/src/models/room.js +++ b/src/models/room.js @@ -23,7 +23,7 @@ limitations under the License. import {EventEmitter} from "events"; import {EventTimelineSet} from "./event-timeline-set"; import {EventTimeline} from "./event-timeline"; -import * as ContentRepo from "../content-repo"; +import {ContentRepo} from "../content-repo"; import * as utils from "../utils"; import {EventStatus, MatrixEvent} from "./event"; import {RoomMember} from "./room-member";