-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.log | ||
.DS_Store | ||
node_modules | ||
dist |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.DS_Store | ||
*.log |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Petro Liashchynskyi | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Why? | ||
|
||
This plugin is based on [https://www.npmjs.com/package/@strapi/provider-upload-aws-s3](https://www.npmjs.com/package/@strapi/provider-upload-aws-s3) with the ability to serve files from Cloudfront. Build with `@aws-sdk/client-s3`. | ||
|
||
# Installation | ||
|
||
```sh | ||
npm install @liashchynskyi/strapi-provider-upload-s3-cloudfront | ||
# or | ||
yarn add @liashchynskyi/strapi-provider-upload-s3-cloudfront | ||
``` | ||
|
||
# Configuration | ||
|
||
All available [configs](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/s3clientresolvedconfig.html). | ||
|
||
```ts | ||
// config/plugins.ts | ||
|
||
import { S3ConfigOptions } from '@liashchynskyi/strapi-provider-upload-s3-cloudfront' | ||
|
||
export default ({ env }) => ({ | ||
upload: { | ||
config: { | ||
provider: '@liashchynskyi/strapi-provider-upload-s3-cloudfront', | ||
providerOptions: { | ||
credentials: { | ||
accessKeyId: env('AWS_ACCESS_KEY_ID'), | ||
secretAccessKey: env('AWS_SECRET_ACCESS_KEY'), | ||
}, | ||
region: env('AWS_S3_BUCKET_LOCATION'), | ||
bucket: env('AWS_S3_BUCKET_PREFIX'), | ||
cdn: env('AWS_CLOUDFRONT_DOMAIN'), | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"scripts": { | ||
"start": "tsdx watch", | ||
"build": "tsdx build", | ||
"test": "tsdx test", | ||
"lint": "tsdx lint", | ||
"prepare": "tsdx build", | ||
"size": "size-limit", | ||
"analyze": "size-limit --why" | ||
}, | ||
"peerDependencies": {}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint" | ||
} | ||
}, | ||
"prettier": { | ||
"printWidth": 80, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
}, | ||
"name": "@liashchynskyi/strapi-provider-upload-s3-cloudfront", | ||
"author": "Petro Liashchynskyi", | ||
"keywords": [ | ||
"strapi", | ||
"aws", | ||
"s3", | ||
"cloudfront", | ||
"upload" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"directory": "git://github.com/liashchynskyi/strapi-provider-upload-s3-cloudfront.git" | ||
}, | ||
"module": "dist/strapi-provider-upload-s3-cloudfront.esm.js", | ||
"size-limit": [ | ||
{ | ||
"path": "dist/strapi-provider-upload-s3-cloudfront.cjs.production.min.js", | ||
"limit": "10 KB" | ||
}, | ||
{ | ||
"path": "dist/strapi-provider-upload-s3-cloudfront.esm.js", | ||
"limit": "10 KB" | ||
} | ||
], | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^8.1.0", | ||
"husky": "^8.0.1", | ||
"size-limit": "^8.1.0", | ||
"tsdx": "^0.14.1", | ||
"tslib": "^2.4.0", | ||
"typescript": "^4.8.3" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
S3Client, | ||
S3ClientConfig, | ||
PutObjectCommand, | ||
PutObjectCommandInput, | ||
DeleteObjectCommand, | ||
DeleteObjectCommandInput, | ||
} from '@aws-sdk/client-s3'; | ||
|
||
export type S3ConfigOptions = S3ClientConfig & { | ||
cdn?: string; | ||
bucket: string; | ||
actionOptions?: { | ||
upload?: PutObjectCommandInput; | ||
uploadStream?: PutObjectCommandInput; | ||
delete?: DeleteObjectCommandInput; | ||
}; | ||
}; | ||
|
||
export function init(config: S3ConfigOptions) { | ||
const S3 = new S3Client(config); | ||
|
||
const upload = ( | ||
file: any, | ||
customParams: PutObjectCommandInput | Object = {} | ||
) => { | ||
const path = file.path ? `${file.path}/` : ''; | ||
const Key = `${path}${file.hash}${file.ext}`; | ||
|
||
const uploadCommand = new PutObjectCommand({ | ||
Bucket: config.bucket, | ||
Key, | ||
Body: file.stream || Buffer.from(file.buffer, 'binary'), | ||
ACL: 'public-read', | ||
ContentType: file.mime, | ||
...customParams, | ||
}); | ||
|
||
return new Promise((resolve, reject) => { | ||
S3.send(uploadCommand) | ||
.then(() => { | ||
if (config.cdn) { | ||
file.url = `${config.cdn}/${Key}`; | ||
} else { | ||
file.url = `https://${config.bucket}.s3.${config.region}.amazonaws.com/${Key}`; | ||
} | ||
|
||
resolve(file); | ||
}) | ||
.catch((err) => reject(err)); | ||
}); | ||
}; | ||
|
||
return { | ||
uploadStream( | ||
file: any, | ||
customParams: PutObjectCommandInput | Object = config?.actionOptions | ||
?.uploadStream ?? {} | ||
) { | ||
return upload(file, customParams); | ||
}, | ||
upload( | ||
file: any, | ||
customParams: PutObjectCommandInput | Object = config?.actionOptions | ||
?.upload ?? {} | ||
) { | ||
return upload(file, customParams); | ||
}, | ||
delete( | ||
file: any, | ||
customParams: DeleteObjectCommandInput | Object = config?.actionOptions | ||
?.delete ?? {} | ||
) { | ||
return new Promise((resolve, reject) => { | ||
const path = file.path ? `${file.path}/` : ''; | ||
|
||
S3.send( | ||
new DeleteObjectCommand({ | ||
Key: `${path}${file.hash}${file.ext}`, | ||
Bucket: config.bucket, | ||
...customParams, | ||
}) | ||
) | ||
.then((data) => resolve(data)) | ||
.catch((err) => reject(err)); | ||
}); | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs | ||
"include": ["src", "types"], | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"lib": ["dom", "esnext"], | ||
"importHelpers": true, | ||
// output .d.ts declaration files for consumers | ||
"declaration": true, | ||
// output .js.map sourcemap files for consumers | ||
"sourceMap": true, | ||
// match output dir to input dir. e.g. dist/index instead of dist/src/index | ||
"rootDir": "./src", | ||
// stricter type-checking for stronger correctness. Recommended by TS | ||
"strict": true, | ||
// linter checks for common issues | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
// noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
// use Node's module resolution algorithm, instead of the legacy TS one | ||
"moduleResolution": "node", | ||
// transpile JSX to React.createElement | ||
"jsx": "react", | ||
// interop between ESM and CJS modules. Recommended by TS | ||
"esModuleInterop": true, | ||
// significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS | ||
"skipLibCheck": true, | ||
// error out if import and file system have a casing mismatch. Recommended by TS | ||
"forceConsistentCasingInFileNames": true, | ||
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc` | ||
"noEmit": true, | ||
} | ||
} |