@tryghost/mg-fs-utils 0.8.3 → 0.9.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/lib/FileCache.js +30 -8
- package/lib/csv.js +12 -0
- package/package.json +2 -2
package/lib/FileCache.js
CHANGED
|
@@ -157,11 +157,27 @@ class FileCache {
|
|
|
157
157
|
if (type === 'images') {
|
|
158
158
|
typeDir = this.imageDir;
|
|
159
159
|
typePath = this.imagePath;
|
|
160
|
+
|
|
161
|
+
// CASE: In ImageSCraper, we infer file type based on the first few bytes of the image.
|
|
162
|
+
// The extension we get for `image/jpeg` is `.jpg`, so here transform `.jpeg` to `.jpg`
|
|
163
|
+
// to ensure we don't miss images that already exist
|
|
164
|
+
if (ext.toLowerCase() === '.jpeg') {
|
|
165
|
+
filename = filename.replace('.jpeg', '.jpg');
|
|
166
|
+
}
|
|
160
167
|
} else if (type === 'media') {
|
|
161
168
|
typeDir = this.mediaDir;
|
|
162
169
|
typePath = this.mediaPath;
|
|
163
170
|
}
|
|
164
171
|
|
|
172
|
+
// CASE: Some image URLs are very long and can cause various issues with storage.
|
|
173
|
+
// If the filepath is more than 200 characters, slice the last 200 and use that
|
|
174
|
+
let theBasename = path.basename(filename);
|
|
175
|
+
let filePath = filename.replace(theBasename, '');
|
|
176
|
+
if (filePath.length > 200) {
|
|
177
|
+
let shorter = filePath.slice(-200);
|
|
178
|
+
filename = path.join('/', shorter, theBasename);
|
|
179
|
+
}
|
|
180
|
+
|
|
165
181
|
// remove the base filePath if it already exists in the path, so we don't get nested filePath directories
|
|
166
182
|
filename = filename.replace(`/${typePath}`, '');
|
|
167
183
|
|
|
@@ -293,15 +309,21 @@ class FileCache {
|
|
|
293
309
|
options = this.resolveImageFileName(options.filename);
|
|
294
310
|
}
|
|
295
311
|
|
|
296
|
-
|
|
297
|
-
let fileExt = path.extname(options.filename).substr(1);
|
|
298
|
-
if (options.optimize && imageTransform.canTransformToFormat(fileExt)) {
|
|
299
|
-
const optimizedStoragePath = options.storagePath;
|
|
300
|
-
const originalStoragePath = imageTransform.generateOriginalImageName(options.storagePath);
|
|
301
|
-
const optimizedData = await imageTransform.resizeFromBuffer(data, {width: 2000});
|
|
312
|
+
const fileExt = path.extname(options.filename).substr(1);
|
|
302
313
|
|
|
303
|
-
|
|
304
|
-
|
|
314
|
+
// CASE: image manipulator is incapable of transforming file (e.g. .bmp)
|
|
315
|
+
if (options.optimize && imageTransform.canTransformToFormat(fileExt)) {
|
|
316
|
+
try {
|
|
317
|
+
const originalStoragePath = imageTransform.generateOriginalImageName(options.storagePath);
|
|
318
|
+
await fs.outputFile(originalStoragePath, data);
|
|
319
|
+
const optimizedStoragePath = options.storagePath;
|
|
320
|
+
const optimizedData = await imageTransform.resizeFromBuffer(data, {width: 2000});
|
|
321
|
+
await fs.outputFile(optimizedStoragePath, optimizedData);
|
|
322
|
+
} catch (error) {
|
|
323
|
+
// Silently fail and only save the original image without manipulation
|
|
324
|
+
// TODO: Catch errors and push to ctx.errors
|
|
325
|
+
await fs.outputFile(options.storagePath, data);
|
|
326
|
+
}
|
|
305
327
|
} else {
|
|
306
328
|
await fs.outputFile(options.storagePath, data);
|
|
307
329
|
}
|
package/lib/csv.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const {parse} = require('csv-parse');
|
|
2
|
+
const {parse: parseSync} = require('csv-parse/sync');
|
|
2
3
|
const fs = require('fs-extra');
|
|
3
4
|
const {isDate} = require('date-fns');
|
|
4
5
|
|
|
@@ -24,6 +25,17 @@ module.exports.parse = (filePath, options = {skip_lines_with_error: true, column
|
|
|
24
25
|
});
|
|
25
26
|
};
|
|
26
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Parse a CSV string and make available as JSON
|
|
30
|
+
* @param {String} csvString - CSV as a string
|
|
31
|
+
* @param {Object} options - optional options to pass to `csv-parse`
|
|
32
|
+
*/
|
|
33
|
+
module.exports.parseString = (csvString, options = {skip_lines_with_error: true, columns: true, skip_empty_lines: true}) => {
|
|
34
|
+
const data = parseSync(csvString, options);
|
|
35
|
+
|
|
36
|
+
return data;
|
|
37
|
+
};
|
|
38
|
+
|
|
27
39
|
/**
|
|
28
40
|
* Convert a JSON file into a CSV format
|
|
29
41
|
* @param {Array} data - the data to format
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"lodash": "4.17.21",
|
|
30
30
|
"sharp": "0.30.7"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "7f4422a89070611f2500548c14d286cccfd4c861"
|
|
33
33
|
}
|