Skip to content

Commit

Permalink
fix(stores): encode reserved path characters in win32 envs
Browse files Browse the repository at this point in the history
  • Loading branch information
kherock committed Mar 21, 2019
1 parent cf29c53 commit a3ec092
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/stores/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ const { concatStreams, walk } = require("../utils");
const S3RVER_SUFFIX = "%s._S3rver_%s";

class FilesystemStore {
static decodeKeyPath(keyPath) {
return process.platform === "win32"
? keyPath.replace(/&../g, ent =>
Buffer.from(ent.slice(1), "hex").toString()
)
: keyPath;
}

static encodeKeyPath(key) {
return process.platform === "win32"
? key.replace(
/[<>:"\\|?*]/g,
ch => "&" + Buffer.from(ch, "utf8").toString("hex")
)
: key;
}

constructor(rootDirectory) {
this.rootDirectory = rootDirectory;
}
Expand All @@ -25,7 +42,7 @@ class FilesystemStore {
}

getResourcePath(bucket, key = "", resource) {
const parts = key.split("/");
const parts = FilesystemStore.encodeKeyPath(key).split("/");
const suffix = format(S3RVER_SUFFIX, parts.pop(), resource);
return path.join(this.rootDirectory, bucket, ...parts, suffix);
}
Expand Down Expand Up @@ -128,7 +145,10 @@ class FilesystemStore {
const objectSuffix = format(S3RVER_SUFFIX, "", "object");
let keys = [...walk(bucketPath)]
.filter(file => file.endsWith(objectSuffix))
.map(key => key.slice(bucketPath.length + 1, -objectSuffix.length));
.map(keyPath =>
keyPath.slice(bucketPath.length + 1, -objectSuffix.length)
)
.map(FilesystemStore.decodeKeyPath);

if (!keys.length) {
return {
Expand Down

0 comments on commit a3ec092

Please sign in to comment.