Skip to content

Commit

Permalink
fix: sanitize unsafe filename chars
Browse files Browse the repository at this point in the history
  • Loading branch information
Quramy committed Jan 2, 2020
1 parent 448c731 commit 2af05b8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/storycap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"mkdirp": "^0.5.1",
"puppeteer": "^1.19.0",
"rimraf": "^3.0.0",
"sanitize-filename": "^1.6.3",
"storycrawler": "^2.3.1",
"yargs": "^14.0.0"
},
Expand Down
10 changes: 9 additions & 1 deletion packages/storycap/src/node/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import * as mkdirp from 'mkdirp';
import { MainOptions } from './types';
import sanitize from 'sanitize-filename';

export class FileSystem {
constructor(private opt: MainOptions) {}
Expand All @@ -18,7 +19,14 @@ export class FileSystem {
*
**/
save(kind: string, story: string, suffix: string[], buffer: Buffer) {
const name = this.opt.flat ? (kind + '_' + story).replace(/\//g, '_') : kind + '/' + story;
const name = this.opt.flat
? sanitize((kind + '_' + story).replace(/\//g, '_'))
: kind
.split('/')
.map(k => sanitize(k))
.join('/') +
'/' +
sanitize(story);
const filePath = path.join(this.opt.outDir, name + (suffix.length ? `_${suffix.join('_')}` : '') + '.png');
return new Promise<string>((resolve, reject) => {
mkdirp.sync(path.dirname(filePath));
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6508,6 +6508,13 @@ sane@^4.0.3:
minimist "^1.1.1"
walker "~1.0.5"

sanitize-filename@^1.6.3:
version "1.6.3"
resolved "https://registry.yarnpkg.com/sanitize-filename/-/sanitize-filename-1.6.3.tgz#755ebd752045931977e30b2025d340d7c9090378"
integrity sha512-y/52Mcy7aw3gRm7IrcGDFx/bCk4AhRh2eI9luHOQM86nZsqwiRkkq2GekHXBBD+SmPidc8i2PqtYZl+pWJ8Oeg==
dependencies:
truncate-utf8-bytes "^1.0.0"

sax@1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.1.tgz#7b8e656190b228e81a66aea748480d828cd2d37a"
Expand Down Expand Up @@ -7164,6 +7171,13 @@ trim-repeated@^1.0.0:
dependencies:
escape-string-regexp "^1.0.2"

truncate-utf8-bytes@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz#405923909592d56f78a5818434b0b78489ca5f2b"
integrity sha1-QFkjkJWS1W94pYGENLC3hInKXys=
dependencies:
utf8-byte-length "^1.0.1"

ts-jest@^24.1.0:
version "24.1.0"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-24.1.0.tgz#2eaa813271a2987b7e6c3fefbda196301c131734"
Expand Down Expand Up @@ -7343,6 +7357,11 @@ use@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"

utf8-byte-length@^1.0.1:
version "1.0.4"
resolved "https://registry.yarnpkg.com/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz#f45f150c4c66eee968186505ab93fcbb8ad6bf61"
integrity sha1-9F8VDExm7uloGGUFq5P8u4rWv2E=

util-deprecate@^1.0.1, util-deprecate@~1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
Expand Down

0 comments on commit 2af05b8

Please sign in to comment.