Skip to content

Commit

Permalink
fix(bos): 修复key中包含特殊字符时的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
otakustay committed Jan 9, 2023
1 parent 7aad953 commit fd69fb2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/bos/object.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs';
import {Readable} from 'node:stream';
import {Http} from '../shared/index.js';
import {normalizeUrl} from '../utils/string.js';

export interface PutObjectOptions {
headers?: Record<string, string>;
Expand All @@ -14,7 +15,7 @@ export class BosObjectClient {

constructor(http: Http, objectKey: string) {
this.http = http;
this.objectKey = objectKey;
this.objectKey = normalizeUrl(objectKey, false);
}

async get() {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const URL_ESCAPE: Record<string, string> = {
'!': '%21',
'\'': '%27',
'(': '%28',
')': '%29',
'*': '%2A',
};

export const normalizeUrl = (value: string, encodeSlash = true) => {
const parts = value.split('/');
const normalized = parts.map(v => v.replace(/[!'()*]/g, c => URL_ESCAPE[c]));
return normalized.join(encodeSlash ? '%2F' : '/');
};

0 comments on commit fd69fb2

Please sign in to comment.