-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
177 lines (158 loc) · 5.19 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
170
171
172
173
174
175
176
177
const sharp = require('sharp')
const glob = require('glob')
const chokidar = require('chokidar')
const {
existsSync,
ensureDir,
readFileSync,
writeFileSync,
move
} = require('fs-extra')
const path = require('path')
const fs = require('fs')
require('dotenv').config()
const SOURCE_DIR = process.env.SOURCE_DIR || 'example/'
const OUTPUT_DIR = process.env.OUTPUT_DIR || 'example-optimized/'
const TEMP_DIR = process.env.TEMP_DIR || 'tmp/'
const MANIFEST_FILE = process.env.MANIFEST_FILE || 'manifest.json'
const GLOB_PATTERN = process.env.GLOB_PATTERN || '**/*'
const ENABLE_WATCHER = typeof(process.env.ENABLE_WATCHER) !== 'undefined' ? JSON.parse(process.env.ENABLE_WATCHER) : true
const ENABLE_PROCESS = typeof(process.env.ENABLE_PROCESS) !== 'undefined' ? JSON.parse(process.env.ENABLE_PROCESS) : true
const SKIP_PROCESSED_FILES = typeof(process.env.SKIP_PROCESSED_FILES) !== 'undefined' ? JSON.parse(process.env.SKIP_PROCESSED_FILES) : true
const SKIP_NON_ACTIVESTORAGE_FILES = typeof(process.env.SKIP_NON_ACTIVESTORAGE_FILES) !== 'undefined' ? JSON.parse(process.env.SKIP_NON_ACTIVESTORAGE_FILES) : false
const PROCESS_NUM_FILES = typeof(process.env.PROCESS_NUM_FILES) !== 'undefined' ? JSON.parse(process.env.PROCESS_NUM_FILES) : undefined
console.log('config:')
console.log('\tSOURCE_DIR =', SOURCE_DIR)
console.log('\tOUTPUT_DIR =', OUTPUT_DIR)
console.log('\tTEMP_DIR =', TEMP_DIR)
console.log('\tMANIFEST_FILE =', MANIFEST_FILE)
console.log('\tGLOB_PATTERN =', GLOB_PATTERN)
console.log('\tENABLE_WATCHER =', ENABLE_WATCHER)
console.log('\tENABLE_PROCESS =', ENABLE_PROCESS)
console.log('\tSKIP_PROCESSED_FILES =', SKIP_PROCESSED_FILES)
console.log('\tSKIP_NON_ACTIVESTORAGE_FILES =', SKIP_NON_ACTIVESTORAGE_FILES)
console.log('\tPROCESS_NUM_FILES =', PROCESS_NUM_FILES)
function humanSize(size) {
let i = Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];
}
function getFileSize(filePath) {
const stat = fs.statSync(filePath)
const {size} = stat
return humanSize(size)
}
function isDirectory(filePath) {
try {
const stat = fs.lstatSync(filePath)
return stat.isDirectory()
} catch (e) {
return false
}
}
async function addToManifest(manifest, filePath, err, info) {
if (!manifest[filePath]) {
manifest[filePath] = {}
}
if (!err) {
manifest[filePath] = {
width: info.width,
height: info.height,
size: humanSize(info.size)
}
} else {
manifest[filePath] = {
error: err
}
}
}
function removeFromManifest(manifest, filePath) {
if (manifest[filePath]) {
delete manifest[filePath]
}
}
function loadManifest() {
try {
return JSON.parse(readFileSync(MANIFEST_FILE, 'utf8'))
} catch(err) {
return {}
}
}
function saveManifest(manifest) {
writeFileSync(MANIFEST_FILE, JSON.stringify(manifest, null, 2))
}
async function resizeImage(filePath) {
const fileName = path.basename(filePath)
const tmpPath = path.join(TEMP_DIR, fileName)
const outPath = filePath.replace(SOURCE_DIR, OUTPUT_DIR)
return await new Promise((resolve, reject) => {
console.log('processing:', filePath)
sharp(filePath)
.resize(4096, 4096, {
withoutEnlargement: true,
fit: 'inside'
})
// .cache(false)
.jpeg({
// quality: 90,
mozjpeg: true
})
.toFile(tmpPath, async (err, info) => {
// console.log('\tinfo =', info)
if (!err) {
console.log('\tformat =', info.format)
console.log('\tsize =', info.width, 'x', info.height)
console.log('\tsrc =', getFileSize(filePath))
console.log('\tdest =', humanSize(info.size))
await move(tmpPath, outPath, {overwrite: true})
resolve(info)
}
else {
console.log('\terror =', err)
reject(err.toString())
}
})
})
}
ensureDir(TEMP_DIR)
ensureDir(OUTPUT_DIR)
const manifest = loadManifest()
let numFilesProcessed = 0
async function processImage(filePath) {
if (SKIP_PROCESSED_FILES && manifest[filePath]) return
if (SKIP_NON_ACTIVESTORAGE_FILES && (path.extname(filePath) !== '' ||
path.basename(filePath).length !== 28)) return
if (isDirectory(filePath)) return
try {
const info = await resizeImage(filePath)
addToManifest(manifest, filePath, null, info)
} catch(err) {
addToManifest(manifest, filePath, err)
}
saveManifest(manifest)
numFilesProcessed++
if (PROCESS_NUM_FILES && numFilesProcessed > PROCESS_NUM_FILES) {
console.log('exiting after ', numFilesProcessed, 'files processed')
process.exit()
}
console.log('memory usage: rss =', humanSize(process.memoryUsage().rss),
', heap used =', humanSize(process.memoryUsage().heapUsed),
', heap total =', humanSize(process.memoryUsage().heapTotal))
}
// Watch filesystem
if (ENABLE_WATCHER) {
chokidar
.watch(SOURCE_DIR + GLOB_PATTERN, {
ignoreInitial: true
})
.on('add', async (filePath) => {
await processImage(filePath)
})
}
// Process filesystem
if (ENABLE_PROCESS) {
glob(SOURCE_DIR + GLOB_PATTERN, async (err, files) => {
for (const filePath of files) {
await processImage(filePath)
}
})
}