2021-07-19 16:02:33 +00:00
|
|
|
/*
|
2021-07-19 16:11:23 +00:00
|
|
|
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
|
2021-07-19 16:02:33 +00:00
|
|
|
Copyright 2021 Šimon Brandner <simon.bra.ag@gmail.com>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import filesize from 'filesize';
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2021-07-19 16:02:33 +00:00
|
|
|
import { IMediaEventContent } from '../customisations/models/IMediaEventContent';
|
|
|
|
import { _t } from '../languageHandler';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts a human readable label for the file attachment to use as
|
|
|
|
* link text.
|
|
|
|
*
|
|
|
|
* @param {IMediaEventContent} content The "content" key of the matrix event.
|
|
|
|
* @param {string} fallbackText The fallback text
|
|
|
|
* @param {boolean} withSize Whether to include size information. Default true.
|
2021-07-29 21:49:23 +00:00
|
|
|
* @param {boolean} shortened Ensure the extension of the file name is visible. Default false.
|
2021-07-19 16:02:33 +00:00
|
|
|
* @return {string} the human readable link text for the attachment.
|
|
|
|
*/
|
|
|
|
export function presentableTextForFile(
|
|
|
|
content: IMediaEventContent,
|
|
|
|
fallbackText = _t("Attachment"),
|
|
|
|
withSize = true,
|
2021-07-29 21:49:23 +00:00
|
|
|
shortened = false,
|
2021-07-19 16:02:33 +00:00
|
|
|
): string {
|
|
|
|
let text = fallbackText;
|
2022-05-14 08:31:53 +00:00
|
|
|
if (content.body?.length > 0) {
|
2021-07-19 16:02:33 +00:00
|
|
|
// The content body should be the name of the file including a
|
|
|
|
// file extension.
|
|
|
|
text = content.body;
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:49:23 +00:00
|
|
|
// We shorten to 15 characters somewhat arbitrarily, and assume most files
|
|
|
|
// will have a 3 character (plus full stop) extension. The goal is to knock
|
|
|
|
// the label down to 15-25 characters, not perfect accuracy.
|
|
|
|
if (shortened && text.length > 19) {
|
|
|
|
const parts = text.split('.');
|
|
|
|
let fileName = parts.slice(0, parts.length - 1).join('.').substring(0, 15);
|
|
|
|
const extension = parts[parts.length - 1];
|
|
|
|
|
|
|
|
// Trim off any full stops from the file name to avoid a case where we
|
|
|
|
// add an ellipsis that looks really funky.
|
|
|
|
fileName = fileName.replace(/\.*$/g, '');
|
|
|
|
|
|
|
|
text = `${fileName}...${extension}`;
|
|
|
|
}
|
|
|
|
|
2022-05-14 08:31:53 +00:00
|
|
|
if (content.info?.size && withSize) {
|
2021-07-19 16:02:33 +00:00
|
|
|
// If we know the size of the file then add it as human readable
|
|
|
|
// string to the end of the link text so that the user knows how
|
|
|
|
// big a file they are downloading.
|
|
|
|
// The content.info also contains a MIME-type but we don't display
|
|
|
|
// it since it is "ugly", users generally aren't aware what it
|
2022-05-09 22:52:05 +00:00
|
|
|
// means and the type of the attachment can usually be inferred
|
2021-07-19 16:02:33 +00:00
|
|
|
// from the file extension.
|
|
|
|
text += ' (' + filesize(content.info.size) + ')';
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|