-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Addo.Zhang <[email protected]>
- Loading branch information
Showing
10 changed files
with
186 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import ImageUploader from "../imageUploader"; | ||
import AWS from 'aws-sdk'; | ||
import {UploaderUtils} from "../uploaderUtils"; | ||
|
||
export default class AwsS3Uploader implements ImageUploader { | ||
private readonly s3!: AWS.S3; | ||
private readonly bucket!: string; | ||
private pathTmpl: string; | ||
private customDomainName: string; | ||
|
||
|
||
constructor(setting: AwsS3Setting) { | ||
this.s3 = new AWS.S3({ | ||
accessKeyId: setting.accessKeyId, | ||
secretAccessKey: setting.secretAccessKey, | ||
region: setting.region | ||
}); | ||
this.bucket = setting.bucketName; | ||
this.pathTmpl = setting.path; | ||
this.customDomainName = setting.customDomainName; | ||
} | ||
|
||
async upload(image: File, fullPath: string): Promise<string> { | ||
const arrayBuffer = await this.readFileAsArrayBuffer(image); | ||
const uint8Array = new Uint8Array(arrayBuffer); | ||
var path = UploaderUtils.generateName(this.pathTmpl, image.name); | ||
path = path.replace(/^\/+/, ''); // remove the / | ||
const params = { | ||
Bucket: this.bucket, | ||
Key: path, | ||
Body: uint8Array, | ||
}; | ||
return new Promise((resolve, reject) => { | ||
this.s3.upload(params, (err, data) => { | ||
if (err) { | ||
console.log(err) | ||
reject(err); | ||
} else { | ||
resolve(UploaderUtils.customizeDomainName(data.Location, this.customDomainName)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
private readFileAsArrayBuffer(file: File): Promise<ArrayBuffer> { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onload = () => resolve(reader.result as ArrayBuffer); | ||
reader.onerror = reject; | ||
reader.readAsArrayBuffer(file); | ||
}); | ||
} | ||
} | ||
export interface AwsS3Setting { | ||
accessKeyId: string; | ||
secretAccessKey: string; | ||
region: string; | ||
bucketName: string; | ||
path: string; | ||
customDomainName: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export class UploaderUtils { | ||
static generateName(pathTmpl,imageName: string): string { | ||
const date = new Date(); | ||
const year = date.getFullYear().toString(); | ||
const month = (date.getMonth() + 1).toString().padStart(2, '0'); | ||
const day = date.getDate().toString().padStart(2, '0'); | ||
const random = this.generateRandomString(20); | ||
|
||
return pathTmpl != undefined && pathTmpl.trim().length > 0 ? pathTmpl | ||
.replace('{year}', year) | ||
.replace('{mon}', month) | ||
.replace('{day}', day) | ||
.replace('{random}', random) | ||
.replace('{filename}', imageName) | ||
: imageName | ||
; | ||
} | ||
|
||
private static generateRandomString(length: number): string { | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
let result = ''; | ||
|
||
for (let i = 0; i < length; i++) { | ||
const randomIndex = Math.floor(Math.random() * characters.length); | ||
result += characters.charAt(randomIndex); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
static customizeDomainName(url, customDomainName) { | ||
const regex = /https?:\/\/([^/]+)/; | ||
if (customDomainName && customDomainName.trim() !== "") { | ||
return url.replace(regex, (match, domain) => { | ||
return match.replace(domain, customDomainName); | ||
}) | ||
} | ||
return url; | ||
} | ||
} |