@@ -6,7 +6,7 @@ import { INITIAL_SCHEMA } from './schema';
66import { v4 as uuidv4 } from 'uuid' ;
77import * as crypto from 'crypto' ;
88// Fix: Import types to use for casting
9- import type { Node , Document , DocVersion , DatabaseStats } from '../types' ;
9+ import type { Node , Document , DocVersion , DatabaseStats , DocType , ViewMode , ImportedNodeSummary } from '../types' ;
1010
1111let db : Database . Database ;
1212
@@ -43,6 +43,23 @@ const mapExtensionToLanguageId_local = (extension: string | null): string => {
4343 case 'application/pdf' :
4444 case 'pdf' :
4545 return 'pdf' ;
46+ case 'png' :
47+ case 'jpg' :
48+ case 'jpeg' :
49+ case 'gif' :
50+ case 'bmp' :
51+ case 'webp' :
52+ case 'svg' :
53+ case 'svgz' :
54+ case 'image/png' :
55+ case 'image/jpg' :
56+ case 'image/jpeg' :
57+ case 'image/gif' :
58+ case 'image/bmp' :
59+ case 'image/webp' :
60+ case 'image/svg' :
61+ case 'image/svg+xml' :
62+ return 'image' ;
4663 default : return 'plaintext' ;
4764 }
4865} ;
@@ -409,7 +426,8 @@ export const databaseService = {
409426 }
410427 } ,
411428
412- importFiles ( filesData : { path : string , name : string , content : string } [ ] , targetParentId : string | null ) : { success : boolean , error ?: string } {
429+ importFiles ( filesData : { path : string ; name : string ; content : string } [ ] , targetParentId : string | null ) : { success : boolean ; error ?: string ; createdNodes : ImportedNodeSummary [ ] } {
430+ const createdNodes : ImportedNodeSummary [ ] = [ ] ;
413431 const transaction = db . transaction ( ( ) => {
414432 console . log ( `Starting import transaction for ${ filesData . length } files.` ) ;
415433 const knownFolderPaths = new Map < string , string > ( ) ; // 'parentId/folderName' -> 'node_id'
@@ -461,15 +479,23 @@ export const databaseService = {
461479 const extension = file . name . split ( '.' ) . pop ( ) || null ;
462480 let languageHint = mapExtensionToLanguageId_local ( extension ) ;
463481
464- db . prepare ( `INSERT INTO nodes (node_id, parent_id, node_type, title, sort_order, created_at, updated_at) VALUES (?, ?, 'document', ?, ?, ?, ?)` ) . run ( newNodeId , currentParentId , file . name , sortOrder , now , now ) ;
465-
466482 const trimmedContent = file . content . trim ( ) ;
467- const isPdf = languageHint === 'pdf' || languageHint === 'application/pdf' || trimmedContent . startsWith ( 'data:application/pdf' ) ;
483+ const sample = trimmedContent . slice ( 0 , 64 ) . toLowerCase ( ) ;
484+ const isPdf = languageHint === 'pdf' || sample . startsWith ( 'data:application/pdf' ) ;
485+ const isSvgContent = sample . startsWith ( '<svg' ) ;
486+ const isImageDataUrl = sample . startsWith ( 'data:image/' ) ;
487+ const isImage = languageHint === 'image' || isImageDataUrl || isSvgContent ;
488+
468489 if ( isPdf ) {
469490 languageHint = 'pdf' ;
491+ } else if ( isImage ) {
492+ languageHint = 'image' ;
470493 }
471- const docType = isPdf ? 'pdf' : 'source_code' ;
472- const defaultViewMode = isPdf ? 'preview' : null ;
494+
495+ const docType : DocType = isPdf ? 'pdf' : isImage ? 'image' : 'source_code' ;
496+ const defaultViewMode : ViewMode | null = docType === 'pdf' || docType === 'image' ? 'preview' : null ;
497+
498+ db . prepare ( `INSERT INTO nodes (node_id, parent_id, node_type, title, sort_order, created_at, updated_at) VALUES (?, ?, 'document', ?, ?, ?, ?)` ) . run ( newNodeId , currentParentId , file . name , sortOrder , now , now ) ;
473499
474500 const docResult = db . prepare ( `INSERT INTO documents (node_id, doc_type, language_hint, default_view_mode) VALUES (?, ?, ?, ?)` )
475501 . run ( newNodeId , docType , languageHint , defaultViewMode ) ;
@@ -480,15 +506,23 @@ export const databaseService = {
480506 const newVersionId = Number ( versionResult . lastInsertRowid ) ;
481507 db . prepare ( 'UPDATE documents SET current_version_id = ? WHERE document_id = ?' ) . run ( newVersionId , documentId ) ;
482508 console . log ( `Created document "${ file . name } " with node id ${ newNodeId } ` ) ;
509+
510+ createdNodes . push ( {
511+ nodeId : newNodeId ,
512+ parentId : currentParentId ?? null ,
513+ docType,
514+ languageHint,
515+ defaultViewMode,
516+ } ) ;
483517 }
484518 } ) ;
485519
486520 try {
487521 transaction ( ) ;
488- return { success : true } ;
522+ return { success : true , createdNodes } ;
489523 } catch ( error ) {
490524 console . error ( 'File import transaction failed:' , error ) ;
491- return { success : false , error : error instanceof Error ? error . message : String ( error ) } ;
525+ return { success : false , error : error instanceof Error ? error . message : String ( error ) , createdNodes : [ ] } ;
492526 }
493527 } ,
494528
0 commit comments