@@ -21,12 +21,10 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2121 * Node-specifics.
2222 */
2323
24- import { readdirSync , readFileSync } from "fs" ;
24+ import { readdirSync } from "fs" ;
2525import { join , relative , resolve } from "path" ;
2626
27- import { getMimeForTextFile } from "../_helpers/mime" ;
28- import { AttachmentEncoding } from "../enums/attachmentEncoding" ;
29- import { Attachment } from "../models/attachment" ;
27+ import type { AttachmentFactory } from "../factories/fromPath.node" ;
3028import { NamedLicense } from "../models/license" ;
3129
3230
@@ -36,6 +34,12 @@ import {NamedLicense} from "../models/license";
3634 */
3735export class LicenseEvidenceBuilder {
3836
37+ readonly #afac: AttachmentFactory
38+
39+ constructor ( afac : AttachmentFactory ) {
40+ this . #afac = afac
41+ }
42+
3943 readonly #LICENSE_FILENAME_PATTERN = / ^ (?: U N ) ? L I C E N [ C S ] E | .\. L I C E N [ C S ] E $ | ^ N O T I C E $ / i
4044
4145 /**
@@ -47,10 +51,6 @@ export class LicenseEvidenceBuilder {
4751 * @returns {@link NamedLicense } on success
4852 */
4953 public fromFile ( file : string , relativeFrom : string | undefined = undefined ) : NamedLicense | undefined {
50- const contentType = getMimeForTextFile ( file )
51- if ( contentType === undefined ) {
52- return undefined
53- }
5454 let lname
5555 if ( relativeFrom === undefined ) {
5656 lname = `file: ${ file } `
@@ -59,26 +59,19 @@ export class LicenseEvidenceBuilder {
5959 file = resolve ( relativeFrom , file )
6060 lname = `file: ${ relative ( relativeFrom , file ) } `
6161 }
62- return new NamedLicense (
63- `file: ${ lname } ` ,
64- {
65- text : new Attachment (
66- // may throw if `readFileSync()` fails
67- readFileSync ( file ) . toString ( 'base64' ) ,
68- {
69- contentType,
70- encoding : AttachmentEncoding . Base64
71- }
72- )
73- } )
62+ const text = this . #afac. fromTextFile ( file )
63+ if ( text === undefined ) {
64+ return undefined
65+ }
66+ return new NamedLicense ( lname , { text} )
7467 }
7568
7669 /**
7770 * Returns a generator for license evidences.
7871 * Throws errors, if dir cannot be inspected.
7972 *
8073 * @param dir - path to inspect
81- * @param relativeFrom - path the dir shall be relative to
74+ * @param relativeFrom - path the dir and files shall be relative to
8275 */
8376 public * fromDir ( dir : string , relativeFrom : string | undefined = undefined ) : Generator < NamedLicense > {
8477 if ( relativeFrom !== undefined ) {
0 commit comments