Skip to content

Commit

Permalink
Merge pull request #19 from ellukitas-123/main
Browse files Browse the repository at this point in the history
Added bucket pool option
  • Loading branch information
tilman authored Jun 2, 2024
2 parents d7df37d + 55745d2 commit 91deb46
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ yarn add strapi-provider-cloudflare-r2
# using npm
npm install strapi-provider-cloudflare-r2 --save

# using pnpm
# using pnpm
pnpm add strapi-provider-cloudflare-r2
```



## Configuration

- `provider` defines the name of the provider
Expand Down Expand Up @@ -52,6 +50,12 @@ module.exports = ({ env }) => ({
* Check the cloudflare docs for the setup: https://developers.cloudflare.com/r2/data-access/public-buckets/#enable-public-access-for-your-bucket
*/
cloudflarePublicAccessUrl: env("CF_PUBLIC_ACCESS_URL"),
/**
* Sets if all assets should be uploaded in the root dir regardless the strapi folder.
* It is useful because strapi sets folder names with numbers, not by user's input folder name
* By default it is false
*/
pool: false,
},
actionOptions: {
upload: {},
Expand Down
17 changes: 10 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ function removeLeadingSlash(str) {
return str.replace(/^\//, "");
}

function getPathKey(file) {
function getPathKey(file, pool = false) {
const filePath = file.path ? `${file.path}/` : "";
const path =
file.folderPath && file.folderPath !== "/"
? `${removeLeadingSlash(file.folderPath)}/${filePath}`
: filePath;
let path = filePath;
if (!pool) {
path =
file.folderPath && file.folderPath !== "/"
? `${removeLeadingSlash(file.folderPath)}/${filePath}`
: filePath;
}

const Key = `${path}${file.hash}${file.ext}`;
return { path, Key };
Expand Down Expand Up @@ -72,7 +75,7 @@ module.exports = {
const upload = (file, customParams = {}) =>
new Promise((resolve, reject) => {
// upload file on S3 bucket
const { Key } = getPathKey(file);
const { Key } = getPathKey(file, config.pool);
S3.upload(
{
Key,
Expand Down Expand Up @@ -131,7 +134,7 @@ module.exports = {
delete(file, customParams = {}) {
return new Promise((resolve, reject) => {
// delete file on S3 bucket
const { Key } = getPathKey(file);
const { Key } = getPathKey(file, config.upload);
S3.deleteObject(
{
Key,
Expand Down

0 comments on commit 91deb46

Please sign in to comment.