-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (154 loc) · 5.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const path = require('path')
const fs = require('fs')
const { promisify } = require('util')
const heicConvert = require('heic-convert')
const ProgressBar = require('progress')
class Convert {
/**
* @param {String} file
* @description remove file extension
* @return {String} file without extension
* @example
* const file = './directory/file.exe'
* const noExt = this.removeExtension(file)
* // ./directory/file
*/
#removeExtension = file => {
const lastDotPosition = file.lastIndexOf('.')
if (lastDotPosition === -1) return file
else return file.substr(0, lastDotPosition)
}
/**
* @param {String} filePath path to your file
* @description convert .heic to .jpg file with progress bar in terminal
* @return {Promise<String>} Promise with file name string
* @error returns [error, fileName]
* @example
* const convert = new Convert()
* convert.cli('someFile.HEIC')
*/
cli = async filePath =>
new Promise(async (resolve, reject) => {
let fileName
let progressBar
try {
// if (path.parse(filePath).ext.toLocaleLowerCase() != '.heic')
// throw err()
fileName = path.parse(filePath).name
progressBar = new ProgressBar(`${fileName} |:bar| :percent :elapseds`, {
complete: '█',
incomplete: ' ',
width: 20,
total: 5,
clear: true,
})
progressBar.tick()
const filePathNoExt = this.#removeExtension(filePath)
progressBar.tick()
const inputBuffer = await promisify(fs.readFile)(filePath)
progressBar.tick()
const outputBuffer = await heicConvert({
buffer: inputBuffer,
format: 'JPEG',
})
progressBar.tick()
await promisify(fs.writeFile)(`${filePathNoExt}.jpg`, outputBuffer)
progressBar.tick()
resolve(fileName)
} catch (err) {
progressBar.terminate()
reject([err, fileName])
}
})
/**
* @param {String} filePath path to your file
* @description convert .heic to .jpg file
* @return {Promise<void>} Promise with file name string
* @example
* const convert = new Convert()
* convert.fileToFile('someFile.HEIC')
*/
fileToFile = async filePath =>
new Promise(async (resolve, reject) => {
try {
const fileName = path.parse(filePath).name
const filePathNoExt = this.#removeExtension(filePath)
const inputBuffer = await promisify(fs.readFile)(filePath)
const outputBuffer = await heicConvert({
buffer: inputBuffer,
format: 'JPEG',
})
await promisify(fs.writeFile)(`${filePathNoExt}.jpg`, outputBuffer)
resolve(fileName)
} catch (err) {
reject(err)
}
})
/**
* @param {String} filePath path to your file
* @description convert .heic to .jpg file
* @return {Promise<Buffer>} Promise with Buffer value
* @example
* const convert = new Convert()
* let buffer = convert.fileToBuffer('someFile.HEIC')
*/
fileToBuffer = async filePath =>
new Promise(async (resolve, reject) => {
try {
const inputBuffer = await promisify(fs.readFile)(filePath)
const outputBuffer = await heicConvert({
buffer: inputBuffer,
format: 'JPEG',
})
resolve(outputBuffer)
} catch (err) {
reject(err)
}
})
/**
* @param {Buffer} buffer file buffer
* @description convert .heic to .jpg file
* @return {Promise<Buffer>} Promise with Buffer value
* @example
* let someBuffer = ...
* const convert = new Convert()
* let buffer = convert.bufferToBuffer(someBuffer)
*/
bufferToBuffer = async buffer =>
new Promise(async (resolve, reject) => {
try {
const outputBuffer = await heicConvert({
buffer: buffer,
format: 'JPEG',
})
resolve(outputBuffer)
} catch (err) {
reject(err)
}
})
/**
* @param {Buffer} buffer
* @param {String} filePath
* @description convert .heic to .jpg file
* @return {Promise<void>} Promise with file name string
* @example
* let someBuffer = ...
* const convert = new Convert()
* convert.bufferToFile(someBuffer)
*/
bufferToFile = async (buffer, filePath) =>
new Promise(async (resolve, reject) => {
try {
const fileName = path.parse(filePath).name
const outputBuffer = await heicConvert({
buffer: buffer,
format: 'JPEG',
})
await promisify(fs.writeFile)(`${filePath}.jpg`, outputBuffer)
resolve(fileName)
} catch (err) {
reject(err)
}
})
}
module.exports = Convert