Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
apikodeveloper committed Sep 21, 2022
0 parents commit 25bfc98
Show file tree
Hide file tree
Showing 14 changed files with 7,843 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.log
.DS_Store
node_modules
dist
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLinters/eslint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/strapi-provider-upload-s3-cloudfront.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.DS_Store
*.log
21 changes: 21 additions & 0 deletions LICENSE
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.
39 changes: 39 additions & 0 deletions README.md
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'),
},
},
},
});

```
66 changes: 66 additions & 0 deletions package.json
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"
}
}
89 changes: 89 additions & 0 deletions src/index.ts
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));
});
},
};
}
35 changes: 35 additions & 0 deletions tsconfig.json
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,
}
}
Loading

0 comments on commit 25bfc98

Please sign in to comment.