-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(master-asset): add compress image on upload
- Loading branch information
1 parent
55091da
commit 31a3ed1
Showing
16 changed files
with
227 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,6 @@ AWS_ACCESS_KEY_ID= | |
AWS_SECRET_ACCESS_KEY= | ||
AWS_REGION= | ||
AWS_BUCKET= | ||
|
||
#Generator File | ||
GENERATOR_FILE_URL= |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -36,4 +36,7 @@ export interface Config { | |
bucket: string | ||
region: string | ||
} | ||
generator_file: { | ||
url: 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
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,67 @@ | ||
import axios from 'axios' | ||
import { Config } from '../config/config.interface' | ||
import Logger from '../pkg/logger' | ||
import S3 from './s3' | ||
import error from '../pkg/error' | ||
|
||
class FileGenerator { | ||
constructor( | ||
private config: Config, | ||
private s3: S3, | ||
private logger: Logger | ||
) {} | ||
|
||
public async ImageCompression( | ||
uri: string, | ||
quality: number, | ||
convertTo: string, | ||
path: string | ||
) { | ||
try { | ||
const { url, size } = await this.send('convert-image', { | ||
url: uri, | ||
quality, | ||
convertTo, | ||
}) | ||
|
||
const { data, headers } = await axios.get(url, { | ||
responseType: 'arraybuffer', | ||
}) | ||
|
||
const contentType = headers['content-type'] || '' | ||
|
||
await this.s3.Upload(data, path, contentType) | ||
this.logger.Info('process compress image success', { | ||
category: 'compress-image', | ||
}) | ||
return size | ||
} catch (error: any) { | ||
this.logger.Error( | ||
'process compress image failed: ' + error.message, | ||
{ category: 'compress-image' } | ||
) | ||
throw error | ||
} | ||
} | ||
|
||
private async send(path: string, body: object) { | ||
try { | ||
const { data } = await axios.post( | ||
this.config.generator_file.url + '/' + path, | ||
body | ||
) | ||
|
||
return data.data | ||
} catch (err: any) { | ||
const message = err.message | ||
|
||
this.logger.Error(message, { | ||
category: FileGenerator.name, | ||
}) | ||
|
||
throw new error(err.status, message) | ||
} | ||
} | ||
} | ||
|
||
export default FileGenerator |
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 |
---|---|---|
@@ -1,7 +1,3 @@ | ||
import path from 'path' | ||
|
||
export const CustomPathFile = (newPath: string, file: any) => { | ||
const ext = path.extname(file.filename) | ||
if (!ext) file.filename = file.filename + path.extname(file.originalname) | ||
return `${newPath}/${file.filename}` | ||
return `${newPath}/${file.originalname}` | ||
} |
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
Oops, something went wrong.