@@ -74,6 +74,11 @@ private async Task ProcessFileAsync(FileEntry fileEntry, CancellationToken cance
7474 return ;
7575 }
7676
77+ if ( fileEntry . Deleted )
78+ {
79+ return ;
80+ }
81+
7782 using var scope = _serviceProvider . CreateScope ( ) ;
7883 var fileStorageManager = scope . ServiceProvider . GetService < IFileStorageManager > ( ) ;
7984 var markdownService = scope . ServiceProvider . GetService < MarkdownService > ( ) ;
@@ -91,19 +96,9 @@ private async Task ProcessFileAsync(FileEntry fileEntry, CancellationToken cance
9196
9297 var chunks = TextChunkingService . ChunkSentences ( Encoding . UTF8 . GetString ( bytes ) ) ;
9398
94- var chunksFolder = Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Chunks" , fileEntry . Id . ToString ( ) ) ;
99+ var chunksFolder = CreateDirectoryIfNotExist ( Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Chunks" , fileEntry . Id . ToString ( ) ) ) ;
95100
96- if ( ! Directory . Exists ( chunksFolder ) )
97- {
98- Directory . CreateDirectory ( chunksFolder ) ;
99- }
100-
101- var embeddingsFolder = Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Embeddings" , fileEntry . Id . ToString ( ) ) ;
102-
103- if ( ! Directory . Exists ( embeddingsFolder ) )
104- {
105- Directory . CreateDirectory ( embeddingsFolder ) ;
106- }
101+ var embeddingsFolder = CreateDirectoryIfNotExist ( Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Embeddings" , fileEntry . Id . ToString ( ) ) ) ;
107102
108103 foreach ( var chunk in chunks )
109104 {
@@ -119,12 +114,7 @@ private async Task ProcessFileAsync(FileEntry fileEntry, CancellationToken cance
119114 {
120115 _logger . LogInformation ( "Converting file to markdown for FileEntry Id: {FileEntryId}" , fileEntry ? . Id ) ;
121116
122- var markdownFolder = Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Markdown" ) ;
123-
124- if ( ! Directory . Exists ( markdownFolder ) )
125- {
126- Directory . CreateDirectory ( markdownFolder ) ;
127- }
117+ var markdownFolder = CreateDirectoryIfNotExist ( Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Markdown" ) ) ;
128118
129119 var markdownFile = Path . Combine ( markdownFolder , fileEntry . Id + ".md" ) ;
130120
@@ -137,19 +127,9 @@ private async Task ProcessFileAsync(FileEntry fileEntry, CancellationToken cance
137127
138128 var chunks = TextChunkingService . ChunkSentences ( await File . ReadAllTextAsync ( markdownFile , cancellationToken ) ) ;
139129
140- var chunksFolder = Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Chunks" , fileEntry . Id . ToString ( ) ) ;
130+ var chunksFolder = CreateDirectoryIfNotExist ( Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Chunks" , fileEntry . Id . ToString ( ) ) ) ;
141131
142- if ( ! Directory . Exists ( chunksFolder ) )
143- {
144- Directory . CreateDirectory ( chunksFolder ) ;
145- }
146-
147- var embeddingsFolder = Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Embeddings" , fileEntry . Id . ToString ( ) ) ;
148-
149- if ( ! Directory . Exists ( embeddingsFolder ) )
150- {
151- Directory . CreateDirectory ( embeddingsFolder ) ;
152- }
132+ var embeddingsFolder = CreateDirectoryIfNotExist ( Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Embeddings" , fileEntry . Id . ToString ( ) ) ) ;
153133
154134 foreach ( var chunk in chunks )
155135 {
@@ -164,21 +144,12 @@ private async Task ProcessFileAsync(FileEntry fileEntry, CancellationToken cance
164144 {
165145 _logger . LogInformation ( "Processing image file for FileEntry Id: {FileEntryId}" , fileEntry ? . Id ) ;
166146
167- var imageAnalysisFolder = Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "ImageAnalysis" ) ;
147+ var imageAnalysisFolder = CreateDirectoryIfNotExist ( Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "ImageAnalysis" ) ) ;
168148
169- if ( ! Directory . Exists ( imageAnalysisFolder ) )
170- {
171- Directory . CreateDirectory ( imageAnalysisFolder ) ;
172- }
149+ var embeddingsFolder = CreateDirectoryIfNotExist ( Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Embeddings" , fileEntry . Id . ToString ( ) ) ) ;
173150
174- var embeddingsFolder = Path . Combine ( _configuration [ "Storage:TempFolderPath" ] , "Embeddings" , fileEntry . Id . ToString ( ) ) ;
175-
176- if ( ! Directory . Exists ( embeddingsFolder ) )
177- {
178- Directory . CreateDirectory ( embeddingsFolder ) ;
179- }
180-
181- var imageAnalysisFile = Path . Combine ( imageAnalysisFolder , fileEntry . Id + ".json" ) ;
151+ var imageAnalysisFile = Path . Combine ( imageAnalysisFolder , $ "{ fileEntry . Id } .json") ;
152+ var embeddingFile = Path . Combine ( embeddingsFolder , $ "{ fileEntry . Id } .json") ;
182153
183154 if ( ! File . Exists ( imageAnalysisFile ) )
184155 {
@@ -189,12 +160,14 @@ private async Task ProcessFileAsync(FileEntry fileEntry, CancellationToken cance
189160 var json = JsonSerializer . Serialize ( imageAnalysisResult ) ;
190161
191162 await File . WriteAllTextAsync ( imageAnalysisFile , json , cancellationToken ) ;
163+ }
192164
165+ if ( ! File . Exists ( embeddingFile ) )
166+ {
167+ var json = await File . ReadAllTextAsync ( imageAnalysisFile , cancellationToken ) ;
193168 var embedding = await embeddingService . GenerateAsync ( json , cancellationToken ) ;
194- await File . WriteAllTextAsync ( Path . Combine ( embeddingsFolder , $ " { fileEntry . Id } .json" ) , JsonSerializer . Serialize ( embedding ) , cancellationToken ) ;
169+ await File . WriteAllTextAsync ( embeddingFile , JsonSerializer . Serialize ( embedding ) , cancellationToken ) ;
195170 }
196-
197-
198171 }
199172 }
200173
@@ -224,4 +197,14 @@ private async Task<byte[]> GetBytesAsync(IFileStorageManager fileStorageManager,
224197
225198 return content ;
226199 }
200+
201+ private static string CreateDirectoryIfNotExist ( string path )
202+ {
203+ if ( ! Directory . Exists ( path ) )
204+ {
205+ Directory . CreateDirectory ( path ) ;
206+ }
207+
208+ return path ;
209+ }
227210}
0 commit comments