Skip to content

Commit 98973e5

Browse files
committed
feat
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 6186d73 commit 98973e5

File tree

2 files changed

+69
-21
lines changed

2 files changed

+69
-21
lines changed

src/builders/fromPath.node.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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";
2525
import {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";
3028
import {NamedLicense} from "../models/license";
3129

3230

@@ -36,6 +34,12 @@ import {NamedLicense} from "../models/license";
3634
*/
3735
export class LicenseEvidenceBuilder {
3836

37+
readonly #afac: AttachmentFactory
38+
39+
constructor(afac: AttachmentFactory) {
40+
this.#afac = afac
41+
}
42+
3943
readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/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) {

src/factories/fromPath.node.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*!
2+
This file is part of CycloneDX JavaScript Library.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache-2.0
17+
Copyright (c) OWASP Foundation. All Rights Reserved.
18+
*/
19+
20+
/**
21+
* Node-specifics.
22+
*/
23+
24+
import {readFileSync} from "fs";
25+
26+
import {getMimeForTextFile} from "../_helpers/mime";
27+
import {AttachmentEncoding} from "../enums/attachmentEncoding";
28+
import {Attachment} from "../models/attachment";
29+
30+
31+
32+
/**
33+
* Node-specific AttachmentFactory.
34+
*/
35+
export class AttachmentFactory {
36+
37+
public fromFile(file: string, contentType: string): Attachment {
38+
return new Attachment(
39+
// may throw if `readFileSync()` fails
40+
readFileSync(file).toString('base64'),
41+
{
42+
contentType,
43+
encoding: AttachmentEncoding.Base64
44+
})
45+
}
46+
47+
public fromTextFile(file: string): Attachment | undefined {
48+
const contentType = getMimeForTextFile(file)
49+
if (contentType === undefined) {
50+
return undefined
51+
}
52+
return this.fromFile(file, contentType)
53+
}
54+
55+
}

0 commit comments

Comments
 (0)