UNPKG

3.65 kBJavaScriptView Raw
1/*
2Copyright 2015, 2016 OpenMarket Ltd
3Copyright 2019 The Matrix.org Foundation C.I.C.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16*/
17/**
18 * @module content-repo
19 */
20
21import * as utils from "./utils";
22
23/**
24 * Get the HTTP URL for an MXC URI.
25 * @param {string} baseUrl The base homeserver url which has a content repo.
26 * @param {string} mxc The mxc:// URI.
27 * @param {Number} width The desired width of the thumbnail.
28 * @param {Number} height The desired height of the thumbnail.
29 * @param {string} resizeMethod The thumbnail resize method to use, either
30 * "crop" or "scale".
31 * @param {Boolean} allowDirectLinks If true, return any non-mxc URLs
32 * directly. Fetching such URLs will leak information about the user to
33 * anyone they share a room with. If false, will return the emptry string
34 * for such URLs.
35 * @return {string} The complete URL to the content.
36 */
37export function getHttpUriForMxc(baseUrl, mxc, width, height,
38 resizeMethod, allowDirectLinks) {
39 if (typeof mxc !== "string" || !mxc) {
40 return '';
41 }
42 if (mxc.indexOf("mxc://") !== 0) {
43 if (allowDirectLinks) {
44 return mxc;
45 } else {
46 return '';
47 }
48 }
49 let serverAndMediaId = mxc.slice(6); // strips mxc://
50 let prefix = "/_matrix/media/r0/download/";
51 const params = {};
52
53 if (width) {
54 params.width = Math.round(width);
55 }
56 if (height) {
57 params.height = Math.round(height);
58 }
59 if (resizeMethod) {
60 params.method = resizeMethod;
61 }
62 if (utils.keys(params).length > 0) {
63 // these are thumbnailing params so they probably want the
64 // thumbnailing API...
65 prefix = "/_matrix/media/r0/thumbnail/";
66 }
67
68 const fragmentOffset = serverAndMediaId.indexOf("#");
69 let fragment = "";
70 if (fragmentOffset >= 0) {
71 fragment = serverAndMediaId.substr(fragmentOffset);
72 serverAndMediaId = serverAndMediaId.substr(0, fragmentOffset);
73 }
74 return baseUrl + prefix + serverAndMediaId +
75 (utils.keys(params).length === 0 ? "" :
76 ("?" + utils.encodeParams(params))) + fragment;
77}
78
79/**
80 * Get an identicon URL from an arbitrary string.
81 * @param {string} baseUrl The base homeserver url which has a content repo.
82 * @param {string} identiconString The string to create an identicon for.
83 * @param {Number} width The desired width of the image in pixels. Default: 96.
84 * @param {Number} height The desired height of the image in pixels. Default: 96.
85 * @return {string} The complete URL to the identicon.
86 * @deprecated This is no longer in the specification.
87 */
88export function getIdenticonUri(baseUrl, identiconString, width, height) {
89 if (!identiconString) {
90 return null;
91 }
92 if (!width) {
93 width = 96;
94 }
95 if (!height) {
96 height = 96;
97 }
98 const params = {
99 width: width,
100 height: height,
101 };
102
103 const path = utils.encodeUri("/_matrix/media/unstable/identicon/$ident", {
104 $ident: identiconString,
105 });
106 return baseUrl + path +
107 (utils.keys(params).length === 0 ? "" :
108 ("?" + utils.encodeParams(params)));
109}