@@ -110,27 +110,24 @@ export class PrismaDocumentService extends PrismaServiceBase {
110110 const documentType = this . determineDocumentType ( file . mimeType , file . originalName ) ;
111111 const textContent = this . extractTextContent ( file . content , documentType ) ;
112112
113- // Store both text content and metadata as JSON in the content field
114- const documentContent = JSON . stringify ( {
115- originalName : file . originalName ,
116- mimeType : file . mimeType ,
117- size : file . size ,
118- type : documentType ,
119- uploadedBy,
120- metadata : metadata || { } ,
121- textContent : textContent || '' ,
122- binaryContent : Buffer . isBuffer ( file . content )
123- ? file . content . toString ( 'base64' )
124- : Buffer . from ( file . content , 'utf-8' ) . toString ( 'base64' )
125- } ) ;
113+ // Prepare binary content
114+ const binaryContent = Buffer . isBuffer ( file . content )
115+ ? file . content
116+ : Buffer . from ( file . content , 'utf-8' ) ;
126117
127118 const document = await this . prismaClient ! . devlogDocument . create ( {
128119 data : {
129120 id : documentId ,
130121 devlogId : Number ( devlogId ) ,
131- title : file . originalName ,
132- content : documentContent ,
133- contentType : file . mimeType ,
122+ filename : documentId ,
123+ originalName : file . originalName ,
124+ mimeType : file . mimeType ,
125+ size : file . size ,
126+ type : documentType ,
127+ textContent : textContent || null ,
128+ binaryContent : binaryContent ,
129+ metadata : metadata || { } ,
130+ uploadedBy : uploadedBy || null ,
134131 } ,
135132 } ) ;
136133
@@ -202,24 +199,10 @@ export class PrismaDocumentService extends PrismaServiceBase {
202199 try {
203200 const document = await this . prismaClient ! . devlogDocument . findUnique ( {
204201 where : { id : documentId } ,
205- select : { content : true } ,
202+ select : { binaryContent : true } ,
206203 } ) ;
207204
208- if ( ! document ?. content ) {
209- return null ;
210- }
211-
212- try {
213- const parsedContent = JSON . parse ( document . content ) ;
214- if ( parsedContent . binaryContent ) {
215- return Buffer . from ( parsedContent . binaryContent , 'base64' ) ;
216- }
217- } catch {
218- // If content is not JSON, treat as plain text
219- return Buffer . from ( document . content , 'utf-8' ) ;
220- }
221-
222- return null ;
205+ return document ?. binaryContent ? Buffer . from ( document . binaryContent ) : null ;
223206 } catch ( error ) {
224207 console . error ( '[DocumentService] Failed to get document content:' , error ) ;
225208 throw new Error ( `Failed to get document content: ${ error instanceof Error ? error . message : 'Unknown error' } ` ) ;
@@ -249,13 +232,14 @@ export class PrismaDocumentService extends PrismaServiceBase {
249232 try {
250233 const where : any = {
251234 OR : [
252- { title : { contains : query , mode : 'insensitive' } } ,
253- { content : { contains : query , mode : 'insensitive' } } ,
235+ { originalName : { contains : query , mode : 'insensitive' } } ,
236+ { textContent : { contains : query , mode : 'insensitive' } } ,
254237 ] ,
255238 } ;
256239
257240 if ( options ?. devlogId ) where . devlogId = Number ( options . devlogId ) ;
258- if ( options ?. mimeType ) where . contentType = { contains : options . mimeType } ;
241+ if ( options ?. type ) where . type = options . type ;
242+ if ( options ?. mimeType ) where . mimeType = { contains : options . mimeType } ;
259243
260244 const [ documents , total ] = await Promise . all ( [
261245 this . prismaClient ! . devlogDocument . findMany ( {
@@ -309,19 +293,13 @@ export class PrismaDocumentService extends PrismaServiceBase {
309293 throw new Error ( 'Document not found' ) ;
310294 }
311295
312- // Parse existing content and update metadata
313- let parsedContent ;
314- try {
315- parsedContent = JSON . parse ( existingDoc . content ) ;
316- } catch {
317- parsedContent = { metadata : { } } ;
318- }
319-
320- parsedContent . metadata = { ...parsedContent . metadata , ...metadata } ;
296+ // Merge with existing metadata
297+ const existingMetadata = existingDoc . metadata as Record < string , any > || { } ;
298+ const updatedMetadata = { ...existingMetadata , ...metadata } ;
321299
322300 const document = await this . prismaClient ! . devlogDocument . update ( {
323301 where : { id : documentId } ,
324- data : { content : JSON . stringify ( parsedContent ) } ,
302+ data : { metadata : updatedMetadata } ,
325303 } ) ;
326304
327305 return this . mapPrismaToDocument ( document ) ;
@@ -395,28 +373,17 @@ export class PrismaDocumentService extends PrismaServiceBase {
395373 try {
396374 const documents = await this . prismaClient ! . devlogDocument . findMany ( {
397375 where : { devlogId : Number ( devlogId ) } ,
398- select : { content : true , contentType : true } ,
376+ select : { size : true , type : true } ,
399377 } ) ;
400378
401379 const totalDocuments = documents . length ;
402380 let totalSize = 0 ;
403381 const typeBreakdown : Record < string , number > = { } ;
404382
405383 documents . forEach ( doc => {
406- try {
407- const parsedContent = JSON . parse ( doc . content ) ;
408- if ( parsedContent . size ) {
409- totalSize += parsedContent . size ;
410- }
411- if ( parsedContent . type ) {
412- typeBreakdown [ parsedContent . type ] = ( typeBreakdown [ parsedContent . type ] || 0 ) + 1 ;
413- }
414- } catch {
415- // If content is not JSON, estimate size and use contentType
416- totalSize += doc . content . length ;
417- const documentType = this . determineDocumentType ( doc . contentType , '' ) ;
418- typeBreakdown [ documentType ] = ( typeBreakdown [ documentType ] || 0 ) + 1 ;
419- }
384+ totalSize += doc . size ;
385+ const documentType = doc . type as DocumentType ;
386+ typeBreakdown [ documentType ] = ( typeBreakdown [ documentType ] || 0 ) + 1 ;
420387 } ) ;
421388
422389 return {
@@ -497,34 +464,18 @@ export class PrismaDocumentService extends PrismaServiceBase {
497464 * Map Prisma document entity to domain type
498465 */
499466 private mapPrismaToDocument ( prismaDoc : any ) : DevlogDocument {
500- // Try to parse the content as JSON to extract structured data
501- let parsedContent : any = { } ;
502- try {
503- parsedContent = JSON . parse ( prismaDoc . content ) ;
504- } catch {
505- // If content is not JSON, treat as plain text content
506- parsedContent = {
507- textContent : prismaDoc . content ,
508- originalName : prismaDoc . title ,
509- mimeType : prismaDoc . contentType ,
510- type : this . determineDocumentType ( prismaDoc . contentType , prismaDoc . title ) ,
511- size : prismaDoc . content . length ,
512- metadata : { } ,
513- } ;
514- }
515-
516467 return {
517468 id : prismaDoc . id ,
518469 devlogId : prismaDoc . devlogId ,
519- filename : prismaDoc . id , // Use ID as filename since we don't store it separately
520- originalName : parsedContent . originalName || prismaDoc . title ,
521- mimeType : parsedContent . mimeType || prismaDoc . contentType ,
522- size : parsedContent . size || prismaDoc . content . length ,
523- type : parsedContent . type || this . determineDocumentType ( prismaDoc . contentType , prismaDoc . title ) ,
524- content : parsedContent . textContent || undefined ,
525- metadata : parsedContent . metadata || { } ,
470+ filename : prismaDoc . filename ,
471+ originalName : prismaDoc . originalName ,
472+ mimeType : prismaDoc . mimeType ,
473+ size : prismaDoc . size ,
474+ type : prismaDoc . type as DocumentType ,
475+ content : prismaDoc . textContent || undefined ,
476+ metadata : prismaDoc . metadata as Record < string , any > || { } ,
526477 uploadedAt : prismaDoc . createdAt ?. toISOString ( ) || new Date ( ) . toISOString ( ) ,
527- uploadedBy : parsedContent . uploadedBy || undefined ,
478+ uploadedBy : prismaDoc . uploadedBy || undefined ,
528479 } ;
529480 }
530481
0 commit comments