Skip to content

Commit

Permalink
Fix the file type not detected on Windows (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhy committed Aug 26, 2024
1 parent 394d8a1 commit 714360b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@
"engines": {
"node": ">=20"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { fillExist } from "@service/components/import-processor"
import { AUTHOR_EMAIL } from "@src/package"
import { IS_WINDOWS } from "@util/constant/environment"
import { extractHostname, isBrowserUrl } from "@util/pattern"
import { formatTimeYMD, MILL_PER_SECOND } from "@util/time"

Expand Down Expand Up @@ -155,8 +156,13 @@ async function parseHistoryTrendsUnlimited(file: File): Promise<timer.imported.R
throw new Error("Invalid file format")
}

const isJsonFile = (file: File): boolean => file?.type?.startsWith('application/json')
const isJsonFile = (file: File): boolean => matchFileTypeOrExtOnWin(file, 'application/json', '.json')

const isCsvFile = (file: File): boolean => file?.type?.startsWith('text/csv')
const isCsvFile = (file: File): boolean => matchFileTypeOrExtOnWin(file, 'text/csv', '.csv')

const isTsvFile = (file: File): boolean => file?.type?.startsWith('text/tab-separated-values')
const isTsvFile = (file: File): boolean => matchFileTypeOrExtOnWin(file, 'text/tab-separated-values', '.tsv')

const matchFileTypeOrExtOnWin = (file: File, fileType: string, fileExtOnWin: string): boolean => {
const { type, name } = file || {}
return type?.startsWith(fileType) || (IS_WINDOWS && name?.toLowerCase()?.endsWith(fileExtOnWin))
}
20 changes: 19 additions & 1 deletion src/util/constant/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* https://opensource.org/licenses/MIT
*/

const { userAgent } = navigator
const { userAgent, platform } = navigator
let isFirefox = false
let isChrome = false
let isEdge = false
Expand Down Expand Up @@ -58,6 +58,24 @@ export const IS_SAFARI: boolean = isSafari
*/
export const BROWSER_MAJOR_VERSION = browserMajorVersion

type NavigatorWithUAData = Navigator & {
userAgentData?: {
platform: string
}
}

let isWindows = false
if (((navigator as unknown as NavigatorWithUAData)?.userAgentData)?.platform === 'Windows') {
isWindows = true
} else if (platform?.startsWith('Win')) {
isWindows = true
}

/**
* @since 2.4.2
*/
export const IS_WINDOWS = isWindows

/**
* @since 1.4.4
*/
Expand Down

0 comments on commit 714360b

Please sign in to comment.