@tryghost/mg-fs-utils 0.10.4 → 0.11.1
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 +19 -23
- package/lib/zip.js +6 -2
- package/package.json +4 -3
package/lib/FileCache.js
CHANGED
|
@@ -5,6 +5,7 @@ import _ from 'lodash';
|
|
|
5
5
|
import fs from 'fs-extra';
|
|
6
6
|
import imageTransform from '@tryghost/image-transform';
|
|
7
7
|
import errors from '@tryghost/errors';
|
|
8
|
+
import transliterate from 'transliteration';
|
|
8
9
|
import csv from './csv.js';
|
|
9
10
|
|
|
10
11
|
const basePath = 'mg';
|
|
@@ -12,6 +13,8 @@ const knownImageExtensions = ['.jpg', '.jpeg', '.gif', '.png', '.svg', '.svgz',
|
|
|
12
13
|
|
|
13
14
|
class FileCache {
|
|
14
15
|
constructor(cacheName, options = {}) {
|
|
16
|
+
this.tmpPath = options.tmpPath || false;
|
|
17
|
+
|
|
15
18
|
this.originalName = cacheName;
|
|
16
19
|
this.options = Object.assign({contentDir: true}, options);
|
|
17
20
|
|
|
@@ -24,8 +27,13 @@ class FileCache {
|
|
|
24
27
|
}
|
|
25
28
|
}
|
|
26
29
|
|
|
30
|
+
// This will be the path specified by `--tmpPath` if set. If not, it'll use a hidden tmp dir
|
|
31
|
+
get tmpDirPath() {
|
|
32
|
+
return this.tmpPath || os.tmpdir();
|
|
33
|
+
}
|
|
34
|
+
|
|
27
35
|
get cacheBaseDir() {
|
|
28
|
-
return path.join(
|
|
36
|
+
return path.join(this.tmpDirPath, basePath);
|
|
29
37
|
}
|
|
30
38
|
|
|
31
39
|
get cacheKey() {
|
|
@@ -53,7 +61,7 @@ class FileCache {
|
|
|
53
61
|
*/
|
|
54
62
|
get cacheDir() {
|
|
55
63
|
if (!this._cacheDir) {
|
|
56
|
-
this._cacheDir = path.join(
|
|
64
|
+
this._cacheDir = path.join(this.tmpDirPath, basePath, this.cacheKey);
|
|
57
65
|
fs.mkdirpSync(path.join(this.tmpDir));
|
|
58
66
|
|
|
59
67
|
// don't create the content directory when migrating members
|
|
@@ -131,24 +139,12 @@ class FileCache {
|
|
|
131
139
|
return (mb * 1048576);
|
|
132
140
|
}
|
|
133
141
|
|
|
134
|
-
// @TODO: move this somewhere shared,
|
|
135
|
-
// it's currently duplicated from https://github.com/TryGhost/Ghost-Storage-Base/blob/main/BaseStorage.js#L63
|
|
136
142
|
sanitizeFileName(src) {
|
|
137
|
-
let
|
|
143
|
+
let fileNameNoExt = path.parse(src).name;
|
|
138
144
|
|
|
139
|
-
|
|
140
|
-
src = src.replace(src, this.sanitizeDirName(src));
|
|
141
|
-
|
|
142
|
-
basename = path.basename(src);
|
|
143
|
-
// below only matches ascii characters, @, and .
|
|
144
|
-
// unicode filenames like город.zip would therefore resolve to ----.zip
|
|
145
|
-
return src.replace(basename, basename.replace(/[^\w@.]/gi, '-'));
|
|
146
|
-
}
|
|
145
|
+
let safeFileNameNoExt = transliterate.slugify(fileNameNoExt);
|
|
147
146
|
|
|
148
|
-
|
|
149
|
-
// Slighly different regex from sanitizing the filename, as we still want to
|
|
150
|
-
// support characters like `/`, `_`, and `-`
|
|
151
|
-
return src.replace(/[^\w@./-_]/gi, '-');
|
|
147
|
+
return src.replace(fileNameNoExt, safeFileNameNoExt);
|
|
152
148
|
}
|
|
153
149
|
|
|
154
150
|
resolveImageFileName(filename) {
|
|
@@ -183,6 +179,12 @@ class FileCache {
|
|
|
183
179
|
typePath = this.filesPath;
|
|
184
180
|
}
|
|
185
181
|
|
|
182
|
+
// remove the base filePath if it already exists in the path, so we don't get nested filePath directories
|
|
183
|
+
filename = filename.replace(`/${typePath}`, '');
|
|
184
|
+
|
|
185
|
+
// replace the basename part with a sanitized version
|
|
186
|
+
filename = filename.replace(filename, this.sanitizeFileName(filename));
|
|
187
|
+
|
|
186
188
|
// CASE: Some image URLs are very long and can cause various issues with storage.
|
|
187
189
|
// If the filepath is more than 200 characters, slice the last 200 and use that
|
|
188
190
|
let theBasename = path.basename(filename);
|
|
@@ -192,12 +194,6 @@ class FileCache {
|
|
|
192
194
|
filename = path.join('/', shorter, theBasename);
|
|
193
195
|
}
|
|
194
196
|
|
|
195
|
-
// remove the base filePath if it already exists in the path, so we don't get nested filePath directories
|
|
196
|
-
filename = filename.replace(`/${typePath}`, '');
|
|
197
|
-
|
|
198
|
-
// replace the basename part with a sanitized version
|
|
199
|
-
filename = filename.replace(filename, this.sanitizeFileName(filename));
|
|
200
|
-
|
|
201
197
|
// @TODO: use content type on request to infer this, rather than assuming jpeg?
|
|
202
198
|
if (type === 'image' && !_.includes(knownImageExtensions, ext)) {
|
|
203
199
|
if (!ext) {
|
package/lib/zip.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {join} from 'node:path';
|
|
2
2
|
import AdmZip from 'adm-zip';
|
|
3
|
+
import {ensureDirSync} from 'fs-extra';
|
|
3
4
|
import zip from '@tryghost/zip';
|
|
4
5
|
import errors from '@tryghost/errors';
|
|
5
6
|
|
|
@@ -43,7 +44,10 @@ const read = (zipPath, callback) => {
|
|
|
43
44
|
};
|
|
44
45
|
|
|
45
46
|
const write = async (zipPath, contentFolder, fileName) => {
|
|
46
|
-
|
|
47
|
+
// Ensure the directory we want to wite to exists
|
|
48
|
+
ensureDirSync(zipPath);
|
|
49
|
+
|
|
50
|
+
const outputPath = join(zipPath, fileName || `ghost-import-${Date.now()}.zip`);
|
|
47
51
|
|
|
48
52
|
return await zip.compress(contentFolder, outputPath);
|
|
49
53
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"date-fns": "2.29.3",
|
|
34
34
|
"fs-extra": "10.1.0",
|
|
35
35
|
"lodash": "4.17.21",
|
|
36
|
-
"sharp": "0.31.
|
|
36
|
+
"sharp": "0.31.2",
|
|
37
|
+
"transliteration": "2.3.5"
|
|
37
38
|
},
|
|
38
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "83d2a7e6b254a6d8b5c54b3b143b379276250b36"
|
|
39
40
|
}
|