fix: allow more file extensions (#12099)

Co-authored-by: icarus <eurfelux@gmail.com>
This commit is contained in:
jardel 2025-12-25 16:59:13 +08:00 committed by GitHub
parent 05dfb459a6
commit 4ae9bf8ff4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,7 +2,7 @@ import { loggerService } from '@logger'
import { import {
checkName, checkName,
getFilesDir, getFilesDir,
getFileType, getFileType as getFileTypeByExt,
getName, getName,
getNotesDir, getNotesDir,
getTempDir, getTempDir,
@ -11,13 +11,13 @@ import {
} from '@main/utils/file' } from '@main/utils/file'
import { documentExts, imageExts, KB, MB } from '@shared/config/constant' import { documentExts, imageExts, KB, MB } from '@shared/config/constant'
import type { FileMetadata, NotesTreeNode } from '@types' import type { FileMetadata, NotesTreeNode } from '@types'
import { FileTypes } from '@types'
import chardet from 'chardet' import chardet from 'chardet'
import type { FSWatcher } from 'chokidar' import type { FSWatcher } from 'chokidar'
import chokidar from 'chokidar' import chokidar from 'chokidar'
import * as crypto from 'crypto' import * as crypto from 'crypto'
import type { OpenDialogOptions, OpenDialogReturnValue, SaveDialogOptions, SaveDialogReturnValue } from 'electron' import type { OpenDialogOptions, OpenDialogReturnValue, SaveDialogOptions, SaveDialogReturnValue } from 'electron'
import { app } from 'electron' import { app, dialog, net, shell } from 'electron'
import { dialog, net, shell } from 'electron'
import * as fs from 'fs' import * as fs from 'fs'
import { writeFileSync } from 'fs' import { writeFileSync } from 'fs'
import { readFile } from 'fs/promises' import { readFile } from 'fs/promises'
@ -185,7 +185,7 @@ class FileStorage {
}) })
} }
findDuplicateFile = async (filePath: string): Promise<FileMetadata | null> => { private findDuplicateFile = async (filePath: string): Promise<FileMetadata | null> => {
const stats = fs.statSync(filePath) const stats = fs.statSync(filePath)
logger.debug(`stats: ${stats}, filePath: ${filePath}`) logger.debug(`stats: ${stats}, filePath: ${filePath}`)
const fileSize = stats.size const fileSize = stats.size
@ -204,6 +204,8 @@ class FileStorage {
if (originalHash === storedHash) { if (originalHash === storedHash) {
const ext = path.extname(file) const ext = path.extname(file)
const id = path.basename(file, ext) const id = path.basename(file, ext)
const type = await this.getFileType(filePath)
return { return {
id, id,
origin_name: file, origin_name: file,
@ -212,7 +214,7 @@ class FileStorage {
created_at: storedStats.birthtime.toISOString(), created_at: storedStats.birthtime.toISOString(),
size: storedStats.size, size: storedStats.size,
ext, ext,
type: getFileType(ext), type,
count: 2 count: 2
} }
} }
@ -222,6 +224,13 @@ class FileStorage {
return null return null
} }
public getFileType = async (filePath: string): Promise<FileTypes> => {
const ext = path.extname(filePath)
const fileType = getFileTypeByExt(ext)
return fileType === FileTypes.OTHER && (await this._isTextFile(filePath)) ? FileTypes.TEXT : fileType
}
public selectFile = async ( public selectFile = async (
_: Electron.IpcMainInvokeEvent, _: Electron.IpcMainInvokeEvent,
options?: OpenDialogOptions options?: OpenDialogOptions
@ -241,7 +250,7 @@ class FileStorage {
const fileMetadataPromises = result.filePaths.map(async (filePath) => { const fileMetadataPromises = result.filePaths.map(async (filePath) => {
const stats = fs.statSync(filePath) const stats = fs.statSync(filePath)
const ext = path.extname(filePath) const ext = path.extname(filePath)
const fileType = getFileType(ext) const fileType = await this.getFileType(filePath)
return { return {
id: uuidv4(), id: uuidv4(),
@ -307,7 +316,7 @@ class FileStorage {
} }
const stats = await fs.promises.stat(destPath) const stats = await fs.promises.stat(destPath)
const fileType = getFileType(ext) const fileType = await this.getFileType(destPath)
const fileMetadata: FileMetadata = { const fileMetadata: FileMetadata = {
id: uuid, id: uuid,
@ -332,8 +341,7 @@ class FileStorage {
} }
const stats = fs.statSync(filePath) const stats = fs.statSync(filePath)
const ext = path.extname(filePath) const fileType = await this.getFileType(filePath)
const fileType = getFileType(ext)
return { return {
id: uuidv4(), id: uuidv4(),
@ -342,7 +350,7 @@ class FileStorage {
path: filePath, path: filePath,
created_at: stats.birthtime.toISOString(), created_at: stats.birthtime.toISOString(),
size: stats.size, size: stats.size,
ext: ext, ext: path.extname(filePath),
type: fileType, type: fileType,
count: 1 count: 1
} }
@ -690,7 +698,7 @@ class FileStorage {
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
size: buffer.length, size: buffer.length,
ext: ext.slice(1), ext: ext.slice(1),
type: getFileType(ext), type: getFileTypeByExt(ext),
count: 1 count: 1
} }
} catch (error) { } catch (error) {
@ -740,7 +748,7 @@ class FileStorage {
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
size: stats.size, size: stats.size,
ext: ext.slice(1), ext: ext.slice(1),
type: getFileType(ext), type: getFileTypeByExt(ext),
count: 1 count: 1
} }
} catch (error) { } catch (error) {
@ -1317,7 +1325,7 @@ class FileStorage {
await fs.promises.writeFile(destPath, buffer) await fs.promises.writeFile(destPath, buffer)
const stats = await fs.promises.stat(destPath) const stats = await fs.promises.stat(destPath)
const fileType = getFileType(ext) const fileType = await this.getFileType(destPath)
return { return {
id: uuid, id: uuid,
@ -1604,6 +1612,10 @@ class FileStorage {
} }
public isTextFile = async (_: Electron.IpcMainInvokeEvent, filePath: string): Promise<boolean> => { public isTextFile = async (_: Electron.IpcMainInvokeEvent, filePath: string): Promise<boolean> => {
return this._isTextFile(filePath)
}
private _isTextFile = async (filePath: string): Promise<boolean> => {
try { try {
const isBinary = await isBinaryFile(filePath) const isBinary = await isBinaryFile(filePath)
if (isBinary) { if (isBinary) {