Skip to content

Commit 54db4a2

Browse files
committed
fix windows paths
1 parent 0907c50 commit 54db4a2

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/lib/gcp-client.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ describe("GcpClient", () => {
6666
gcpClient.uploadFile(mockFilePath, mockDestinationPath)
6767
).rejects.toThrow("Upload failed");
6868
});
69+
70+
it("should normalize Windows-style paths to use forward slashes", async () => {
71+
// Arrange
72+
const windowsPath = "path\\in\\bucket\\file.txt";
73+
const expectedPath = "path/in/bucket/file.txt";
74+
const mockStorage = new Storage();
75+
const mockBucket = mockStorage.bucket(mockBucketName);
76+
77+
// Act
78+
await gcpClient.uploadFile(mockFilePath, windowsPath);
79+
80+
// Assert
81+
expect(mockBucket.upload).toHaveBeenCalledWith(mockFilePath, {
82+
destination: expectedPath,
83+
metadata: {
84+
cacheControl: "public, max-age=31536000",
85+
},
86+
});
87+
});
88+
89+
it("should handle paths with mixed separators", async () => {
90+
// Arrange
91+
const mixedPath = "path\\in/bucket\\file.txt";
92+
const expectedPath = "path/in/bucket/file.txt";
93+
const mockStorage = new Storage();
94+
const mockBucket = mockStorage.bucket(mockBucketName);
95+
96+
// Act
97+
await gcpClient.uploadFile(mockFilePath, mixedPath);
98+
99+
// Assert
100+
expect(mockBucket.upload).toHaveBeenCalledWith(mockFilePath, {
101+
destination: expectedPath,
102+
metadata: {
103+
cacheControl: "public, max-age=31536000",
104+
},
105+
});
106+
});
69107
});
70108

71109
describe("listContent", () => {

src/lib/gcp-client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Storage } from "@google-cloud/storage";
22
import * as dotenv from "dotenv";
33
import * as fs from "fs";
4+
import * as path from "path";
45

56
dotenv.config();
67

@@ -54,14 +55,16 @@ export class GcpClient {
5455
async uploadFile(filePath: string, destinationPath: string): Promise<void> {
5556
try {
5657
const bucket = this.storage.bucket(this.bucketName);
58+
// Normalize the destination path to use forward slashes by replacing all backslashes
59+
const normalizedDestinationPath = destinationPath.replace(/\\/g, '/');
5760
await bucket.upload(filePath, {
58-
destination: destinationPath,
61+
destination: normalizedDestinationPath,
5962
metadata: {
6063
cacheControl: "public, max-age=31536000",
6164
},
6265
});
6366
console.log(
64-
`File ${filePath} uploaded to gs://${this.bucketName}/${destinationPath}`
67+
`File ${filePath} uploaded to gs://${this.bucketName}/${normalizedDestinationPath}`
6568
);
6669
} catch (error) {
6770
console.error("Error uploading file:", error);

0 commit comments

Comments
 (0)