@tryghost/mg-fs-utils 0.7.13 → 0.8.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2013-2021 Ghost Foundation
3
+ Copyright (c) 2013-2022 Ghost Foundation
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -36,4 +36,4 @@ Follow the instructions for the top-level repo.
36
36
 
37
37
  # Copyright & License
38
38
 
39
- Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).
39
+ Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
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 knownExtensions = ['.jpg', '.jpeg', '.gif', '.png', '.svg', '.svgz', '.ico', 'webp'];
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/master/BaseStorage.js#L62
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
- // remove the base imagePath if it already exists in the path, so we don't get nested imagePath directories
131
- filename = filename.replace(`/${this.imagePath}`, '');
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(knownExtensions, ext)) {
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(this.imageDir, filename),
148
- outputPath: path.join('/', this.imagePath, filename)
182
+ storagePath: path.join(typeDir, filename),
183
+ outputPath: path.join('/', typePath, filename)
149
184
  };
150
185
  }
151
186
 
@@ -157,10 +192,11 @@ 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 filepath = path.join(this.tmpDir, filename);
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
- await fs.outputJson(`${filepath}.json`, data, {spaces: 2});
199
+ await fs.outputJson(filepath, data, {spaces: 2});
164
200
  } else {
165
201
  await fs.writeFile(filepath, data);
166
202
  }
@@ -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 filepath = path.join(this.tmpDir, `${filename}.json`);
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.GhostError({message: 'Unknown file type'});
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.GhostError({message: `Unable to read zip file ${zipPath}: ${error}`});
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.7.13",
4
- "repository": "https://github.com/TryGhost/migrate/tree/master/packages/mg-fs-utils",
3
+ "version": "0.8.2",
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,14 +19,15 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@tryghost/errors": "^0.2.13",
23
- "@tryghost/image-transform": "1.0.18",
24
- "@tryghost/zip": "1.1.18",
22
+ "@tryghost/errors": "1.2.12",
23
+ "@tryghost/image-transform": "1.0.32",
24
+ "@tryghost/zip": "1.1.25",
25
25
  "adm-zip": "0.5.9",
26
- "csv-parse": "4.16.3",
27
- "date-fns": "2.25.0",
28
- "fs-extra": "10.0.0",
29
- "lodash": "4.17.21"
26
+ "csv-parse": "5.1.0",
27
+ "date-fns": "2.28.0",
28
+ "fs-extra": "10.1.0",
29
+ "lodash": "4.17.21",
30
+ "sharp": "0.30.5"
30
31
  },
31
- "gitHead": "7f5133e5ad1c6b6981fc6cd70782aa58a64fba13"
32
+ "gitHead": "d5086f08707d7f00b5174ee9cf0a53ff8f026b63"
32
33
  }