Skip to content

Commit

Permalink
Stored files flat inside bucket directory, and URL encode the path co…
Browse files Browse the repository at this point in the history
…mponents
  • Loading branch information
rhodgkins committed Aug 9, 2021
1 parent 4a92c39 commit e7c7f8c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
18 changes: 18 additions & 0 deletions scripts/storage-emulator-integration/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ describe("Storage emulator", () => {
resumable: true,
};
});

it("should be able to upload file named 'prefix/file.txt' when file named 'prefix' already exists", async () => {
await testBucket.upload(smallFilePath, {
destination: "prefix",
});
await testBucket.upload(smallFilePath, {
destination: "prefix/file.txt",
});
});

it("should be able to upload file named 'prefix' when file named 'prefix/file.txt' already exists", async () => {
await testBucket.upload(smallFilePath, {
destination: "prefix/file.txt",
});
await testBucket.upload(smallFilePath, {
destination: "prefix",
});
});
});

describe("#getFiles()", () => {
Expand Down
23 changes: 5 additions & 18 deletions src/emulator/storage/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,8 @@ export class StorageLayer {
await fse.ensureDir(metadataDirPath);

for await (const [p, file] of this._files.entries()) {
const metadataExportPath = path.join(metadataDirPath, p) + ".json";
const metadataExportDirPath = path.dirname(metadataExportPath);
const metadataExportPath = path.join(metadataDirPath, encodeURIComponent(p)) + ".json";

await fse.ensureDir(metadataExportDirPath);
await fse.writeFile(metadataExportPath, StoredFileMetadata.toJSON(file.metadata));
}
}
Expand Down Expand Up @@ -560,7 +558,9 @@ export class StorageLayer {
// 1) Get the relative path to the metadata export dir
// 2) Subtract .json from the end
const metadataRelPath = path.relative(metadataDir, f);
const blobPath = metadataRelPath.substring(0, metadataRelPath.length - dotJson.length);
const blobPath = decodeURIComponent(
metadataRelPath.substring(0, metadataRelPath.length - dotJson.length)
);

const blobAbsPath = path.join(blobsDir, blobPath);
if (!existsSync(blobAbsPath)) {
Expand Down Expand Up @@ -605,13 +605,6 @@ export class Persistence {
appendBytes(fileName: string, bytes: Buffer, fileOffset?: number): string {
const filepath = this.getDiskPath(fileName);

const encodedSlashIndex = filepath.toLowerCase().lastIndexOf("%2f");
const dirPath =
encodedSlashIndex >= 0 ? filepath.substring(0, encodedSlashIndex) : path.dirname(filepath);

mkdirSync(dirPath, {
recursive: true,
});
let fd;

try {
Expand Down Expand Up @@ -667,16 +660,10 @@ export class Persistence {
}

renameFile(oldName: string, newName: string): void {
const dirPath = this.getDiskPath(path.dirname(newName));

mkdirSync(dirPath, {
recursive: true,
});

renameSync(this.getDiskPath(oldName), this.getDiskPath(newName));
}

getDiskPath(fileName: string): string {
return path.join(this._dirPath, fileName);
return path.join(this._dirPath, encodeURIComponent(fileName));
}
}

0 comments on commit e7c7f8c

Please sign in to comment.