Skip to content

Allow custom LibreOffice binary path via environment variable #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.vscode/
.serverless/
coverage/
lib/
# lib/
node_modules/

*.log
Expand Down
1 change: 1 addition & 0 deletions lib/cleanup.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function cleanupTempFiles(): Promise<void>;
23 changes: 23 additions & 0 deletions lib/cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cleanupTempFiles = cleanupTempFiles;
var _promises = _interopRequireDefault(require("fs/promises"));
var _del = _interopRequireDefault(require("del"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// Removes temp files generated by LibreOffice
async function cleanupTempFiles() {
const files = await _promises.default.readdir(`/tmp`);
for (const file of files) {
if (file.endsWith('.tmp') === true || file.startsWith('OSL_PIPE')) {
try {
await (0, _del.default)([`/tmp/${file}`, `/tmp/${file}/*`], {
force: true
});
// eslint-disable-next-line no-empty
} catch (error) {}
}
}
}
2 changes: 2 additions & 0 deletions lib/convert.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare const DEFAULT_ARGS: string[];
export declare function convertTo(filename: string, format: string): Promise<string>;
33 changes: 33 additions & 0 deletions lib/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DEFAULT_ARGS = void 0;
exports.convertTo = convertTo;
var _child_process = _interopRequireDefault(require("child_process"));
var _util = _interopRequireDefault(require("util"));
var _cleanup = require("./cleanup");
var _logs = require("./logs");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const exec = _util.default.promisify(_child_process.default.exec);
const DEFAULT_ARGS = ['--headless', '--invisible', '--nodefault', '--view', '--nolockcheck', '--nologo', '--norestore'];
exports.DEFAULT_ARGS = DEFAULT_ARGS;
const LO_BINARY_PATH = process.env.LO_BINARY_PATH ?? 'libreoffice7.6';
async function convertTo(filename, format) {
await (0, _cleanup.cleanupTempFiles)();
const argumentsString = DEFAULT_ARGS.join(' ');
const outputFilename = filename.split(/\\ /).join(' ');
const cmd = `cd /tmp && ${LO_BINARY_PATH} ${argumentsString} --convert-to ${format} --outdir /tmp '/tmp/${outputFilename}'`;
let logs;

// due to an unknown issue, we need to run command twice
try {
logs = (await exec(cmd)).stdout;
} catch (e) {
logs = (await exec(cmd)).stdout;
}
await exec(`rm '/tmp/${outputFilename}'`);
await (0, _cleanup.cleanupTempFiles)();
return (0, _logs.getConvertedFilePath)(logs.toString());
}
2 changes: 2 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './convert';
export * from './validations';
27 changes: 27 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
var _convert = require("./convert");
Object.keys(_convert).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _convert[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _convert[key];
}
});
});
var _validations = require("./validations");
Object.keys(_validations).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _validations[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _validations[key];
}
});
});
1 change: 1 addition & 0 deletions lib/logs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function getConvertedFilePath(logs: string): string;
15 changes: 15 additions & 0 deletions lib/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getConvertedFilePath = getConvertedFilePath;
function getConvertedFilePath(logs) {
try {
return logs.match(/\/tmp\/.+->\s(\/tmp\/.+) using/)[1];
} catch (e) {
const ErrorWithExtendedMessage = new Error(e);
ErrorWithExtendedMessage.message += `;\tTried to parse string: "${logs}"`;
throw ErrorWithExtendedMessage;
}
}
1 change: 1 addition & 0 deletions lib/validations.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function canBeConvertedToPDF(filename: string): boolean;
19 changes: 19 additions & 0 deletions lib/validations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.canBeConvertedToPDF = canBeConvertedToPDF;
var _isVideo = _interopRequireDefault(require("is-video"));
var _isImage = _interopRequireDefault(require("is-image"));
var _isAudioFilepath = _interopRequireDefault(require("@shelf/is-audio-filepath"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const UNSUPPORTED_FILE_EXTENSIONS = ['.chm', '.heic', '.gdoc', '.gsheet', '.gslides', '.zip', '.dwg'];
function canBeConvertedToPDF(filename) {
filename = filename.toLowerCase();
const isFileExtensionUnsupported = UNSUPPORTED_FILE_EXTENSIONS.some(ext => filename.endsWith(ext));
if (isFileExtensionUnsupported) {
return false;
}
return !(0, _isImage.default)(filename) && !(0, _isVideo.default)(filename) && !(0, _isAudioFilepath.default)(filename);
}
2 changes: 1 addition & 1 deletion src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const DEFAULT_ARGS = [
'--nologo',
'--norestore',
];
const LO_BINARY_PATH = 'libreoffice7.6';
const LO_BINARY_PATH = process.env.LO_BINARY_PATH ?? 'libreoffice7.6';

export async function convertTo(filename: string, format: string): Promise<string> {
await cleanupTempFiles();
Expand Down