Fix getMimeType()-related functionality

This commit is contained in:
dimitrievgs 2025-11-11 23:03:20 +03:00
parent b773fdbc40
commit f61cf08efd
4 changed files with 79 additions and 132 deletions

103
main.js

File diff suppressed because one or more lines are too long

View File

@ -6,6 +6,7 @@
import { TFile, normalizePath } from 'obsidian';
import LLMAgentPlugin from 'main';
import { v4 as uuidv4 } from 'uuid'; // Нужно установить: npm install uuid @types/uuid
import { getMimeType } from 'src/utils/fileUtils';
export class CacheManager {
private plugin: LLMAgentPlugin;
@ -114,7 +115,7 @@ export class CacheManager {
uuid: uuid,
cachedPath: cachedFileName,
name: fileName,
mimeType: this.getMimeType(fileName)
mimeType: getMimeType(fileName)
};
}
@ -184,36 +185,4 @@ export class CacheManager {
return false;
}
}
private getMimeType(fileName: string): string {
const ext = fileName.split('.').pop()?.toLowerCase();
const mimeTypes: Record<string, string> = {
txt: 'text/plain',
md: 'text/markdown',
json: 'application/json',
js: 'text/javascript',
ts: 'text/typescript',
html: 'text/html',
css: 'text/css',
pdf: 'application/pdf',
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/gif',
svg: 'image/svg+xml',
webp: 'image/webp',
xml: 'application/xml',
csv: 'text/csv',
zip: 'application/zip',
py: 'text/x-python',
java: 'text/x-java',
cpp: 'text/x-c++src',
c: 'text/x-csrc',
h: 'text/x-chdr',
sh: 'application/x-sh',
yaml: 'text/yaml',
yml: 'text/yaml'
};
return mimeTypes[ext || ''] || 'application/octet-stream';
}
}

View File

@ -9,6 +9,7 @@ import { FileReference, ReferenceParser } from './ReferenceParser';
import { TemplateEngine } from './TemplateEngine';
import { Attachment, ChatHistoryItem } from 'src/components/models/ChatHistoryItem';
import { CacheManager } from './CacheManager';
import { getMimeType } from 'src/utils/fileUtils';
// ----------------------------------------------- Reference Expander ---------------------------------------------------------------
@ -110,7 +111,7 @@ export class ReferenceExpander {
const cacheResult = await this.cacheManager.cacheVaultFile(file);
cachedPath = cacheResult.cachedPath;
uuid = cacheResult.uuid;
mimeType = this.getMimeType(file.name);
mimeType = getMimeType(file.name);
sourcePath = file.path; // Относительный
} else {
console.warn(`Файл не найден в vault: ${reference.path}`);
@ -153,38 +154,6 @@ export class ReferenceExpander {
};
}
private getMimeType(fileName: string): string {
// Та же логика, что в CacheManager
const ext = fileName.split('.').pop()?.toLowerCase();
const mimeTypes: Record<string, string> = {
txt: 'text/plain',
md: 'text/markdown',
json: 'application/json',
js: 'text/javascript',
ts: 'text/typescript',
html: 'text/html',
css: 'text/css',
pdf: 'application/pdf',
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/gif',
svg: 'image/svg+xml',
xml: 'application/xml',
csv: 'text/csv',
zip: 'application/zip',
py: 'text/x-python',
java: 'text/x-java',
cpp: 'text/x-c++src',
c: 'text/x-csrc',
h: 'text/x-chdr',
sh: 'application/x-sh',
yaml: 'text/yaml',
yml: 'text/yaml'
};
return mimeTypes[ext || ''] || 'application/octet-stream';
}
/**
* Подготавливает сообщения для LLM с учетом вложений.
* Раскрывает только постоянные (@@/##) ссылки и ссылки в последнем сообщении.

38
src/utils/fileUtils.ts Normal file
View File

@ -0,0 +1,38 @@
// src/utils/fileUtils.ts
/**
* Возвращает MIME-тип файла на основе его расширения.
* @param fileName - Имя файла (например, "document.pdf", "image.png")
* @returns MIME-тип или 'application/octet-stream', если тип неизвестен
*/
export function getMimeType(fileName: string): string {
const ext = fileName.split('.').pop()?.toLowerCase();
const mimeTypes: Record<string, string> = {
txt: 'text/plain',
md: 'text/markdown',
json: 'application/json',
js: 'text/javascript',
ts: 'text/typescript',
html: 'text/html',
css: 'text/css',
pdf: 'application/pdf',
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/gif',
svg: 'image/svg+xml',
webp: 'image/webp',
xml: 'application/xml',
csv: 'text/csv',
zip: 'application/zip',
py: 'text/x-python',
java: 'text/x-java',
cpp: 'text/x-c++src',
c: 'text/x-csrc',
h: 'text/x-chdr',
sh: 'application/x-sh',
yaml: 'text/yaml',
yml: 'text/yaml'
};
return mimeTypes[ext || ''] || 'application/octet-stream';
}