chromiumly 3.2.1 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -2
- package/dist/chromium/converters/converter.js +1 -1
- package/dist/chromium/converters/converter.js.map +1 -1
- package/dist/chromium/converters/html.converter.js +1 -1
- package/dist/chromium/converters/html.converter.js.map +1 -1
- package/dist/chromium/converters/markdown.converter.js +1 -1
- package/dist/chromium/converters/markdown.converter.js.map +1 -1
- package/dist/chromium/converters/url.converter.js +1 -1
- package/dist/chromium/converters/url.converter.js.map +1 -1
- package/dist/chromium/screenshots/html.screenshot.js +1 -1
- package/dist/chromium/screenshots/html.screenshot.js.map +1 -1
- package/dist/chromium/screenshots/markdown.screenshot.js +1 -1
- package/dist/chromium/screenshots/markdown.screenshot.js.map +1 -1
- package/dist/chromium/screenshots/screenshot.js +1 -1
- package/dist/chromium/screenshots/screenshot.js.map +1 -1
- package/dist/chromium/screenshots/url.screenshot.js +1 -1
- package/dist/chromium/screenshots/url.screenshot.js.map +1 -1
- package/dist/gotenberg.d.ts +1 -1
- package/dist/gotenberg.js +4 -2
- package/dist/gotenberg.js.map +1 -1
- package/dist/libre-office/libre-office.js +2 -2
- package/dist/libre-office/libre-office.js.map +1 -1
- package/dist/main.config.d.ts +30 -3
- package/dist/main.config.js +42 -3
- package/dist/main.config.js.map +1 -1
- package/dist/main.d.ts +1 -0
- package/dist/main.js +3 -1
- package/dist/main.js.map +1 -1
- package/dist/pdf-engines/pdf-engines.js +8 -8
- package/dist/pdf-engines/pdf-engines.js.map +1 -1
- package/dist/pdf-engines/utils/pdf-engines.utils.js +5 -5
- package/dist/pdf-engines/utils/pdf-engines.utils.js.map +1 -1
- package/package.json +16 -11
- package/src/.prettierrc.yml +0 -4
- package/src/chromium/converters/converter.ts +0 -22
- package/src/chromium/converters/html.converter.ts +0 -134
- package/src/chromium/converters/markdown.converter.ts +0 -129
- package/src/chromium/converters/tests/html.converter.test.ts +0 -190
- package/src/chromium/converters/tests/markdown.converter.test.ts +0 -187
- package/src/chromium/converters/tests/url.converter.test.ts +0 -164
- package/src/chromium/converters/url.converter.ts +0 -125
- package/src/chromium/index.ts +0 -6
- package/src/chromium/interfaces/common.types.ts +0 -15
- package/src/chromium/interfaces/converter.types.ts +0 -45
- package/src/chromium/interfaces/screenshot.types.ts +0 -15
- package/src/chromium/screenshots/html.screenshot.ts +0 -105
- package/src/chromium/screenshots/markdown.screenshot.ts +0 -100
- package/src/chromium/screenshots/screenshot.ts +0 -22
- package/src/chromium/screenshots/tests/html.screenshot.test.ts +0 -192
- package/src/chromium/screenshots/tests/markdown.screenshot.test.ts +0 -176
- package/src/chromium/screenshots/tests/url.screenshot.test.ts +0 -166
- package/src/chromium/screenshots/url.screenshot.ts +0 -96
- package/src/chromium/utils/converter.utils.ts +0 -187
- package/src/chromium/utils/screenshot.utils.ts +0 -127
- package/src/chromium/utils/tests/converter.utils.test.ts +0 -496
- package/src/chromium/utils/tests/screenshot.utils.test.ts +0 -338
- package/src/common/constants.ts +0 -9
- package/src/common/gotenberg.utils.ts +0 -86
- package/src/common/index.ts +0 -3
- package/src/common/tests/gotenberg.utils.test.ts +0 -141
- package/src/common/types.ts +0 -7
- package/src/gotenberg.ts +0 -54
- package/src/libre-office/index.ts +0 -1
- package/src/libre-office/interfaces/libre-office.types.ts +0 -156
- package/src/libre-office/libre-office.ts +0 -61
- package/src/libre-office/tests/libre-office.test.ts +0 -56
- package/src/libre-office/utils/constants.ts +0 -132
- package/src/libre-office/utils/libre-office.utils.ts +0 -128
- package/src/libre-office/utils/tests/libre-office.utils.test.ts +0 -156
- package/src/main.config.ts +0 -106
- package/src/main.ts +0 -11
- package/src/pdf-engines/index.ts +0 -1
- package/src/pdf-engines/interfaces/pdf-engines.types.ts +0 -10
- package/src/pdf-engines/pdf-engines.ts +0 -156
- package/src/pdf-engines/tests/pdf.engine.test.ts +0 -163
- package/src/pdf-engines/utils/pdf-engines.utils.ts +0 -68
- package/src/pdf-engines/utils/tests/pdf-engines.utils.test.ts +0 -71
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { ReadStream } from 'fs';
|
|
2
|
-
import { Metadata, PdfFormat } from '../../common';
|
|
3
|
-
|
|
4
|
-
type FileExtension =
|
|
5
|
-
| '123'
|
|
6
|
-
| '602'
|
|
7
|
-
| 'abw'
|
|
8
|
-
| 'bib'
|
|
9
|
-
| 'bmp'
|
|
10
|
-
| 'cdr'
|
|
11
|
-
| 'cgm'
|
|
12
|
-
| 'cmx'
|
|
13
|
-
| 'csv'
|
|
14
|
-
| 'cwk'
|
|
15
|
-
| 'dbf'
|
|
16
|
-
| 'dif'
|
|
17
|
-
| 'doc'
|
|
18
|
-
| 'docm'
|
|
19
|
-
| 'docx'
|
|
20
|
-
| 'dot'
|
|
21
|
-
| 'dotm'
|
|
22
|
-
| 'dotx'
|
|
23
|
-
| 'dxf'
|
|
24
|
-
| 'emf'
|
|
25
|
-
| 'eps'
|
|
26
|
-
| 'epub'
|
|
27
|
-
| 'fodg'
|
|
28
|
-
| 'fodp'
|
|
29
|
-
| 'fods'
|
|
30
|
-
| 'fodt'
|
|
31
|
-
| 'fopd'
|
|
32
|
-
| 'gif'
|
|
33
|
-
| 'htm'
|
|
34
|
-
| 'html'
|
|
35
|
-
| 'hwp'
|
|
36
|
-
| 'jpeg'
|
|
37
|
-
| 'jpg'
|
|
38
|
-
| 'key'
|
|
39
|
-
| 'ltx'
|
|
40
|
-
| 'lwp'
|
|
41
|
-
| 'mcw'
|
|
42
|
-
| 'met'
|
|
43
|
-
| 'mml'
|
|
44
|
-
| 'mw'
|
|
45
|
-
| 'numbers'
|
|
46
|
-
| 'odd'
|
|
47
|
-
| 'odg'
|
|
48
|
-
| 'odm'
|
|
49
|
-
| 'odp'
|
|
50
|
-
| 'ods'
|
|
51
|
-
| 'odt'
|
|
52
|
-
| 'otg'
|
|
53
|
-
| 'oth'
|
|
54
|
-
| 'otp'
|
|
55
|
-
| 'ots'
|
|
56
|
-
| 'ott'
|
|
57
|
-
| 'pages'
|
|
58
|
-
| 'pbm'
|
|
59
|
-
| 'pcd'
|
|
60
|
-
| 'pct'
|
|
61
|
-
| 'pcx'
|
|
62
|
-
| 'pdb'
|
|
63
|
-
| 'pdf'
|
|
64
|
-
| 'pgm'
|
|
65
|
-
| 'png'
|
|
66
|
-
| 'pot'
|
|
67
|
-
| 'potm'
|
|
68
|
-
| 'potx'
|
|
69
|
-
| 'ppm'
|
|
70
|
-
| 'pps'
|
|
71
|
-
| 'ppt'
|
|
72
|
-
| 'pptm'
|
|
73
|
-
| 'pptx'
|
|
74
|
-
| 'psd'
|
|
75
|
-
| 'psw'
|
|
76
|
-
| 'pub'
|
|
77
|
-
| 'pwp'
|
|
78
|
-
| 'pxl'
|
|
79
|
-
| 'ras'
|
|
80
|
-
| 'rtf'
|
|
81
|
-
| 'sda'
|
|
82
|
-
| 'sdc'
|
|
83
|
-
| 'sdd'
|
|
84
|
-
| 'sdp'
|
|
85
|
-
| 'sdw'
|
|
86
|
-
| 'sgl'
|
|
87
|
-
| 'slk'
|
|
88
|
-
| 'smf'
|
|
89
|
-
| 'stc'
|
|
90
|
-
| 'std'
|
|
91
|
-
| 'sti'
|
|
92
|
-
| 'stw'
|
|
93
|
-
| 'svg'
|
|
94
|
-
| 'svm'
|
|
95
|
-
| 'swf'
|
|
96
|
-
| 'sxc'
|
|
97
|
-
| 'sxd'
|
|
98
|
-
| 'sxg'
|
|
99
|
-
| 'sxi'
|
|
100
|
-
| 'sxm'
|
|
101
|
-
| 'sxw'
|
|
102
|
-
| 'tga'
|
|
103
|
-
| 'tif'
|
|
104
|
-
| 'tiff'
|
|
105
|
-
| 'txt'
|
|
106
|
-
| 'uof'
|
|
107
|
-
| 'uop'
|
|
108
|
-
| 'uos'
|
|
109
|
-
| 'uot'
|
|
110
|
-
| 'vdx'
|
|
111
|
-
| 'vor'
|
|
112
|
-
| 'vsd'
|
|
113
|
-
| 'vsdm'
|
|
114
|
-
| 'vsdx'
|
|
115
|
-
| 'wb2'
|
|
116
|
-
| 'wk1'
|
|
117
|
-
| 'wks'
|
|
118
|
-
| 'wmf'
|
|
119
|
-
| 'wpd'
|
|
120
|
-
| 'wpg'
|
|
121
|
-
| 'wps'
|
|
122
|
-
| 'xbm'
|
|
123
|
-
| 'xhtml'
|
|
124
|
-
| 'xls'
|
|
125
|
-
| 'xlsb'
|
|
126
|
-
| 'xlsm'
|
|
127
|
-
| 'xlsx'
|
|
128
|
-
| 'xlt'
|
|
129
|
-
| 'xltm'
|
|
130
|
-
| 'xltx'
|
|
131
|
-
| 'xlw'
|
|
132
|
-
| 'xml'
|
|
133
|
-
| 'xpm'
|
|
134
|
-
| 'zabw';
|
|
135
|
-
|
|
136
|
-
type FileInfo = {
|
|
137
|
-
data: Buffer | ReadStream;
|
|
138
|
-
ext: FileExtension;
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
export type PathLikeOrReadStream = string | FileInfo;
|
|
142
|
-
|
|
143
|
-
export type PageProperties = {
|
|
144
|
-
landscape?: boolean; // Set the paper orientation to landscape (default false)
|
|
145
|
-
nativePageRanges?: { from: number; to: number }; // Page ranges to print
|
|
146
|
-
exportFormFields?: boolean; // Set whether to export the form fields or to use the inputted/selected content of the fields (default true)
|
|
147
|
-
singlePageSheets?: boolean; // Set whether to render the entire spreadsheet as a single page. (default false)
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
export type ConversionOptions = {
|
|
151
|
-
properties?: PageProperties;
|
|
152
|
-
merge?: boolean;
|
|
153
|
-
pdfa?: PdfFormat;
|
|
154
|
-
pdfUA?: boolean;
|
|
155
|
-
metadata?: Metadata;
|
|
156
|
-
};
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import FormData from 'form-data';
|
|
2
|
-
|
|
3
|
-
import { Chromiumly } from '../main.config';
|
|
4
|
-
import { GotenbergUtils, PdfFormat, Metadata } from '../common';
|
|
5
|
-
import {
|
|
6
|
-
PageProperties,
|
|
7
|
-
PathLikeOrReadStream
|
|
8
|
-
} from './interfaces/libre-office.types';
|
|
9
|
-
import { LibreOfficeUtils } from './utils/libre-office.utils';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Class representing a LibreOffice for various operations such as merging and conversion.
|
|
13
|
-
*/
|
|
14
|
-
export class LibreOffice {
|
|
15
|
-
/**
|
|
16
|
-
* Converts various document formats to PDF.
|
|
17
|
-
*
|
|
18
|
-
* @param {Object} options - Options for the conversion operation.
|
|
19
|
-
* @param {PathLikeOrReadStream[]} options.files - An array of PathLikes or ReadStreams to the files to be converted to PDF.
|
|
20
|
-
* @param {PageProperties} [options.properties] - Page properties for the conversion.
|
|
21
|
-
* @param {pdfa} [options.pdfFormat] - PDF format options.
|
|
22
|
-
* @param {boolean} [options.pdfUA] - Indicates whether to generate PDF/UA compliant output.
|
|
23
|
-
* @param {boolean} [options.merge] - Indicates whether to merge the resulting PDFs.
|
|
24
|
-
* @returns {Promise<Buffer>} A Promise resolving to the converted PDF content as a Buffer.
|
|
25
|
-
*/
|
|
26
|
-
public static async convert({
|
|
27
|
-
files,
|
|
28
|
-
properties,
|
|
29
|
-
pdfa,
|
|
30
|
-
pdfUA,
|
|
31
|
-
merge,
|
|
32
|
-
metadata
|
|
33
|
-
}: {
|
|
34
|
-
files: PathLikeOrReadStream[];
|
|
35
|
-
properties?: PageProperties;
|
|
36
|
-
pdfa?: PdfFormat;
|
|
37
|
-
pdfUA?: boolean;
|
|
38
|
-
merge?: boolean;
|
|
39
|
-
metadata?: Metadata;
|
|
40
|
-
}): Promise<Buffer> {
|
|
41
|
-
const data = new FormData();
|
|
42
|
-
|
|
43
|
-
await LibreOfficeUtils.addFiles(files, data);
|
|
44
|
-
await LibreOfficeUtils.customize(data, {
|
|
45
|
-
properties,
|
|
46
|
-
merge,
|
|
47
|
-
pdfa,
|
|
48
|
-
pdfUA,
|
|
49
|
-
metadata
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.LIBRE_OFFICE_PATH}/${Chromiumly.LIBRE_OFFICE_ROUTES.convert}`;
|
|
53
|
-
|
|
54
|
-
return GotenbergUtils.fetch(
|
|
55
|
-
endpoint,
|
|
56
|
-
data,
|
|
57
|
-
Chromiumly.GOTENBERG_API_BASIC_AUTH_USERNAME,
|
|
58
|
-
Chromiumly.GOTENBERG_API_BASIC_AUTH_PASSWORD
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { LibreOffice } from '../libre-office';
|
|
2
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3
|
-
import { createReadStream, promises } from 'fs';
|
|
4
|
-
import FormData from 'form-data';
|
|
5
|
-
import fetch from 'node-fetch';
|
|
6
|
-
import { PdfFormat } from '../../common';
|
|
7
|
-
|
|
8
|
-
const { Response } = jest.requireActual('node-fetch');
|
|
9
|
-
jest.mock('node-fetch', () => jest.fn());
|
|
10
|
-
|
|
11
|
-
describe('LibreOffice', () => {
|
|
12
|
-
const mockPromisesAccess = jest.spyOn(promises, 'access');
|
|
13
|
-
const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
|
|
14
|
-
const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
|
|
15
|
-
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
(createReadStream as jest.Mock) = jest
|
|
18
|
-
.fn()
|
|
19
|
-
.mockImplementation((file) => file);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
afterEach(() => {
|
|
23
|
-
jest.resetAllMocks();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe('convert', () => {
|
|
27
|
-
describe('when no properties are passed', () => {
|
|
28
|
-
it('should return a buffer', async () => {
|
|
29
|
-
mockPromisesAccess.mockResolvedValue();
|
|
30
|
-
mockFetch.mockResolvedValue(new Response('content'));
|
|
31
|
-
const buffer = await LibreOffice.convert({
|
|
32
|
-
files: ['path/to/file.docx', 'path/to/file.bib']
|
|
33
|
-
});
|
|
34
|
-
expect(buffer).toEqual(Buffer.from('content'));
|
|
35
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
describe('when properties are passed', () => {
|
|
40
|
-
it('should return a buffer', async () => {
|
|
41
|
-
mockPromisesAccess.mockResolvedValue();
|
|
42
|
-
mockFetch.mockResolvedValue(new Response('content'));
|
|
43
|
-
const buffer = await LibreOffice.convert({
|
|
44
|
-
files: ['path/to/file.docx', 'path/to/file.bib'],
|
|
45
|
-
properties: { landscape: true },
|
|
46
|
-
pdfa: PdfFormat.A_2b,
|
|
47
|
-
pdfUA: true,
|
|
48
|
-
metadata: { author: 'John Doe' },
|
|
49
|
-
merge: true
|
|
50
|
-
});
|
|
51
|
-
expect(buffer).toEqual(Buffer.from('content'));
|
|
52
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(7);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
});
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
export const LIBRE_OFFICE_EXTENSIONS = [
|
|
2
|
-
'123',
|
|
3
|
-
'602',
|
|
4
|
-
'abw',
|
|
5
|
-
'bib',
|
|
6
|
-
'bmp',
|
|
7
|
-
'cdr',
|
|
8
|
-
'cgm',
|
|
9
|
-
'cmx',
|
|
10
|
-
'csv',
|
|
11
|
-
'cwk',
|
|
12
|
-
'dbf',
|
|
13
|
-
'dif',
|
|
14
|
-
'doc',
|
|
15
|
-
'docm',
|
|
16
|
-
'docx',
|
|
17
|
-
'dot',
|
|
18
|
-
'dotm',
|
|
19
|
-
'dotx',
|
|
20
|
-
'dxf',
|
|
21
|
-
'emf',
|
|
22
|
-
'eps',
|
|
23
|
-
'epub',
|
|
24
|
-
'fodg',
|
|
25
|
-
'fodp',
|
|
26
|
-
'fods',
|
|
27
|
-
'fodt',
|
|
28
|
-
'fopd',
|
|
29
|
-
'gif',
|
|
30
|
-
'htm',
|
|
31
|
-
'html',
|
|
32
|
-
'hwp',
|
|
33
|
-
'jpeg',
|
|
34
|
-
'jpg',
|
|
35
|
-
'key',
|
|
36
|
-
'ltx',
|
|
37
|
-
'lwp',
|
|
38
|
-
'mcw',
|
|
39
|
-
'met',
|
|
40
|
-
'mml',
|
|
41
|
-
'mw',
|
|
42
|
-
'numbers',
|
|
43
|
-
'odd',
|
|
44
|
-
'odg',
|
|
45
|
-
'odm',
|
|
46
|
-
'odp',
|
|
47
|
-
'ods',
|
|
48
|
-
'odt',
|
|
49
|
-
'otg',
|
|
50
|
-
'oth',
|
|
51
|
-
'otp',
|
|
52
|
-
'ots',
|
|
53
|
-
'ott',
|
|
54
|
-
'pages',
|
|
55
|
-
'pbm',
|
|
56
|
-
'pcd',
|
|
57
|
-
'pct',
|
|
58
|
-
'pcx',
|
|
59
|
-
'pdb',
|
|
60
|
-
'pdf',
|
|
61
|
-
'pgm',
|
|
62
|
-
'png',
|
|
63
|
-
'pot',
|
|
64
|
-
'potm',
|
|
65
|
-
'potx',
|
|
66
|
-
'ppm',
|
|
67
|
-
'pps',
|
|
68
|
-
'ppt',
|
|
69
|
-
'pptm',
|
|
70
|
-
'pptx',
|
|
71
|
-
'psd',
|
|
72
|
-
'psw',
|
|
73
|
-
'pub',
|
|
74
|
-
'pwp',
|
|
75
|
-
'pxl',
|
|
76
|
-
'ras',
|
|
77
|
-
'rtf',
|
|
78
|
-
'sda',
|
|
79
|
-
'sdc',
|
|
80
|
-
'sdd',
|
|
81
|
-
'sdp',
|
|
82
|
-
'sdw',
|
|
83
|
-
'sgl',
|
|
84
|
-
'slk',
|
|
85
|
-
'smf',
|
|
86
|
-
'stc',
|
|
87
|
-
'std',
|
|
88
|
-
'sti',
|
|
89
|
-
'stw',
|
|
90
|
-
'svg',
|
|
91
|
-
'svm',
|
|
92
|
-
'swf',
|
|
93
|
-
'sxc',
|
|
94
|
-
'sxd',
|
|
95
|
-
'sxg',
|
|
96
|
-
'sxi',
|
|
97
|
-
'sxm',
|
|
98
|
-
'sxw',
|
|
99
|
-
'tga',
|
|
100
|
-
'tif',
|
|
101
|
-
'tiff',
|
|
102
|
-
'txt',
|
|
103
|
-
'uof',
|
|
104
|
-
'uop',
|
|
105
|
-
'uos',
|
|
106
|
-
'uot',
|
|
107
|
-
'vdx',
|
|
108
|
-
'vor',
|
|
109
|
-
'vsd',
|
|
110
|
-
'vsdm',
|
|
111
|
-
'vsdx',
|
|
112
|
-
'wb2',
|
|
113
|
-
'wk1',
|
|
114
|
-
'wks',
|
|
115
|
-
'wmf',
|
|
116
|
-
'wpd',
|
|
117
|
-
'wpg',
|
|
118
|
-
'wps',
|
|
119
|
-
'xbm',
|
|
120
|
-
'xhtml',
|
|
121
|
-
'xls',
|
|
122
|
-
'xlsb',
|
|
123
|
-
'xlsm',
|
|
124
|
-
'xlsx',
|
|
125
|
-
'xlt',
|
|
126
|
-
'xltm',
|
|
127
|
-
'xltx',
|
|
128
|
-
'xlw',
|
|
129
|
-
'xml',
|
|
130
|
-
'xpm',
|
|
131
|
-
'zabw'
|
|
132
|
-
];
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { constants, createReadStream, promises } from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
import FormData from 'form-data';
|
|
5
|
-
|
|
6
|
-
import { GotenbergUtils } from '../../common';
|
|
7
|
-
import { LIBRE_OFFICE_EXTENSIONS } from './constants';
|
|
8
|
-
import {
|
|
9
|
-
ConversionOptions,
|
|
10
|
-
PageProperties,
|
|
11
|
-
PathLikeOrReadStream
|
|
12
|
-
} from '../interfaces/libre-office.types';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Utility class for handling common tasks related to LibreOffice conversions.
|
|
16
|
-
*/
|
|
17
|
-
export class LibreOfficeUtils {
|
|
18
|
-
private static async getFileInfo(file: PathLikeOrReadStream) {
|
|
19
|
-
if (typeof file === 'string') {
|
|
20
|
-
await promises.access(file, constants.R_OK);
|
|
21
|
-
const filename = path.basename(path.parse(file).base);
|
|
22
|
-
return {
|
|
23
|
-
data: createReadStream(file),
|
|
24
|
-
ext: path.extname(filename).slice(1)
|
|
25
|
-
};
|
|
26
|
-
} else {
|
|
27
|
-
return { data: file.data, ext: file.ext };
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Adds files to the FormData object for LibreOffice conversion.
|
|
33
|
-
*
|
|
34
|
-
* @param {PathLikeOrReadStream[]} files - An array of files to be added to the FormData.
|
|
35
|
-
* @param {FormData} data - The FormData object to which files will be added.
|
|
36
|
-
* @throws {Error} Throws an error if the file extension is not supported.
|
|
37
|
-
*/
|
|
38
|
-
public static async addFiles(
|
|
39
|
-
files: PathLikeOrReadStream[],
|
|
40
|
-
data: FormData
|
|
41
|
-
) {
|
|
42
|
-
await Promise.all(
|
|
43
|
-
files.map(async (file, key) => {
|
|
44
|
-
const fileInfo = await this.getFileInfo(file);
|
|
45
|
-
if (!LIBRE_OFFICE_EXTENSIONS.includes(fileInfo.ext)) {
|
|
46
|
-
throw new Error(`${fileInfo.ext} is not supported`);
|
|
47
|
-
}
|
|
48
|
-
data.append('files', fileInfo.data, `${key}.${fileInfo.ext}`);
|
|
49
|
-
})
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Adds page properties to the FormData object based on the provided PageProperties.
|
|
55
|
-
*
|
|
56
|
-
* @param {FormData} data - The FormData object to which page properties will be added.
|
|
57
|
-
* @param {PageProperties} pageProperties - The page properties to be added to the FormData.
|
|
58
|
-
*/
|
|
59
|
-
public static addPageProperties(
|
|
60
|
-
data: FormData,
|
|
61
|
-
pageProperties: PageProperties
|
|
62
|
-
): void {
|
|
63
|
-
if (pageProperties.landscape) {
|
|
64
|
-
data.append('landscape', String(pageProperties.landscape));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (pageProperties.nativePageRanges) {
|
|
68
|
-
GotenbergUtils.assert(
|
|
69
|
-
pageProperties.nativePageRanges.from > 0 &&
|
|
70
|
-
pageProperties.nativePageRanges.to > 0 &&
|
|
71
|
-
pageProperties.nativePageRanges.to >=
|
|
72
|
-
pageProperties.nativePageRanges.from,
|
|
73
|
-
'page ranges syntax error'
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
data.append(
|
|
77
|
-
'nativePageRanges',
|
|
78
|
-
`${pageProperties.nativePageRanges.from}-${pageProperties.nativePageRanges.to}`
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (pageProperties.exportFormFields === false) {
|
|
83
|
-
data.append(
|
|
84
|
-
'exportFormFields',
|
|
85
|
-
String(pageProperties.exportFormFields)
|
|
86
|
-
);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (pageProperties.singlePageSheets) {
|
|
90
|
-
data.append(
|
|
91
|
-
'singlePageSheets',
|
|
92
|
-
String(pageProperties.singlePageSheets)
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Customizes the FormData object based on the provided conversion options.
|
|
99
|
-
*
|
|
100
|
-
* @param {FormData} data - The FormData object to be customized.
|
|
101
|
-
* @param {ConversionOptions} options - The conversion options to apply to the FormData.
|
|
102
|
-
* @returns {Promise<void>} A Promise that resolves once the customization is complete.
|
|
103
|
-
*/
|
|
104
|
-
public static async customize(
|
|
105
|
-
data: FormData,
|
|
106
|
-
options: ConversionOptions
|
|
107
|
-
): Promise<void> {
|
|
108
|
-
if (options.pdfa) {
|
|
109
|
-
data.append('pdfa', options.pdfa);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (options.pdfUA) {
|
|
113
|
-
data.append('pdfUA', String(options.pdfUA));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (options.merge) {
|
|
117
|
-
data.append('merge', String(options.merge));
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (options.metadata) {
|
|
121
|
-
data.append('metadata', JSON.stringify(options.metadata));
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (options.properties) {
|
|
125
|
-
LibreOfficeUtils.addPageProperties(data, options.properties);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
|
-
import { promises, createReadStream } from 'fs';
|
|
3
|
-
import { LibreOfficeUtils } from '../libre-office.utils';
|
|
4
|
-
|
|
5
|
-
import FormData from 'form-data';
|
|
6
|
-
|
|
7
|
-
describe('LibreOfficeUtils', () => {
|
|
8
|
-
const mockPromisesAccess = jest.spyOn(promises, 'access');
|
|
9
|
-
const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
|
|
10
|
-
|
|
11
|
-
const data = new FormData();
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
(createReadStream as jest.Mock) = jest
|
|
15
|
-
.fn()
|
|
16
|
-
.mockImplementation((file) => file);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
afterEach(() => {
|
|
20
|
-
jest.resetAllMocks();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
describe('addFiles', () => {
|
|
24
|
-
describe('when files exist', () => {
|
|
25
|
-
describe('when files parameter contains paths', () => {
|
|
26
|
-
it('should append each file to data', async () => {
|
|
27
|
-
mockPromisesAccess.mockResolvedValue();
|
|
28
|
-
await LibreOfficeUtils.addFiles(
|
|
29
|
-
['path/to/file.docx', 'path/to/file.bib'],
|
|
30
|
-
data
|
|
31
|
-
);
|
|
32
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
describe('when files parameter contains a buffer', () => {
|
|
36
|
-
it('should append each file to data', async () => {
|
|
37
|
-
mockPromisesAccess.mockResolvedValueOnce();
|
|
38
|
-
await LibreOfficeUtils.addFiles(
|
|
39
|
-
[
|
|
40
|
-
{ data: Buffer.from('data'), ext: 'csv' },
|
|
41
|
-
'path/to/file.bib'
|
|
42
|
-
],
|
|
43
|
-
data
|
|
44
|
-
);
|
|
45
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe('when one of the files has undetermined format', () => {
|
|
51
|
-
it('should throw an error', async () => {
|
|
52
|
-
mockPromisesAccess.mockResolvedValueOnce();
|
|
53
|
-
|
|
54
|
-
await expect(
|
|
55
|
-
LibreOfficeUtils.addFiles(
|
|
56
|
-
[
|
|
57
|
-
{ data: Buffer.from('data'), ext: 'docx' },
|
|
58
|
-
'path/to/file.none'
|
|
59
|
-
],
|
|
60
|
-
data
|
|
61
|
-
)
|
|
62
|
-
).rejects.toThrow('none is not supported');
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
describe('when one of the files has unsupported format', () => {
|
|
67
|
-
it('should throw an error', async () => {
|
|
68
|
-
mockPromisesAccess.mockResolvedValueOnce();
|
|
69
|
-
await expect(() =>
|
|
70
|
-
LibreOfficeUtils.addFiles(
|
|
71
|
-
['path/to/file.rar', 'path/to/file.pdf'],
|
|
72
|
-
data
|
|
73
|
-
)
|
|
74
|
-
).rejects.toThrow('rar is not supported');
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
describe('when one of the files does not exist', () => {
|
|
79
|
-
it('should throw an error', async () => {
|
|
80
|
-
const errorMessage =
|
|
81
|
-
"ENOENT: no such file or directory, access 'path/to/index.html'";
|
|
82
|
-
mockPromisesAccess.mockRejectedValueOnce(
|
|
83
|
-
new Error(errorMessage)
|
|
84
|
-
);
|
|
85
|
-
await expect(() =>
|
|
86
|
-
LibreOfficeUtils.addFiles(
|
|
87
|
-
['path/to/file.pdf', 'path/to/another-file.pdf'],
|
|
88
|
-
data
|
|
89
|
-
)
|
|
90
|
-
).rejects.toThrow(errorMessage);
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
describe('addPageProperties', () => {
|
|
96
|
-
describe('Page landscape', () => {
|
|
97
|
-
describe('when landscape parameter is set', () => {
|
|
98
|
-
it('should append landscape to data', () => {
|
|
99
|
-
LibreOfficeUtils.addPageProperties(data, {
|
|
100
|
-
landscape: true
|
|
101
|
-
});
|
|
102
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(1);
|
|
103
|
-
expect(data.append).toHaveBeenCalledWith(
|
|
104
|
-
'landscape',
|
|
105
|
-
'true'
|
|
106
|
-
);
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('Export form fields', () => {
|
|
112
|
-
describe('when exportFormFields parameter is set', () => {
|
|
113
|
-
it('should append exportFormFields to data', () => {
|
|
114
|
-
LibreOfficeUtils.addPageProperties(data, {
|
|
115
|
-
exportFormFields: false
|
|
116
|
-
});
|
|
117
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(1);
|
|
118
|
-
expect(data.append).toHaveBeenCalledWith(
|
|
119
|
-
'exportFormFields',
|
|
120
|
-
'false'
|
|
121
|
-
);
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe('Page ranges', () => {
|
|
127
|
-
describe('when nativePageRanges is valid', () => {
|
|
128
|
-
it('should append nativePageRanges to data', () => {
|
|
129
|
-
LibreOfficeUtils.addPageProperties(data, {
|
|
130
|
-
nativePageRanges: { from: 1, to: 6 }
|
|
131
|
-
});
|
|
132
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(1);
|
|
133
|
-
expect(data.append).toHaveBeenCalledWith(
|
|
134
|
-
'nativePageRanges',
|
|
135
|
-
'1-6'
|
|
136
|
-
);
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
describe('Single page sheets', () => {
|
|
142
|
-
describe('when singlePageSheets parameter is set', () => {
|
|
143
|
-
it('should append singlePageSheets to data', () => {
|
|
144
|
-
LibreOfficeUtils.addPageProperties(data, {
|
|
145
|
-
singlePageSheets: true
|
|
146
|
-
});
|
|
147
|
-
expect(mockFormDataAppend).toHaveBeenCalledTimes(1);
|
|
148
|
-
expect(data.append).toHaveBeenCalledWith(
|
|
149
|
-
'singlePageSheets',
|
|
150
|
-
'true'
|
|
151
|
-
);
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
});
|