You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.any.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { IReduxState, IStore } from '../app/types';
  2. import {
  3. IconImage,
  4. IconShareDoc,
  5. IconVideo,
  6. IconVolumeUp
  7. } from '../base/icons/svg';
  8. import { getLocalizedDateFormatter } from '../base/i18n/dateUtil';
  9. import { showErrorNotification } from '../notifications/actions';
  10. import { NOTIFICATION_TIMEOUT_TYPE, NOTIFICATION_TYPE } from '../notifications/constants';
  11. import { uploadFiles } from './actions';
  12. import { MAX_FILE_SIZE } from './constants';
  13. /**
  14. * Checks whether file sharing feature is enabled.
  15. *
  16. * @param {IReduxState} state - The redux state.
  17. * @returns {boolean} - Indicates if file sharing feature is enabled.
  18. */
  19. export function isFileSharingEnabled(state: IReduxState) {
  20. const { fileSharing } = state['features/base/config'] ?? {};
  21. return Boolean(fileSharing?.enabled && fileSharing?.apiUrl);
  22. }
  23. /**
  24. * Gets the file extension from a file name.
  25. *
  26. * @param {string} fileName - The name of the file to extract the extension from.
  27. * @returns {string} The file extension or an empty string if none exists.
  28. */
  29. export function getFileExtension(fileName: string): string {
  30. const parts = fileName.split('.');
  31. if (parts.length > 1) {
  32. return parts.pop()?.toLowerCase() || '';
  33. }
  34. return '';
  35. }
  36. /**
  37. * Gets the appropriate icon for a file based on its type.
  38. *
  39. * @param {string} fileType - The file type.
  40. * @returns {Function} The icon component to use.
  41. */
  42. export function getFileIcon(fileType: string) {
  43. if ([ 'mkv', 'mp4', 'mov', 'avi', 'webm' ].includes(fileType)) {
  44. return IconVideo;
  45. }
  46. if ([ 'mp3', 'wav', 'ogg' ].includes(fileType)) {
  47. return IconVolumeUp;
  48. }
  49. if ([ 'jpg', 'jpeg', 'png', 'gif' ].includes(fileType)) {
  50. return IconImage;
  51. }
  52. return IconShareDoc;
  53. }
  54. /**
  55. * Formats the file size into a human-readable string.
  56. *
  57. * @param {number} bytes - The size in bytes.
  58. * @returns {string} The formatted file size string.
  59. */
  60. export function formatFileSize(bytes: number): string {
  61. if (bytes <= 0) {
  62. return '0 Bytes';
  63. }
  64. const sizes = [ 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB' ];
  65. const i = Math.floor(Math.log(bytes) / Math.log(1024));
  66. const size = bytes / Math.pow(1024, i);
  67. // Check if size is an integer after rounding to 2 decimals
  68. const rounded = Math.round(size * 100) / 100;
  69. const formattedSize = Number.isInteger(rounded) ? rounded : rounded.toFixed(2);
  70. return `${formattedSize} ${sizes[i]}`;
  71. }
  72. /**
  73. * Formats the timestamp into a human-readable string.
  74. *
  75. * @param {number} timestamp - The timestamp to format.
  76. * @returns {string} The formatted timestamp string.
  77. */
  78. export function formatTimestamp(timestamp: number): string {
  79. const date = getLocalizedDateFormatter(timestamp);
  80. const monthDay = date.format('MMM D'); // Eg. "May 15"
  81. const time = date.format('h:mm A'); // Eg. "2:30 PM"
  82. return `${monthDay}\n${time}`;
  83. }
  84. /**
  85. * Processes a list of files for upload.
  86. *
  87. * @param {FileList|File[]} fileList - The list of files to process.
  88. * @param {Dispatch} dispatch - The Redux dispatch function.
  89. * @returns {void}
  90. */
  91. // @ts-ignore
  92. export const processFiles = (fileList: FileList | File[], store: IStore) => {
  93. const state = store.getState();
  94. const dispatch = store.dispatch;
  95. const { maxFileSize = MAX_FILE_SIZE } = state['features/base/config']?.fileSharing ?? {};
  96. const newFiles = Array.from(fileList as File[]).filter((file: File) => {
  97. // No file size limitation
  98. if (maxFileSize === -1) {
  99. return true;
  100. }
  101. // Check file size before upload
  102. if (file.size > maxFileSize) {
  103. dispatch(showErrorNotification({
  104. titleKey: 'fileSharing.fileTooLargeTitle',
  105. descriptionKey: 'fileSharing.fileTooLargeDescription',
  106. descriptionArguments: {
  107. maxFileSize: formatFileSize(maxFileSize)
  108. },
  109. appearance: NOTIFICATION_TYPE.ERROR
  110. }, NOTIFICATION_TIMEOUT_TYPE.STICKY));
  111. return false;
  112. }
  113. return true;
  114. });
  115. if (newFiles.length > 0) {
  116. dispatch(uploadFiles(newFiles as File[]));
  117. }
  118. };