mirror of
https://github.com/laosb/ghos3.git
synced 2025-04-30 23:51:08 +00:00
Merge f2970d584a
into 82a47babb7
This commit is contained in:
commit
b625cf9fcb
1 changed files with 9 additions and 6 deletions
15
src/index.ts
15
src/index.ts
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue