UNPKG

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