@@ -9,7 +9,12 @@ import { Dispatcher } from './Dispatcher/Dispatcher';
9
9
import { DispatchableItem } from './Dispatcher/DispatchableItem' ;
10
10
import { DispatcherResponse } from './Dispatcher/DispatcherResponse' ;
11
11
import { ScannerCfg } from './ScannerCfg' ;
12
- import { ScannerEvents , ScannerInput , ScannerResults } from './ScannerTypes' ;
12
+ import {
13
+ ContentScannerInput , ScannerComponent ,
14
+ ScannerEvents ,
15
+ ScannerInput ,
16
+ ScannerResults
17
+ } from './ScannerTypes' ;
13
18
14
19
import { WfpProvider } from './WfpProvider/WfpProvider' ;
15
20
import { FingerprintPackage } from './WfpProvider/FingerprintPackage' ;
@@ -18,11 +23,15 @@ import { WfpSplitter } from './WfpProvider/WfpSplitter/WfpSplitter';
18
23
19
24
import sortPaths from 'sort-paths' ;
20
25
import { v4 as uuidv4 } from 'uuid' ;
26
+ import path from 'path' ;
21
27
22
28
let finishPromiseResolve ;
23
29
let finishPromiseReject ;
24
30
25
31
export class Scanner extends EventEmitter {
32
+
33
+ private readonly SCAN_FOLDER_NAME = 'scanner' ;
34
+
26
35
private scannerCfg : ScannerCfg ;
27
36
28
37
private workDirectory : string ;
@@ -67,6 +76,10 @@ export class Scanner extends EventEmitter {
67
76
this . scannerId = new Date ( ) . getTime ( ) . toString ( ) ;
68
77
}
69
78
79
+ private getScanFolderId ( ) {
80
+ return `${ this . SCAN_FOLDER_NAME } -${ this . getScannerId ( ) } ` ;
81
+ }
82
+
70
83
public init ( ) {
71
84
this . scanFinished = false ;
72
85
this . processingNewData = false ;
@@ -89,7 +102,7 @@ export class Scanner extends EventEmitter {
89
102
this . setDispatcherListeners ( ) ;
90
103
91
104
if ( this . workDirectory === undefined )
92
- this . setWorkDirectory ( `${ os . tmpdir ( ) } /scanner- ${ this . getScannerId ( ) } ` ) ;
105
+ this . setWorkDirectory ( `${ os . tmpdir ( ) } /${ this . getScanFolderId ( ) } ` ) ;
93
106
}
94
107
95
108
public setWorkDirectory ( workDirectory : string ) {
@@ -118,7 +131,8 @@ export class Scanner extends EventEmitter {
118
131
if ( fs . existsSync ( this . wfpFilePath ) ) fs . unlinkSync ( this . wfpFilePath ) ;
119
132
}
120
133
121
- public scan ( scannerInput : Array < ScannerInput > ) : Promise < string > {
134
+ public async scan ( scannerInput : Array < ScannerInput > ) :Promise < string > {
135
+
122
136
this . init ( ) ;
123
137
this . createOutputFiles ( ) ;
124
138
this . scannerInput = scannerInput ;
@@ -149,6 +163,43 @@ export class Scanner extends EventEmitter {
149
163
return this . finishPromise ;
150
164
}
151
165
166
+ /**
167
+ * Scans the provided content.
168
+ *
169
+ * @param {ContentScannerInput } contentScannerInput - The input containing content and file name.
170
+ * @param {string } contentScannerInput.content - The content to be scanned.
171
+ * @param {string } contentScannerInput.key - Unique key to be referenced on scan result .
172
+ * @returns {Promise<ScannerComponent | null> } - The scan result as a `ScannerComponent` or `null` if no content is provided.
173
+ *
174
+ * @throws {Error } - Throws an error if there is an issue during the scan.
175
+ *
176
+ * */
177
+ public async scanContents ( contentScannerInput : ContentScannerInput ) :Promise < ScannerComponent | null > {
178
+ if ( ! contentScannerInput . content ) {
179
+ this . reportLog ( '[ SCANNER ]: No input provided' , 'warning' ) ;
180
+ return null ;
181
+ }
182
+ const workingDir = `${ os . tmpdir ( ) } /${ this . getScanFolderId ( ) } ` ;
183
+ this . setWorkDirectory ( workingDir ) ;
184
+ this . workDirectory = workingDir ;
185
+
186
+ await fs . promises . writeFile ( `${ workingDir } /${ contentScannerInput . key } ` , contentScannerInput . content , 'utf-8' ) ;
187
+
188
+ const rootPath = path . resolve ( `${ workingDir } /${ contentScannerInput . key } ` ) ;
189
+
190
+ // Build the input for a common scan
191
+ const scannerInput : ScannerInput = {
192
+ folderRoot : workingDir ,
193
+ fileList : [ rootPath ] ,
194
+ } ;
195
+ const input = { ...contentScannerInput , ...scannerInput } ;
196
+
197
+ // Perform a common scan
198
+ const resultPath = await this . scan ( [ input ] ) ;
199
+
200
+ return JSON . parse ( await fs . promises . readFile ( resultPath , 'utf-8' ) ) as ScannerComponent ;
201
+ }
202
+
152
203
public getScannerId ( ) {
153
204
return this . scannerId ;
154
205
}
0 commit comments