UNPKG

3.89 kBJavaScriptView Raw
1/*
2Copyright 2015, 2016 OpenMarket Ltd
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16/**
17 * @module content-repo
18 */
19const utils = require("./utils");
20
21/** Content Repo utility functions */
22module.exports = {
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 */
37 getHttpUriForMxc: function(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/v1/download/";
51 const params = {};
52
53 if (width) {
54 params.width = width;
55 }
56 if (height) {
57 params.height = 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/v1/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 */
87 getIdenticonUri: function(baseUrl, identiconString, width, height) {
88 if (!identiconString) {
89 return null;
90 }
91 if (!width) {
92 width = 96;
93 }
94 if (!height) {
95 height = 96;
96 }
97 const params = {
98 width: width,
99 height: height,
100 };
101
102 const path = utils.encodeUri("/_matrix/media/v1/identicon/$ident", {
103 $ident: identiconString,
104 });
105 return baseUrl + path +
106 (utils.keys(params).length === 0 ? "" :
107 ("?" + utils.encodeParams(params)));
108 },
109};