LOCKING盒子版
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.
 
 
 
 

54 lines
1.3 KiB

  1. import React from 'react';
  2. import { Image, ImageProps } from 'antd';
  3. import classNames from 'classnames';
  4. import { memo } from 'react';
  5. import styles from './fileIcon.less';
  6. import { memoize } from 'lodash';
  7. interface FileIconProps extends ImageProps {
  8. extension: string;
  9. absolutePath?: string;
  10. }
  11. export default memo(function FileIcon(props: FileIconProps) {
  12. const { className, extension, absolutePath, ...restProps } = props;
  13. const isImg = isImgFile(extension) && !!absolutePath;
  14. return (
  15. <div className={classNames(styles.fileIcon, className)}>
  16. <Image
  17. preview={false}
  18. {...restProps}
  19. src={isImg ? absolutePath : fileIconSrc(extension)}
  20. fallback={fileIconSrc('default')}
  21. />
  22. </div>
  23. );
  24. });
  25. const fileIconSrc = memoize((extension: string) => {
  26. const fixedExtension = matchFileImage(extension);
  27. return `assets/file_icon/${fixedExtension}.svg`;
  28. });
  29. const imgList: string[] = ['jpg', 'png', 'bmp', 'gif', 'jpeg'];
  30. function isImgFile(extension: string) {
  31. return imgList.includes(extension);
  32. }
  33. export function matchFileImage(extension: string) {
  34. switch (extension) {
  35. case 'ppt':
  36. case 'pptx':
  37. return 'ppt';
  38. case 'xls':
  39. case 'xlsx':
  40. return 'excel';
  41. case 'doc':
  42. case 'docx':
  43. return 'word';
  44. default:
  45. return extension;
  46. }
  47. }