11import React , { useEffect , useState } from "react"
22
33import { Button , CircularProgress , Link , Tooltip } from "@heroui/react"
4- import binaryExtensions from "binary-extensions "
4+ import chardet from "chardet "
55
66import { useErrorModal } from "../components/ErrorModal.js"
77import { DarkModeToggle , useDarkModeSelection } from "../components/DarkModeToggle.js"
@@ -18,10 +18,6 @@ import "../style.css"
1818import "../styles/highlight-theme-light.css"
1919import "../styles/highlight-theme-dark.css"
2020
21- function isBinaryPath ( path : string ) {
22- return binaryExtensions . includes ( path . replace ( / .* \. / , "" ) )
23- }
24-
2521export function DisplayPaste ( ) {
2622 const [ pasteFile , setPasteFile ] = useState < File | undefined > ( undefined )
2723 const [ pasteContentBuffer , setPasteContentBuffer ] = useState < ArrayBuffer | undefined > ( undefined )
@@ -73,7 +69,6 @@ export function DisplayPaste() {
7369
7470 const inferredFilename = filename || ( ext && name + ext ) || filenameFromDisp
7571 const respBytes = await resp . bytes ( )
76- const isBinary = lang === null && inferredFilename !== undefined && isBinaryPath ( inferredFilename )
7772 setPasteLang ( lang || undefined )
7873
7974 const keyString = url . hash . slice ( 1 )
@@ -84,7 +79,8 @@ export function DisplayPaste() {
8479 setDecrypted ( "encrypted" )
8580 setFileBinary ( true )
8681 } else {
87- setFileBinary ( isBinary )
82+ const encoding = chardet . detect ( respBytes )
83+ setFileBinary ( encoding !== "utf8" && encoding !== "ASCII" )
8884 }
8985 } else {
9086 let key : CryptoKey | undefined
@@ -109,8 +105,8 @@ export function DisplayPaste() {
109105 setPasteContentBuffer ( decrypted )
110106 setPasteLang ( lang || undefined )
111107
112- const isBinary = lang === null && inferredFilename !== undefined && isBinaryPath ( inferredFilename )
113- setFileBinary ( isBinary )
108+ const encoding = chardet . detect ( decrypted )
109+ setFileBinary ( encoding !== "utf8" && encoding !== "ASCII" )
114110 setDecrypted ( "decrypted" )
115111 }
116112 } finally {
@@ -127,7 +123,7 @@ export function DisplayPaste() {
127123 < div className = "absolute top-[50%] left-[50%] translate-[-50%] flex flex-col items-center w-full" >
128124 < div className = "text-foreground-600 mb-2" > { `${ pasteFile ?. name } (${ formatSize ( pasteFile . size ) } )` } </ div >
129125 < div className = "w-fit text-center" >
130- Possibly Binary file{ " " }
126+ This file seems to be binary or not in UTF-8. { " " }
131127 < button className = "text-primary-500 inline" onClick = { ( ) => setForceShowBinary ( true ) } >
132128 (Click to show)
133129 </ button >
0 commit comments