UNPKG

mattermost-redux

Version:

Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client

90 lines (89 loc) 3.04 kB
"use strict"; // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.getFormattedFileSize = getFormattedFileSize; exports.getFileType = getFileType; exports.getFileUrl = getFileUrl; exports.getFileDownloadUrl = getFileDownloadUrl; exports.getFileThumbnailUrl = getFileThumbnailUrl; exports.getFilePreviewUrl = getFilePreviewUrl; exports.getFileMiniPreviewUrl = getFileMiniPreviewUrl; exports.sortFileInfos = sortFileInfos; const helpers_1 = require("@mattermost/client/lib/helpers"); const client_1 = require("mattermost-redux/client"); const constants_1 = require("../constants"); function getFormattedFileSize(bytes) { const fileSizes = [ ['TB', 1024 * 1024 * 1024 * 1024], ['GB', 1024 * 1024 * 1024], ['MB', 1024 * 1024], ['KB', 1024], ]; const size = fileSizes.find((unitAndMinBytes) => { const minBytes = unitAndMinBytes[1]; return bytes > minBytes; }); if (size) { return `${Math.floor(bytes / size[1])} ${size[0]}`; } return `${bytes} B`; } function getFileType(file) { if (!file || !file.extension) { return 'other'; } const fileExt = file.extension.toLowerCase(); const fileTypes = [ 'image', 'code', 'pdf', 'video', 'audio', 'spreadsheet', 'text', 'word', 'presentation', 'patch', ]; return fileTypes.find((fileType) => { const constForFileTypeExtList = `${fileType}_types`.toUpperCase(); const fileTypeExts = constants_1.Files[constForFileTypeExtList]; return fileTypeExts.indexOf(fileExt) > -1; }) || 'other'; } function getFileUrl(fileId) { return client_1.Client4.getFileRoute(fileId); } function getFileDownloadUrl(fileId, asContentReviewer, flaggedPostId) { const queryParamsArgs = {}; queryParamsArgs.download = 1; if (asContentReviewer) { queryParamsArgs.as_content_reviewer = true; } if (flaggedPostId) { queryParamsArgs.flagged_post_id = flaggedPostId; } const queryParams = (0, helpers_1.buildQueryString)(queryParamsArgs); return `${client_1.Client4.getFileRoute(fileId)}${queryParams}`; } function getFileThumbnailUrl(fileId) { return `${client_1.Client4.getFileRoute(fileId)}/thumbnail`; } function getFilePreviewUrl(fileId) { return `${client_1.Client4.getFileRoute(fileId)}/preview`; } function getFileMiniPreviewUrl(fileInfo) { if (!fileInfo?.mini_preview || !fileInfo?.mime_type) { return undefined; } return `data:${fileInfo.mime_type};base64,${fileInfo.mini_preview}`; } function sortFileInfos(fileInfos = [], locale = constants_1.General.DEFAULT_LOCALE) { return fileInfos.sort((a, b) => { if (a.create_at !== b.create_at) { return a.create_at - b.create_at; } return a.name.localeCompare(b.name, locale, { numeric: true }); }); }