@tryghost/mg-fs-utils 0.7.11 → 0.8.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/LICENSE +1 -1
- package/README.md +1 -1
- package/lib/FileCache.js +85 -10
- package/lib/csv.js +3 -1
- package/lib/zip.js +1 -1
- package/package.json +11 -11
package/LICENSE
CHANGED
package/README.md
CHANGED
package/lib/FileCache.js
CHANGED
|
@@ -5,9 +5,10 @@ const crypto = require('crypto');
|
|
|
5
5
|
const os = require('os');
|
|
6
6
|
const imageTransform = require('@tryghost/image-transform');
|
|
7
7
|
const errors = require('@tryghost/errors');
|
|
8
|
+
const csv = require('./csv');
|
|
8
9
|
|
|
9
10
|
const basePath = 'mg';
|
|
10
|
-
const
|
|
11
|
+
const knownImageExtensions = ['.jpg', '.jpeg', '.gif', '.png', '.svg', '.svgz', '.ico', 'webp'];
|
|
11
12
|
|
|
12
13
|
class FileCache {
|
|
13
14
|
constructor(cacheName, options = {}) {
|
|
@@ -45,6 +46,8 @@ class FileCache {
|
|
|
45
46
|
* - json file
|
|
46
47
|
* - /content/images
|
|
47
48
|
* - image files
|
|
49
|
+
* - /content/media
|
|
50
|
+
* - non-image media files like videos or audio files
|
|
48
51
|
*/
|
|
49
52
|
get cacheDir() {
|
|
50
53
|
if (!this._cacheDir) {
|
|
@@ -54,6 +57,7 @@ class FileCache {
|
|
|
54
57
|
// don't create the content directory when migrating members
|
|
55
58
|
if (this.options && this.options.contentDir) {
|
|
56
59
|
fs.mkdirpSync(path.join(this.imageDir));
|
|
60
|
+
fs.mkdirpSync(path.join(this.mediaDir));
|
|
57
61
|
} else {
|
|
58
62
|
fs.mkdirpSync(path.join(this.zipDir));
|
|
59
63
|
}
|
|
@@ -81,6 +85,14 @@ class FileCache {
|
|
|
81
85
|
return path.join(this.zipDir, this.imagePath);
|
|
82
86
|
}
|
|
83
87
|
|
|
88
|
+
get mediaPath() {
|
|
89
|
+
return path.join('content', 'media');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get mediaDir() {
|
|
93
|
+
return path.join(this.zipDir, this.mediaPath);
|
|
94
|
+
}
|
|
95
|
+
|
|
84
96
|
get defaultCacheFileName() {
|
|
85
97
|
if (this.batchName) {
|
|
86
98
|
return `gh-${this.cacheName}-batch-${this.batchName}-${Date.now()}`;
|
|
@@ -104,8 +116,12 @@ class FileCache {
|
|
|
104
116
|
return `${this.defaultCacheFileName}.errors.json`;
|
|
105
117
|
}
|
|
106
118
|
|
|
119
|
+
convertMbToBytes(mb) {
|
|
120
|
+
return (mb * 1048576);
|
|
121
|
+
}
|
|
122
|
+
|
|
107
123
|
// @TODO: move this somewhere shared,
|
|
108
|
-
// it's currently duplicated from https://github.com/TryGhost/Ghost-Storage-Base/blob/
|
|
124
|
+
// it's currently duplicated from https://github.com/TryGhost/Ghost-Storage-Base/blob/main/BaseStorage.js#L63
|
|
109
125
|
sanitizeFileName(src) {
|
|
110
126
|
let basename;
|
|
111
127
|
|
|
@@ -125,16 +141,35 @@ class FileCache {
|
|
|
125
141
|
}
|
|
126
142
|
|
|
127
143
|
resolveImageFileName(filename) {
|
|
144
|
+
return this.resolveFileName(filename, 'images');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
resolveMediaFileName(filename) {
|
|
148
|
+
return this.resolveFileName(filename, 'media');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
resolveFileName(filename, type = 'images') {
|
|
128
152
|
let ext = path.extname(filename);
|
|
129
153
|
|
|
130
|
-
|
|
131
|
-
|
|
154
|
+
let typeDir = null;
|
|
155
|
+
let typePath = null;
|
|
156
|
+
|
|
157
|
+
if (type === 'images') {
|
|
158
|
+
typeDir = this.imageDir;
|
|
159
|
+
typePath = this.imagePath;
|
|
160
|
+
} else if (type === 'media') {
|
|
161
|
+
typeDir = this.mediaDir;
|
|
162
|
+
typePath = this.mediaPath;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// remove the base filePath if it already exists in the path, so we don't get nested filePath directories
|
|
166
|
+
filename = filename.replace(`/${typePath}`, '');
|
|
132
167
|
|
|
133
168
|
// replace the basename part with a sanitized version
|
|
134
169
|
filename = filename.replace(filename, this.sanitizeFileName(filename));
|
|
135
170
|
|
|
136
171
|
// @TODO: use content type on request to infer this, rather than assuming jpeg?
|
|
137
|
-
if (!_.includes(
|
|
172
|
+
if (type === 'image' && !_.includes(knownImageExtensions, ext)) {
|
|
138
173
|
if (!ext) {
|
|
139
174
|
filename += '.jpeg';
|
|
140
175
|
} else {
|
|
@@ -144,8 +179,8 @@ class FileCache {
|
|
|
144
179
|
|
|
145
180
|
return {
|
|
146
181
|
filename: filename,
|
|
147
|
-
storagePath: path.join(
|
|
148
|
-
outputPath: path.join('/',
|
|
182
|
+
storagePath: path.join(typeDir, filename),
|
|
183
|
+
outputPath: path.join('/', typePath, filename)
|
|
149
184
|
};
|
|
150
185
|
}
|
|
151
186
|
|
|
@@ -157,7 +192,8 @@ class FileCache {
|
|
|
157
192
|
* @param {Boolean} isJSON - defaults to write a JSON file
|
|
158
193
|
*/
|
|
159
194
|
async writeTmpFile(data, filename, isJSON = true) {
|
|
160
|
-
let
|
|
195
|
+
let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
196
|
+
let filepath = path.join(this.tmpDir, fileNameWithExt);
|
|
161
197
|
|
|
162
198
|
if (isJSON) {
|
|
163
199
|
await fs.outputJson(filepath, data, {spaces: 2});
|
|
@@ -174,7 +210,8 @@ class FileCache {
|
|
|
174
210
|
* @param {String} filename - name of file to read
|
|
175
211
|
*/
|
|
176
212
|
async readTmpJSONFile(filename) {
|
|
177
|
-
let
|
|
213
|
+
let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
214
|
+
let filepath = path.join(this.tmpDir, fileNameWithExt);
|
|
178
215
|
|
|
179
216
|
return await fs.readJson(filepath);
|
|
180
217
|
}
|
|
@@ -221,6 +258,30 @@ class FileCache {
|
|
|
221
258
|
return await this.writeTmpFile(data, filename);
|
|
222
259
|
}
|
|
223
260
|
|
|
261
|
+
/**
|
|
262
|
+
* Create a CSV file with our reports
|
|
263
|
+
*
|
|
264
|
+
* @param {Object} data - a valid JSON object
|
|
265
|
+
* @param {Object} options - config
|
|
266
|
+
*/
|
|
267
|
+
async writeReportCSVFile(report, options = {}) {
|
|
268
|
+
const fileName = options.filename || false;
|
|
269
|
+
const filePath = path.join(this.cacheDir, `report-${fileName}.csv`);
|
|
270
|
+
|
|
271
|
+
const dedupedData = _.uniqBy(report.data, (e) => {
|
|
272
|
+
return e.src;
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
const fileData = csv.jsonToCSV(dedupedData);
|
|
276
|
+
|
|
277
|
+
await fs.writeFile(filePath, fileData);
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
data: dedupedData,
|
|
281
|
+
path: filePath
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
224
285
|
/**
|
|
225
286
|
* Create a binary image file with fetched data
|
|
226
287
|
*
|
|
@@ -248,6 +309,20 @@ class FileCache {
|
|
|
248
309
|
return options.outputPath;
|
|
249
310
|
}
|
|
250
311
|
|
|
312
|
+
async writeMediaFile(data, options) {
|
|
313
|
+
return this.writeFile(data, options, 'media');
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
async writeFile(data, options, type = 'images') {
|
|
317
|
+
if (!options.storagePath || !options.outputPath) {
|
|
318
|
+
options = this.resolveFileName(options.filename, type);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
await fs.outputFile(options.storagePath, data);
|
|
322
|
+
|
|
323
|
+
return options.outputPath;
|
|
324
|
+
}
|
|
325
|
+
|
|
251
326
|
/**
|
|
252
327
|
* Check if we've got this file already
|
|
253
328
|
*
|
|
@@ -258,7 +333,7 @@ class FileCache {
|
|
|
258
333
|
let pathToCheck = filename;
|
|
259
334
|
|
|
260
335
|
if (type && !_.includes(['tmp', 'json', 'image'], type)) {
|
|
261
|
-
throw new errors.
|
|
336
|
+
throw new errors.NotFoundError({message: 'Unknown file type'});
|
|
262
337
|
} else if (type) {
|
|
263
338
|
pathToCheck = path.join(this[`${type}Dir`], filename);
|
|
264
339
|
}
|
package/lib/csv.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const parse = require('csv-parse');
|
|
1
|
+
const {parse} = require('csv-parse');
|
|
2
2
|
const fs = require('fs-extra');
|
|
3
3
|
const {isDate} = require('date-fns');
|
|
4
4
|
|
|
@@ -47,6 +47,8 @@ module.exports.jsonToCSV = (data, fields = Object.keys(data[0])) => {
|
|
|
47
47
|
if (entry[field] !== null) {
|
|
48
48
|
if (isDate(entry[field])) {
|
|
49
49
|
fieldToAdd = entry[field].toISOString();
|
|
50
|
+
} else if (entry[field] && entry[field].toString().indexOf(',') !== -1) { // If the field contains a comma, wrap it in quotes
|
|
51
|
+
fieldToAdd = `"${entry[field]}"`;
|
|
50
52
|
} else {
|
|
51
53
|
fieldToAdd = entry[field];
|
|
52
54
|
}
|
package/lib/zip.js
CHANGED
|
@@ -18,7 +18,7 @@ module.exports.read = (zipPath, callback) => {
|
|
|
18
18
|
try {
|
|
19
19
|
zip = _private.openZipForRead(zipPath);
|
|
20
20
|
} catch (error) {
|
|
21
|
-
throw new errors.
|
|
21
|
+
throw new errors.InternalServerError({message: `Unable to read zip file ${zipPath}: ${error}`});
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// Entries is cleaned first
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"repository": "https://github.com/TryGhost/migrate/tree/
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "index.js",
|
|
@@ -19,15 +19,15 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@tryghost/errors": "
|
|
23
|
-
"@tryghost/image-transform": "1.0.
|
|
24
|
-
"@tryghost/zip": "1.1.
|
|
25
|
-
"adm-zip": "0.5.
|
|
26
|
-
"csv-parse": "
|
|
27
|
-
"date-fns": "2.
|
|
28
|
-
"fs-extra": "10.
|
|
22
|
+
"@tryghost/errors": "1.2.11",
|
|
23
|
+
"@tryghost/image-transform": "1.0.30",
|
|
24
|
+
"@tryghost/zip": "1.1.23",
|
|
25
|
+
"adm-zip": "0.5.9",
|
|
26
|
+
"csv-parse": "5.0.4",
|
|
27
|
+
"date-fns": "2.28.0",
|
|
28
|
+
"fs-extra": "10.1.0",
|
|
29
29
|
"lodash": "4.17.21",
|
|
30
|
-
"sharp": "0.
|
|
30
|
+
"sharp": "0.30.4"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "a2fd432523e0e9f316686511d7494b855ef0007e"
|
|
33
33
|
}
|