Skip to content

Commit 99c1fcd

Browse files
committed
feat: code revamp and optimization.
1 parent b5c23ec commit 99c1fcd

25 files changed

+490
-348
lines changed

dist/Exception/OperatingSystemException.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class OperatingSystemException extends Error {
77
}
88
static isNotSupported() {
99
return new OperatingSystemException('The current operating system is not supported. ' +
10-
'Please use only WIN, Mac OS X or Linux to use this package.');
10+
'Please use only WIN, Mac OS X, UNIX or Linux to use this package.');
1111
}
1212
}
1313
exports.OperatingSystemException = OperatingSystemException;

dist/FFMPEGCompression.d.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import { ImageSqueezerCommon } from './ImageSqueezerCommon';
2-
export default class FFMPEGCompression extends ImageSqueezerCommon {
3-
static readonly WINDOWS_OS: string;
4-
static readonly LINUX_OS: string;
5-
static readonly UNIX_OS: string;
6-
static readonly MACOSX_OS: string;
7-
private operatingSystem;
2+
export declare class FFMPEGCompression extends ImageSqueezerCommon {
83
constructor();
9-
setOperatingSystem(operatingSystem: string): void;
10-
load(): void;
11-
verifySupportedOperatingSystem(): void;
12-
private getOperatingSystem;
13-
compress(): Promise<boolean>;
4+
protected command(): string;
145
}

dist/FFMPEGCompression.js

+6-42
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
return (mod && mod.__esModule) ? mod : { "default": mod };
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
6-
const child_process_1 = __importDefault(require("child_process"));
76
const image_size_1 = __importDefault(require("image-size"));
87
const ImageSqueezerCommon_1 = require("./ImageSqueezerCommon");
9-
const OperatingSystemException_1 = require("./Exception/OperatingSystemException");
108
class FFMPEGCompression extends ImageSqueezerCommon_1.ImageSqueezerCommon {
119
constructor() {
1210
super();
13-
this.operatingSystem = '';
1411
this.setSubClassType('ffmpeg-compression');
12+
this.setBin('ffmpeg');
1513
}
16-
setOperatingSystem(operatingSystem) {
17-
this.operatingSystem = operatingSystem;
18-
}
19-
load() {
20-
this.verifySupportedOperatingSystem();
21-
}
22-
verifySupportedOperatingSystem() {
23-
let selectedOperatingSystem = this.getOperatingSystem();
24-
switch (selectedOperatingSystem) {
25-
case FFMPEGCompression.WINDOWS_OS:
26-
case FFMPEGCompression.LINUX_OS:
27-
break;
28-
case FFMPEGCompression.UNIX_OS:
29-
case FFMPEGCompression.MACOSX_OS:
30-
default:
31-
throw OperatingSystemException_1.OperatingSystemException.isNotSupported();
32-
}
33-
}
34-
getOperatingSystem() {
35-
if (this.operatingSystem) {
36-
return this.operatingSystem;
37-
}
38-
return process.platform;
39-
}
40-
compress() {
41-
this.transferSouceFilePathToOutputFilePath();
42-
this.validateRequiredProperties();
14+
command() {
4315
let imageDimensions = image_size_1.default(this.sourceFilePath);
4416
let cmd = this.bin + ' -y -i ' +
4517
this.escapeShellArg(this.sourceFilePath) +
4618
' -vf scale=w=' + imageDimensions.width +
4719
':h=' + imageDimensions.height +
48-
':force_original_aspect_ratio=decrease:interl=1 ';
49-
cmd += this.handleOutputFilePath();
50-
return (new Promise((resolve, reject) => {
51-
child_process_1.default.exec(cmd, (error) => {
52-
(error ? reject(error) : resolve(true));
53-
});
54-
}));
20+
':force_original_aspect_ratio=decrease:interl=1 ' +
21+
this.handleOutputFilePath();
22+
return cmd;
5523
}
5624
}
57-
FFMPEGCompression.WINDOWS_OS = 'win32';
58-
FFMPEGCompression.LINUX_OS = 'linux';
59-
FFMPEGCompression.UNIX_OS = 'freebsd';
60-
FFMPEGCompression.MACOSX_OS = 'darwin';
61-
exports.default = FFMPEGCompression;
25+
exports.FFMPEGCompression = FFMPEGCompression;

dist/ImageSqueezer.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import ProgressiveJPEG from './ProgressiveJPEG';
2-
import FFMPEGCompression from './FFMPEGCompression';
1+
import { ProgressiveJPEG } from './ProgressiveJPEG';
2+
import { FFMPEGCompression } from './FFMPEGCompression';
33
declare const _default: {
44
FFMPegCompression: typeof FFMPEGCompression;
55
ProgressiveJPEG: typeof ProgressiveJPEG;

dist/ImageSqueezer.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
"use strict";
2-
var __importDefault = (this && this.__importDefault) || function (mod) {
3-
return (mod && mod.__esModule) ? mod : { "default": mod };
4-
};
52
Object.defineProperty(exports, "__esModule", { value: true });
6-
const ProgressiveJPEG_1 = __importDefault(require("./ProgressiveJPEG"));
7-
const FFMPEGCompression_1 = __importDefault(require("./FFMPEGCompression"));
3+
const ProgressiveJPEG_1 = require("./ProgressiveJPEG");
4+
const FFMPEGCompression_1 = require("./FFMPEGCompression");
85
exports.default = {
9-
FFMPegCompression: FFMPEGCompression_1.default,
10-
ProgressiveJPEG: ProgressiveJPEG_1.default
6+
FFMPegCompression: FFMPEGCompression_1.FFMPEGCompression,
7+
ProgressiveJPEG: ProgressiveJPEG_1.ProgressiveJPEG
118
};

dist/ImageSqueezerCommon.d.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
export declare class ImageSqueezerCommon {
2+
static readonly WINDOWS_OS: string;
3+
static readonly LINUX_OS: string;
4+
static readonly UNIX_OS: string;
5+
static readonly MACOSX_OS: string;
6+
static readonly DISABLED_CHILD_PROC_MSG: string;
7+
protected operatingSystem: string;
28
protected subClassType: string;
39
protected bin: string;
410
protected sourceFilePath: string;
511
protected outputFilePath: string;
612
protected isAllowedEmptyOutputFilePath: boolean;
13+
protected commandStatement: string;
14+
protected isExecuteChildProcess: boolean;
15+
load(): void;
16+
verifySupportedOperatingSystem(): void;
17+
setOperatingSystem(operatingSystem: string): void;
18+
protected getOperatingSystem(): string;
719
protected setSubClassType(subClassType: string): void;
820
setBin(bin: string): void;
921
setSourceFilePath(sourceFilePath: string): void;
1022
setOutputFilePath(outputFilePath: string): void;
1123
allowEmptyOutputFilePath(): void;
12-
protected validateRequiredProperties(): void;
24+
protected setCommandStatement(commandStatement: string): void;
25+
getCommandStatement(): string;
26+
disableChildProcessExecution(): void;
27+
build(): void;
28+
compress(): Promise<boolean>;
1329
protected transferSouceFilePathToOutputFilePath(): void;
30+
protected validateRequiredProperties(): void;
1431
protected handleOutputFilePath(): string;
1532
protected generateTemporaryOutputFilePath(): string;
33+
private renameCommandWithCompatibilityChecking;
1634
protected escapeShellArg(arg: string): string;
35+
protected executeChildProcess(): Promise<boolean>;
36+
/**
37+
* This is a abstract or no-op class method.
38+
* The subclass is expected to override this method.
39+
*/
40+
protected command(): string;
1741
}

dist/ImageSqueezerCommon.js

+86-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,42 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
66
const path_1 = __importDefault(require("path"));
7+
const child_process_1 = __importDefault(require("child_process"));
8+
const OperatingSystemException_1 = require("./Exception/OperatingSystemException");
79
const ImageSqueezerCommonException_1 = require("./Exception/ImageSqueezerCommonException");
810
class ImageSqueezerCommon {
911
constructor() {
12+
this.operatingSystem = '';
1013
this.subClassType = '';
1114
this.bin = '';
1215
this.sourceFilePath = '';
1316
this.outputFilePath = '';
1417
this.isAllowedEmptyOutputFilePath = false;
18+
this.commandStatement = '';
19+
this.isExecuteChildProcess = true;
20+
}
21+
load() {
22+
this.verifySupportedOperatingSystem();
23+
}
24+
verifySupportedOperatingSystem() {
25+
switch (this.getOperatingSystem()) {
26+
case ImageSqueezerCommon.WINDOWS_OS:
27+
case ImageSqueezerCommon.LINUX_OS:
28+
case ImageSqueezerCommon.UNIX_OS:
29+
break;
30+
case ImageSqueezerCommon.MACOSX_OS:
31+
default:
32+
throw OperatingSystemException_1.OperatingSystemException.isNotSupported();
33+
}
34+
}
35+
setOperatingSystem(operatingSystem) {
36+
this.operatingSystem = operatingSystem;
37+
}
38+
getOperatingSystem() {
39+
if (this.operatingSystem) {
40+
return this.operatingSystem;
41+
}
42+
return process.platform;
1543
}
1644
setSubClassType(subClassType) {
1745
this.subClassType = subClassType;
@@ -28,6 +56,28 @@ class ImageSqueezerCommon {
2856
allowEmptyOutputFilePath() {
2957
this.isAllowedEmptyOutputFilePath = true;
3058
}
59+
setCommandStatement(commandStatement) {
60+
this.commandStatement = commandStatement;
61+
}
62+
getCommandStatement() {
63+
return this.commandStatement;
64+
}
65+
disableChildProcessExecution() {
66+
this.isExecuteChildProcess = false;
67+
}
68+
build() {
69+
this.transferSouceFilePathToOutputFilePath();
70+
this.validateRequiredProperties();
71+
this.setCommandStatement(this.command());
72+
}
73+
compress() {
74+
return this.executeChildProcess();
75+
}
76+
transferSouceFilePathToOutputFilePath() {
77+
if (this.isAllowedEmptyOutputFilePath) {
78+
this.outputFilePath = this.sourceFilePath;
79+
}
80+
}
3181
validateRequiredProperties() {
3282
if (!this.sourceFilePath) {
3383
throw ImageSqueezerCommonException_1.ImageSqueezerCommonException.emptySourceFilePath();
@@ -36,11 +86,6 @@ class ImageSqueezerCommon {
3686
throw ImageSqueezerCommonException_1.ImageSqueezerCommonException.emptyOutputFilePath();
3787
}
3888
}
39-
transferSouceFilePathToOutputFilePath() {
40-
if (this.isAllowedEmptyOutputFilePath) {
41-
this.outputFilePath = this.sourceFilePath;
42-
}
43-
}
4489
handleOutputFilePath() {
4590
if (this.isAllowedEmptyOutputFilePath) {
4691
return this.generateTemporaryOutputFilePath();
@@ -54,11 +99,45 @@ class ImageSqueezerCommon {
5499
let splittedFilename = filename.split('.');
55100
let newFilename = splittedFilename[0] + '-compressed-' + this.subClassType + '.' + splittedFilename[1];
56101
let newBasename = this.escapeShellArg(this.outputFilePath.replace(filename, newFilename));
57-
return newBasename + ' && mv ' +
58-
newBasename + ' ' + this.escapeShellArg(this.outputFilePath);
102+
return newBasename + this.renameCommandWithCompatibilityChecking(newBasename);
103+
}
104+
renameCommandWithCompatibilityChecking(newBasename) {
105+
let cmd = '';
106+
switch (this.getOperatingSystem()) {
107+
case ImageSqueezerCommon.WINDOWS_OS:
108+
cmd = ' && move -y ' + newBasename + ' ' + this.escapeShellArg(this.outputFilePath);
109+
break;
110+
case ImageSqueezerCommon.LINUX_OS:
111+
case ImageSqueezerCommon.UNIX_OS:
112+
cmd = ' && mv ' + newBasename + ' ' + this.escapeShellArg(this.outputFilePath);
113+
break;
114+
}
115+
return cmd;
59116
}
60117
escapeShellArg(arg) {
61118
return `'${arg.replace(/'/g, `'\\''`)}'`;
62119
}
120+
executeChildProcess() {
121+
if (!this.isExecuteChildProcess) {
122+
return Promise.reject(ImageSqueezerCommon.DISABLED_CHILD_PROC_MSG);
123+
}
124+
return new Promise((resolve, reject) => {
125+
child_process_1.default.exec(this.commandStatement, (error) => {
126+
(error ? reject(error) : resolve(true));
127+
});
128+
});
129+
}
130+
/**
131+
* This is a abstract or no-op class method.
132+
* The subclass is expected to override this method.
133+
*/
134+
command() {
135+
return '';
136+
}
63137
}
138+
ImageSqueezerCommon.WINDOWS_OS = 'win32';
139+
ImageSqueezerCommon.LINUX_OS = 'linux';
140+
ImageSqueezerCommon.UNIX_OS = 'freebsd';
141+
ImageSqueezerCommon.MACOSX_OS = 'darwin';
142+
ImageSqueezerCommon.DISABLED_CHILD_PROC_MSG = 'The Child Process Execution was disabled.';
64143
exports.ImageSqueezerCommon = ImageSqueezerCommon;

dist/ProgressiveJPEG.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ImageSqueezerCommon } from './ImageSqueezerCommon';
2-
export default class ProgressiveJPEG extends ImageSqueezerCommon {
2+
export declare class ProgressiveJPEG extends ImageSqueezerCommon {
33
constructor();
4-
compress(): Promise<boolean>;
4+
protected command(): string;
55
}

dist/ProgressiveJPEG.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
"use strict";
2-
var __importDefault = (this && this.__importDefault) || function (mod) {
3-
return (mod && mod.__esModule) ? mod : { "default": mod };
4-
};
52
Object.defineProperty(exports, "__esModule", { value: true });
6-
const child_process_1 = __importDefault(require("child_process"));
73
const ImageSqueezerCommon_1 = require("./ImageSqueezerCommon");
84
class ProgressiveJPEG extends ImageSqueezerCommon_1.ImageSqueezerCommon {
95
constructor() {
106
super();
117
this.setSubClassType('progessive-jpeg');
128
this.setBin('convert');
139
}
14-
compress() {
15-
this.transferSouceFilePathToOutputFilePath();
16-
this.validateRequiredProperties();
10+
command() {
1711
let cmd = this.bin + ' ' +
18-
this.sourceFilePath + ' -interlace plane ';
19-
cmd += this.handleOutputFilePath();
20-
return new Promise((resolve, reject) => {
21-
child_process_1.default.exec(cmd, (error) => {
22-
(error ? reject(error) : resolve(true));
23-
});
24-
});
12+
this.sourceFilePath + ' -interlace plane ' +
13+
this.handleOutputFilePath();
14+
return cmd;
2515
}
2616
}
27-
exports.default = ProgressiveJPEG;
17+
exports.ProgressiveJPEG = ProgressiveJPEG;

src/Exception/ImageSqueezerCommonException.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ export class ImageSqueezerCommonException extends Error {
33
constructor(message: string) {
44

55
super(message);
6-
76
this.name = 'ImageSqueezerCommonException';
87
}
98

src/Exception/OperatingSystemException.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ export class OperatingSystemException extends Error {
33
constructor(message: string) {
44

55
super(message);
6-
76
this.name = 'OperatingSystemException';
87
}
98

109
public static isNotSupported(): OperatingSystemException {
1110

1211
return new OperatingSystemException(
1312
'The current operating system is not supported. ' +
14-
'Please use only WIN, Mac OS X or Linux to use this package.'
13+
'Please use only WIN, Mac OS X, UNIX or Linux to use this package.'
1514
);
1615
}
1716
}

0 commit comments

Comments
 (0)