matchingFeilType.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. function fileList(list) {
  2. if (!Array.isArray(list) || list.length === 0) return [];
  3. const suffixList = {
  4. image: ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'svg', 'tiff'],
  5. video: ['mp4', 'ogg', 'webm'],
  6. word: ['doc', 'docx'],
  7. excel: ['xls', 'xlsx'],
  8. ppt: ['ppt', 'pptx'],
  9. txt: ['txt', 'md', 'js', 'json'],
  10. pdf: ['pdf'],
  11. rar: ['7z', 'zip', 'rar', 'kz', 'ace', 'arj', 'bz2', 'cab', 'gz', 'iso', 'jar', 'lzh', 'tar', 'z'],
  12. folder: ['folder']
  13. };
  14. return list.map(file => {
  15. const suffix = file.postfix ? file.postfix.toLowerCase() : '';
  16. const fileType = suffixList.folder.includes(suffix) ? 'folder' :
  17. Object.keys(suffixList).find(key => suffixList[key].includes(suffix)) || 'unknown';
  18. return {
  19. ...file,
  20. fileType,
  21. cover: fileType === 'image' ? file.url :
  22. fileType !== 'unknown' ? `/static/image/file/${fileType}.png` :
  23. `/static/image/file/unknown.png`
  24. };
  25. });
  26. }
  27. //得到缩略图或者压缩图 getType默认得到缩略图传true得到压缩图
  28. function getSpecifiedImage(obj, getType = false) {
  29. if (!obj || !Array.isArray(obj.subfiles)) return "";
  30. const type = getType ? 'compressed' : 'thumbnail';
  31. const imgObj = obj.subfiles.find(v => v.type === type);
  32. return imgObj ? imgObj.url : "";
  33. }
  34. module.exports = {
  35. fileList,
  36. getSpecifiedImage
  37. }