[go: nahoru, domu]

Skip to content

Commit

Permalink
Fixes #3406
Browse files Browse the repository at this point in the history
  • Loading branch information
abeisgoat committed May 26, 2021
1 parent a012838 commit fd03b27
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- Fixes manually setting download tokens in Storage Emulator. (#3396)
- Fixes deleting custom metadata in Storage emulator. (#3385)
- Fixes errors when calling makePublic() with Storage Emulator(#3394)
- Fixes mishandling of bytes when uploading two files with the same name in the Storage Emulator (#3406)
76 changes: 76 additions & 0 deletions scripts/storage-emulator-integration/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,31 @@ describe("Storage emulator", () => {
// Doesn't require an assertion, will throw on failure
});

it("should replace existing file on upload", async () => {
const path = "replace.txt";
const content1 = createRandomFile("small_content_1", 10);
const content2 = createRandomFile("small_content_2", 10);
const file = testBucket.file(path);

await testBucket.upload(content1, {
destination: path,
});

const [readContent1] = await file.download();

expect(readContent1).to.deep.equal(fs.readFileSync(content1));

await testBucket.upload(content2, {
destination: path,
});

const [readContent2] = await file.download();
expect(readContent2).to.deep.equal(fs.readFileSync(content2));

fs.unlinkSync(content1);
fs.unlinkSync(content2);
});

it("should handle gzip'd uploads", async () => {
// This appears to pass, but the file gets corrupted cause it's gzipped?
// expect(true).to.be.false;
Expand Down Expand Up @@ -637,6 +662,57 @@ describe("Storage emulator", () => {
expect(uploadState).to.equal("success");
});

it("should upload replace existing file", async function (this) {
this.timeout(TEST_SETUP_TIMEOUT);

const uploadText = (text: string) =>
page.evaluate((TEXT_FILE) => {
const auth = (window as any).auth as firebase.auth.Auth;

return auth
.signInAnonymously()
.then(() => {
return firebase.storage().ref("replace.txt").putString(TEXT_FILE);
})
.then((task) => {
return task.state;
})
.catch((err) => {
throw err.message;
});
}, text);

await uploadText("some-content");
await uploadText("some-other-content");

const downloadUrl = await page.evaluate((filename) => {
return firebase.storage().ref("replace.txt").getDownloadURL();
}, filename);

const requestClient = TEST_CONFIG.useProductionServers ? https : http;
await new Promise((resolve, reject) => {
requestClient.get(
downloadUrl,
{
headers: {
// This is considered an authorized request in the emulator
Authorization: "Bearer owner",
},
},
(response) => {
const data: any = [];
response
.on("data", (chunk) => data.push(chunk))
.on("end", () => {
expect(Buffer.concat(data).toString()).to.equal("some-other-content");
})
.on("close", resolve)
.on("error", reject);
}
);
});
});

it("should upload a file into a directory", async () => {
const uploadState = await page.evaluate((IMAGE_FILE_BASE64) => {
const auth = (window as any).auth as firebase.auth.Auth;
Expand Down
15 changes: 13 additions & 2 deletions src/emulator/storage/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ export class StorageLayer {
);
const file = new StoredFile(finalMetadata, filePath);
this._files.set(filePath, file);

this._persistence.deleteFile(filePath, true);
this._persistence.renameFile(upload.fileLocation, filePath);

this._cloudFunctions.dispatch("finalize", new CloudStorageObjectMetadata(file.metadata));
Expand All @@ -283,6 +285,9 @@ export class StorageLayer {
bytes: Buffer
) {
const filePath = this.path(bucket, object);

this._persistence.deleteFile(filePath, true);

this._persistence.appendBytes(filePath, bytes);
const md = new StoredFileMetadata(
{
Expand Down Expand Up @@ -635,8 +640,14 @@ export class Persistence {
}
}

deleteFile(fileName: string): void {
unlinkSync(this.getDiskPath(fileName));
deleteFile(fileName: string, failSilently = false): void {
try {
unlinkSync(this.getDiskPath(fileName));
} catch (err) {
if (!failSilently) {
throw err;
}
}
}

deleteAll(): Promise<void> {
Expand Down

0 comments on commit fd03b27

Please sign in to comment.