11import * as S3 from 'aws-sdk/clients/s3' ;
22import * as SecretsManager from 'aws-sdk/clients/secretsmanager' ;
3+ import * as SSM from 'aws-sdk/clients/ssm' ;
34import * as HttpErrors from 'http-errors' ;
45import * as Koa from 'koa' ; // http://koajs.cn
56import * as bodyParser from 'koa-bodyparser' ;
@@ -8,9 +9,13 @@ import * as Router from 'koa-router';
89import * as sharp from 'sharp' ;
910import config from './config' ;
1011import debug from './debug' ;
11- import { bufferStore , getProcessor , parseRequest } from './default' ;
12+ import { bufferStore , getProcessor , parseRequest , setMaxGifSizeMB , setMaxGifPages } from './default' ;
13+ import * as is from './is' ;
1214import { IHttpHeaders , InvalidArgument } from './processor' ;
1315
16+ const ssm = new SSM ( { region : config . region } ) ;
17+ const smclient = new SecretsManager ( { region : config . region } ) ;
18+
1419const DefaultBufferStore = bufferStore ( ) ;
1520const app = new Koa ( ) ;
1621const router = new Router ( ) ;
@@ -47,6 +52,12 @@ router.post('/images', async (ctx) => {
4752
4853router . get ( [ '/' , '/ping' ] , async ( ctx ) => {
4954 ctx . body = 'ok' ;
55+
56+ try {
57+ await setMaxGifLimit ( ) ;
58+ } catch ( err : any ) {
59+ console . error ( err ) ;
60+ }
5061} ) ;
5162
5263router . get ( [ '/debug' , '/_debug' ] , async ( ctx ) => {
@@ -162,11 +173,6 @@ function bypass() {
162173 throw new HttpErrors [ 403 ] ( 'Please visit s3 directly' ) ;
163174}
164175
165- // Create a Secrets Manager client
166- const smclient = new SecretsManager ( {
167- region : config . region ,
168- } ) ;
169-
170176async function getSecretFromSecretsManager ( ) {
171177 // Load the AWS SDK
172178 const secretName = config . secretName ;
@@ -178,4 +184,21 @@ async function getHeaderFromSecretsManager() {
178184 const secretString = secret . SecretString ! ;
179185 const keypair = JSON . parse ( secretString ) ;
180186 return keypair [ 'X-Client-Authorization' ] ;
187+ }
188+
189+ async function setMaxGifLimit ( ) {
190+ if ( config . configJsonParameterName ) {
191+ const data = await ssm . getParameter ( { Name : config . configJsonParameterName } ) . promise ( ) ;
192+ if ( data . Parameter ) {
193+ const configJson = JSON . parse ( data . Parameter . Value ?? '{}' ) ;
194+ const maxGifSizeMB = configJson . max_gif_size_mb ;
195+ if ( is . number ( maxGifSizeMB ) ) {
196+ setMaxGifSizeMB ( maxGifSizeMB ) ;
197+ }
198+ const maxGifPages = configJson . max_gif_pages ;
199+ if ( is . number ( maxGifPages ) ) {
200+ setMaxGifPages ( maxGifPages ) ;
201+ }
202+ }
203+ }
181204}
0 commit comments