This commit is contained in:
George 2025-01-17 16:18:17 +08:00 committed by GitHub
commit b625cf9fcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -16,6 +16,9 @@ const stripLeadingSlash = (s: string) =>
s.indexOf('/') === 0 ? s.substring(1) : s
const stripEndingSlash = (s: string) =>
s.indexOf('/') === s.length - 1 ? s.substring(0, s.length - 1) : s
const normalizePath = (filePath) => {
return filePath.replace(/\\/g, '/');
};
type Config = {
accessKeyId?: string
@ -88,7 +91,7 @@ class S3Storage extends StorageBase {
defaultHost
this.pathPrefix = stripLeadingSlash(
process.env.GHOST_STORAGE_ADAPTER_S3_PATH_PREFIX || pathPrefix || ''
normalizePath(process.env.GHOST_STORAGE_ADAPTER_S3_PATH_PREFIX || pathPrefix || '')
)
this.endpoint =
process.env.GHOST_STORAGE_ADAPTER_S3_ENDPOINT || endpoint || ''
@ -103,7 +106,7 @@ class S3Storage extends StorageBase {
try {
await this.s3().deleteObject({
Bucket: this.bucket,
Key: stripLeadingSlash(join(directory, fileName)),
Key: stripLeadingSlash(normalizePath(join(directory, fileName))),
})
} catch {
return false
@ -116,7 +119,7 @@ class S3Storage extends StorageBase {
await this.s3().getObject({
Bucket: this.bucket,
Key: stripLeadingSlash(
targetDir ? join(targetDir, fileName) : fileName
normalizePath(targetDir ? join(targetDir, fileName) : fileName)
),
})
} catch {
@ -164,7 +167,7 @@ class S3Storage extends StorageBase {
Bucket: this.bucket,
CacheControl: `max-age=${30 * 24 * 60 * 60}`,
ContentType: image.type,
Key: stripLeadingSlash(fileName),
Key: stripLeadingSlash(normalizePath(fileName)),
}
await this.s3().putObject(config)
@ -176,7 +179,7 @@ class S3Storage extends StorageBase {
try {
const output = await this.s3().getObject({
Bucket: this.bucket,
Key: stripLeadingSlash(stripEndingSlash(this.pathPrefix) + req.path),
Key: stripLeadingSlash(stripEndingSlash(this.pathPrefix) + normalizePath(req.path)),
})
const headers: { [key: string]: string } = {}
@ -224,7 +227,7 @@ class S3Storage extends StorageBase {
const response = await this.s3().getObject({
Bucket: this.bucket,
Key: stripLeadingSlash(path),
Key: stripLeadingSlash(normalizePath(path)),
})
const stream = response.Body as Readable