(filePath: string, category = 'folder', HTMLFormat = true, imageAsThumbnail = true)
| 51 | * @returns {Promise<string>} the preview of the file/folder |
| 52 | */ |
| 53 | const fileThumbnail = async (filePath: string, category = 'folder', HTMLFormat = true, imageAsThumbnail = true): Promise<string> => { |
| 54 | if (!trieInitilized) { |
| 55 | for (const category of FileLib) { |
| 56 | if (category.extensions?.length && category.thumbnail) { |
| 57 | for (const extension of category.extensions) { |
| 58 | extensionThumbnailTrie.insert(extension, category.thumbnail); |
| 59 | } |
| 60 | } |
| 61 | if (category.fileNames?.length && category.thumbnail) { |
| 62 | for (const fileName of category.fileNames) { |
| 63 | filenameThumbnailTrie.insert(fileName, category.thumbnail); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | for (const category of FolderLib) { |
| 68 | if (category.folderNames?.length && category.thumbnail) { |
| 69 | for (const folderName of category.folderNames) { |
| 70 | folderThumbnailTrie.insert(folderName, category.thumbnail); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | trieInitilized = true; |
| 75 | } |
| 76 | if (category === 'file' && filePath.indexOf('.') === -1) return imageThumbnail(defaultThumbnail.DEFAULT_FILE_THUMBNAIL, HTMLFormat); |
| 77 | const ext = filePath.split('.').pop().toLowerCase(); // Get extension of filename |
| 78 | const basename = getBasename(filePath); |
| 79 | const appearance = await Storage.get('appearance'); |
| 80 | if (IMAGE_TYPES.indexOf(ext) !== -1) { |
| 81 | if (imageAsThumbnail) { |
| 82 | return imageThumbnail(filePath, HTMLFormat, true); |
| 83 | } else { |
| 84 | return imageThumbnail(DEFAULT_IMAGE_THUMBNAIL, HTMLFormat); |
| 85 | } |
| 86 | } else if (VIDEO_TYPES.indexOf(ext) !== -1) { |
| 87 | const assetSrc = new FileAPI(filePath).readAsset(); |
| 88 | return HTMLFormat ? await videoPreview(assetSrc) : assetSrc; |
| 89 | } else if ((appearance?.extractExeIcon ?? false) && (ext === 'exe' || ext === 'msi')) { |
| 90 | return imageThumbnail(await new FileAPI(filePath).extractIcon(), HTMLFormat, true); |
| 91 | } |
| 92 | |
| 93 | const filename = filePath.toLowerCase(); // Lowercase filename |
| 94 | if (category === 'contextmenu') { |
| 95 | return imageThumbnail(`contextmenu/${filePath + '.svg'}`); |
| 96 | } |
| 97 | |
| 98 | if (category === 'file') { |
| 99 | return imageThumbnail( |
| 100 | filenameThumbnailTrie.search(basename) ?? extensionThumbnailTrie.search(ext) ?? defaultThumbnail.DEFAULT_FILE_THUMBNAIL, |
| 101 | HTMLFormat |
| 102 | ); |
| 103 | } else { |
| 104 | if (category !== 'folder') { |
| 105 | const _key = `${category}-${filename}`; |
| 106 | if (Object.keys(customThumbnail).indexOf(_key) !== -1) { |
| 107 | return imageThumbnail(customThumbnail[_key], HTMLFormat); |
| 108 | } |
| 109 | } |
| 110 | return imageThumbnail(folderThumbnailTrie.search(basename) ?? defaultThumbnail.DEFAULT_FOLDER_THUMBNAIL, HTMLFormat); |
no test coverage detected