@tryghost/mg-fs-utils 0.11.6 → 0.12.0
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/index.js +3 -1
- package/lib/FileCache.js +39 -39
- package/lib/read-zip.js +32 -0
- package/package.json +7 -6
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {FileCache} from './lib/FileCache.js';
|
|
2
2
|
import zip from './lib/zip.js';
|
|
3
|
+
import {readZipEntries} from './lib/read-zip.js';
|
|
3
4
|
import ghostJSON from './lib/ghostJSON.js';
|
|
4
5
|
import csv from './lib/csv.js';
|
|
5
6
|
|
|
@@ -7,5 +8,6 @@ export default {
|
|
|
7
8
|
FileCache,
|
|
8
9
|
zip,
|
|
9
10
|
ghostJSON,
|
|
10
|
-
csv
|
|
11
|
+
csv,
|
|
12
|
+
readZipEntries
|
|
11
13
|
};
|
package/lib/FileCache.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import {join, parse, extname, basename, dirname} from 'node:path';
|
|
2
|
+
import {tmpdir} from 'node:os';
|
|
3
|
+
import {createHash} from 'node:crypto';
|
|
4
4
|
import _ from 'lodash';
|
|
5
5
|
import {writeFileSync, readdirSync, rmdir, lstatSync, existsSync} from 'node:fs';
|
|
6
6
|
import {writeFile} from 'node:fs/promises';
|
|
@@ -21,8 +21,8 @@ class FileCache {
|
|
|
21
21
|
this.options = Object.assign({contentDir: true}, options);
|
|
22
22
|
|
|
23
23
|
// Remove any extension, handles removing TLDs as well if the name is based on a URL
|
|
24
|
-
let ext =
|
|
25
|
-
this.cacheName =
|
|
24
|
+
let ext = extname(cacheName);
|
|
25
|
+
this.cacheName = basename(cacheName, ext);
|
|
26
26
|
|
|
27
27
|
if (options.batchName) {
|
|
28
28
|
this.batchName = options.batchName;
|
|
@@ -31,17 +31,17 @@ class FileCache {
|
|
|
31
31
|
|
|
32
32
|
// This will be the path specified by `--tmpPath` if set. If not, it'll use a hidden tmp dir
|
|
33
33
|
get tmpDirPath() {
|
|
34
|
-
return this.tmpPath ||
|
|
34
|
+
return this.tmpPath || tmpdir();
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
get cacheBaseDir() {
|
|
38
|
-
return
|
|
38
|
+
return join(this.tmpDirPath, basePath);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
get cacheKey() {
|
|
42
42
|
if (!this._cacheKey) {
|
|
43
43
|
// Unique hash based on full zip path + the original filename
|
|
44
|
-
this._cacheKey = `${
|
|
44
|
+
this._cacheKey = `${createHash('md5').update(this.originalName).digest('hex')}-${this.cacheName}`;
|
|
45
45
|
}
|
|
46
46
|
return this._cacheKey;
|
|
47
47
|
}
|
|
@@ -63,27 +63,27 @@ class FileCache {
|
|
|
63
63
|
*/
|
|
64
64
|
get cacheDir() {
|
|
65
65
|
if (!this._cacheDir) {
|
|
66
|
-
this._cacheDir =
|
|
67
|
-
mkdirpSync(
|
|
66
|
+
this._cacheDir = join(this.tmpDirPath, basePath, this.cacheKey);
|
|
67
|
+
mkdirpSync(join(this.tmpDir));
|
|
68
68
|
|
|
69
69
|
// don't create the content directory when migrating members
|
|
70
70
|
if (this.options && this.options.contentDir) {
|
|
71
|
-
mkdirpSync(
|
|
72
|
-
mkdirpSync(
|
|
73
|
-
mkdirpSync(
|
|
71
|
+
mkdirpSync(join(this.imageDir));
|
|
72
|
+
mkdirpSync(join(this.mediaDir));
|
|
73
|
+
mkdirpSync(join(this.filesDir));
|
|
74
74
|
} else {
|
|
75
|
-
mkdirpSync(
|
|
75
|
+
mkdirpSync(join(this.zipDir));
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
return this._cacheDir;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
get tmpDir() {
|
|
82
|
-
return
|
|
82
|
+
return join(this.cacheDir, 'tmp');
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
get zipDir() {
|
|
86
|
-
return
|
|
86
|
+
return join(this.cacheDir, 'zip');
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
get jsonDir() {
|
|
@@ -91,27 +91,27 @@ class FileCache {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
get imagePath() {
|
|
94
|
-
return
|
|
94
|
+
return join('content', 'images');
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
get imageDir() {
|
|
98
|
-
return
|
|
98
|
+
return join(this.zipDir, this.imagePath);
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
get mediaPath() {
|
|
102
|
-
return
|
|
102
|
+
return join('content', 'media');
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
get mediaDir() {
|
|
106
|
-
return
|
|
106
|
+
return join(this.zipDir, this.mediaPath);
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
get filesPath() {
|
|
110
|
-
return
|
|
110
|
+
return join('content', 'files');
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
get filesDir() {
|
|
114
|
-
return
|
|
114
|
+
return join(this.zipDir, this.filesPath);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
get defaultCacheFileName() {
|
|
@@ -142,7 +142,7 @@ class FileCache {
|
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
sanitizeFileName(src) {
|
|
145
|
-
let fileNameNoExt =
|
|
145
|
+
let fileNameNoExt = parse(src).name;
|
|
146
146
|
|
|
147
147
|
let safeFileNameNoExt = transliterate.slugify(fileNameNoExt);
|
|
148
148
|
|
|
@@ -158,7 +158,7 @@ class FileCache {
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
resolveFileName(filename, type = 'images') {
|
|
161
|
-
let ext =
|
|
161
|
+
let ext = extname(filename);
|
|
162
162
|
|
|
163
163
|
let typeDir = null;
|
|
164
164
|
let typePath = null;
|
|
@@ -189,11 +189,11 @@ class FileCache {
|
|
|
189
189
|
|
|
190
190
|
// CASE: Some image URLs are very long and can cause various issues with storage.
|
|
191
191
|
// If the filepath is more than 200 characters, slice the last 200 and use that
|
|
192
|
-
let theBasename =
|
|
192
|
+
let theBasename = basename(filename);
|
|
193
193
|
let filePath = filename.replace(theBasename, '');
|
|
194
194
|
if (filePath.length > 200) {
|
|
195
195
|
let shorter = filePath.slice(-200);
|
|
196
|
-
filename =
|
|
196
|
+
filename = join('/', shorter, theBasename);
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
// @TODO: use content type on request to infer this, rather than assuming jpeg?
|
|
@@ -207,8 +207,8 @@ class FileCache {
|
|
|
207
207
|
|
|
208
208
|
return {
|
|
209
209
|
filename: filename,
|
|
210
|
-
storagePath:
|
|
211
|
-
outputPath:
|
|
210
|
+
storagePath: join(typeDir, filename),
|
|
211
|
+
outputPath: join('/', typePath, filename)
|
|
212
212
|
};
|
|
213
213
|
}
|
|
214
214
|
|
|
@@ -221,7 +221,7 @@ class FileCache {
|
|
|
221
221
|
*/
|
|
222
222
|
async writeTmpFile(data, filename, isJSON = true) {
|
|
223
223
|
let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
224
|
-
let filepath =
|
|
224
|
+
let filepath = join(this.tmpDir, fileNameWithExt);
|
|
225
225
|
|
|
226
226
|
if (isJSON) {
|
|
227
227
|
await outputJson(filepath, data, {spaces: 2});
|
|
@@ -241,7 +241,7 @@ class FileCache {
|
|
|
241
241
|
*/
|
|
242
242
|
writeTmpFileSync(data, filename, isJSON = true) {
|
|
243
243
|
let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
244
|
-
let filepath =
|
|
244
|
+
let filepath = join(this.tmpDir, fileNameWithExt);
|
|
245
245
|
|
|
246
246
|
if (isJSON) {
|
|
247
247
|
outputJsonSync(filepath, data, {spaces: 2});
|
|
@@ -259,7 +259,7 @@ class FileCache {
|
|
|
259
259
|
*/
|
|
260
260
|
async readTmpJSONFile(filename) {
|
|
261
261
|
let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
262
|
-
let filepath =
|
|
262
|
+
let filepath = join(this.tmpDir, fileNameWithExt);
|
|
263
263
|
|
|
264
264
|
return await readJson(filepath);
|
|
265
265
|
}
|
|
@@ -271,7 +271,7 @@ class FileCache {
|
|
|
271
271
|
*/
|
|
272
272
|
hasTmpJSONFile(filename) {
|
|
273
273
|
let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
274
|
-
let filepath =
|
|
274
|
+
let filepath = join(this.tmpDir, fileNameWithExt);
|
|
275
275
|
|
|
276
276
|
return existsSync(filepath);
|
|
277
277
|
}
|
|
@@ -294,8 +294,8 @@ class FileCache {
|
|
|
294
294
|
|
|
295
295
|
// Then also write it as "the" JSON file in the zip folder
|
|
296
296
|
filename = options.filename || `ghost-import.json`;
|
|
297
|
-
let basepath = options.path ?
|
|
298
|
-
let filepath =
|
|
297
|
+
let basepath = options.path ? dirname(options.path) : this.zipDir;
|
|
298
|
+
let filepath = join(basepath, filename);
|
|
299
299
|
|
|
300
300
|
if (isJSON) {
|
|
301
301
|
await outputJson(filepath, data, {spaces: 2});
|
|
@@ -326,7 +326,7 @@ class FileCache {
|
|
|
326
326
|
*/
|
|
327
327
|
async writeReportCSVFile(report, options = {}) {
|
|
328
328
|
const fileName = options.filename || false;
|
|
329
|
-
const filePath =
|
|
329
|
+
const filePath = join(this.cacheDir, `report-${fileName}.csv`);
|
|
330
330
|
|
|
331
331
|
const dedupedData = _.uniqBy(report.data, (e) => {
|
|
332
332
|
return e.src;
|
|
@@ -353,7 +353,7 @@ class FileCache {
|
|
|
353
353
|
options = this.resolveImageFileName(options.filename);
|
|
354
354
|
}
|
|
355
355
|
|
|
356
|
-
const fileExt =
|
|
356
|
+
const fileExt = extname(options.filename).substr(1);
|
|
357
357
|
|
|
358
358
|
// CASE: image manipulator is incapable of transforming file (e.g. .bmp)
|
|
359
359
|
if (options.optimize && imageTransform.canTransformToFormat(fileExt)) {
|
|
@@ -397,7 +397,7 @@ class FileCache {
|
|
|
397
397
|
if (type && !_.includes(['tmp', 'json', 'image', 'zip'], type)) {
|
|
398
398
|
throw new errors.NotFoundError({message: 'Unknown file type'});
|
|
399
399
|
} else if (type) {
|
|
400
|
-
pathToCheck =
|
|
400
|
+
pathToCheck = join(this[`${type}Dir`], filename);
|
|
401
401
|
}
|
|
402
402
|
|
|
403
403
|
try {
|
|
@@ -412,7 +412,7 @@ class FileCache {
|
|
|
412
412
|
return false;
|
|
413
413
|
}
|
|
414
414
|
|
|
415
|
-
let siteCachePath =
|
|
415
|
+
let siteCachePath = join(this.cacheBaseDir, this.cacheKey);
|
|
416
416
|
|
|
417
417
|
try {
|
|
418
418
|
await remove(siteCachePath);
|
|
@@ -437,7 +437,7 @@ class FileCache {
|
|
|
437
437
|
|
|
438
438
|
let itemsToDelete = [];
|
|
439
439
|
const dirContents = readdirSync(directory).map((fileName) => {
|
|
440
|
-
return
|
|
440
|
+
return join(directory, fileName);
|
|
441
441
|
});
|
|
442
442
|
|
|
443
443
|
dirContents.forEach((item) => {
|
package/lib/read-zip.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import errors from '@tryghost/errors';
|
|
2
|
+
import StreamZip from 'node-stream-zip';
|
|
3
|
+
|
|
4
|
+
const readZipEntries = async (zipPath = null) => {
|
|
5
|
+
if (!zipPath) {
|
|
6
|
+
throw new errors.InternalServerError({message: `No zip path provided ${zipPath}`});
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const zip = new StreamZip.async({file: zipPath});
|
|
10
|
+
|
|
11
|
+
let files = [];
|
|
12
|
+
|
|
13
|
+
const zipEntries = await zip.entries();
|
|
14
|
+
|
|
15
|
+
for (const entry of Object.values(zipEntries)) {
|
|
16
|
+
if ((entry.name.match(/^__MACOSX/) || entry.name.match(/\.DS_Store$/))) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (entry && !entry.isDirectory) {
|
|
21
|
+
files.push(entry.name);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
await zip.close();
|
|
26
|
+
|
|
27
|
+
return files;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export {
|
|
31
|
+
readZipEntries
|
|
32
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,21 +20,22 @@
|
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"eslint": "8.
|
|
24
|
-
"jest": "29.3
|
|
25
|
-
"jest-extended": "3.2.
|
|
23
|
+
"eslint": "8.34.0",
|
|
24
|
+
"jest": "29.4.3",
|
|
25
|
+
"jest-extended": "3.2.3"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@tryghost/errors": "1.2.20",
|
|
29
29
|
"@tryghost/image-transform": "1.2.3",
|
|
30
30
|
"@tryghost/zip": "1.1.31",
|
|
31
31
|
"adm-zip": "0.5.10",
|
|
32
|
-
"csv-parse": "5.3.
|
|
32
|
+
"csv-parse": "5.3.5",
|
|
33
33
|
"date-fns": "2.29.3",
|
|
34
34
|
"fs-extra": "11.1.0",
|
|
35
35
|
"lodash": "4.17.21",
|
|
36
|
+
"node-stream-zip": "1.15.0",
|
|
36
37
|
"sharp": "0.31.3",
|
|
37
38
|
"transliteration": "2.3.5"
|
|
38
39
|
},
|
|
39
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "c4270f6229caa350155a03a0646b3c0c21eaff5e"
|
|
40
41
|
}
|