Giter Site home page Giter Site logo

strapi-provider-upload-cloudflare-2's Introduction

strapi-provider-upload-cloudflare-2

npm version npm downloads

Non-Official Cloudflare Images and Video Upload Provider for Strapi

This has only been tested with Strapi 4.

Installation

# using yarn
yarn add strapi-provider-upload-cloudflare-2

# using npm
npm install strapi-provider-upload-cloudflare-2

Configurations

Your configuration is passed down to the provider.

See the using a provider documentation for information on installing and using a provider. And see the environment variables for setting and using environment variables in your configs.

Provider Configuration

./config/plugins.js

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: 'strapi-provider-upload-cloudflare-2',
      providerOptions: {
        accountId: env('STRAPI_UPLOAD_CLOUDFLARE_ACCOUNT_ID'),
        apiKey: env('STRAPI_UPLOAD_CLOUDFLARE_API_KEY'),
        variant: 'cms',
      },
    },
  },
  // ...
});

For the variant you can use one you have configured in Cloudflare Images. Or if not, can configure. In this example we used 'cms' as variant. Creating variants of your image is partly explained in the Cloudflare documentation. For this go to your Cloudflare Dash panel and click Images. Next click Variants. From that screen you can add a new variant. Please note that with the Fit Scale Down option your image size will keeps its original size as long as its not above the given width and height parameters.

Environment configuration

In the Provider Configuration there are the accountId and apiKey. Those environment variables should be set in the .env file . To find your Cloudflare accountId and apiKey, log into your Cloudflare dashboard and click "Images". On that page you should see "Account ID" under Developer Resources. For apiKey, if you click the "Use API" tab you'll see a link next to "API Token" to generate an apiKey. Click get started at Create Custom Token and give your token a name e.g. 'Cloudflare images Strapi'. In the permissions section after Account select item choose "Cloudflare Images" with edit permission. Next add another one and after Account select item choose "Stream" with edit permission.

./.env

STRAPI_UPLOAD_CLOUDFLARE_ACCOUNT_ID=<place your Cloudflare Account ID here>
STRAPI_UPLOAD_CLOUDFLARE_API_KEY=<place your Cloudflare apiKey here>

Security Middleware Configuration

Due to the default settings in the Strapi Security Middleware you will need to modify the contentSecurityPolicy settings to properly see thumbnail previews in the Media Library. You should replace strapi::security string with the object bellow instead as explained in the middleware configuration documentation.

./config/middlewares.js

module.exports = [
  // ...
  {
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          'connect-src': ["'self'", 'https:'],
          'img-src': ["'self'", 'data:', 'blob:', 'imagedelivery.net'],
          'media-src': ["'self'", 'data:', 'blob:', 'imagedelivery.net'],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  // ...
];

Optional

By default Strapi comes with Responsive formats and provides large, medium and small sizes automatically which also will be uploaded to Cloudflare images. This will count through your Cloudflare images plan by the limit of the amount of images. Because the variants in cloudflare images comes included in the plan it is recommended to turn off the responsive formats. To turn this off open your Strapi admin dashboard and go to settings at global settings choose Media Library. In this screen you can turn off Responsive friendly upload and Size optimization since this is handled by Cloudflare images as well. Please note Strapi still comes with a thumbnail image variant which is also uploaded automatically to Cloudflare images.

Resources

Links

strapi-provider-upload-cloudflare-2's People

Contributors

sparkinson avatar sanjay-io avatar jurjendevries avatar svezyridis avatar savvas-mettle avatar

Stargazers

Mike avatar Max F. Findel avatar

strapi-provider-upload-cloudflare-2's Issues

Warning: The upload provider "strapi-provider-upload-cloudflare-2" doesn't implement the uploadStream function. Strapi will fallback on the upload method. Some performance issues may occur.

When running npm run develop, we get the following warning :

Warning: The upload provider "strapi-provider-upload-cloudflare-2" doesn't implement the uploadStream function. Strapi will fallback on the upload method. Some performance issues may occur.

We created the api key with ACCOUNT CLOUDFLARE IMAGES & ACCOUNT STREAMS with edit more in the cloudflare dashboard.

Video uploading to CloudFlare is broken.

Hello!

First of all I would like to say a lot of thanks and appreciation for developing this open source plugin.
It is working perfectly with images, but unfortunately, it seems video uploading to Cloudflare is broken.

I think it is implemented using this example: https://developers.cloudflare.com/stream/uploading-videos/upload-video-file/#nodejs-example

For some reason it now returns 2 responses and current implementation onAfterResponse resolves Promise before uploading is finished. https://github.com/mettle-studio/strapi-provider-upload-cloudflare-2/blob/main/lib/uploadVideo.js#L29

As possible quick fix solution I would suggest saving onAfterResponse result to closure variable and resolve video uploading promise on onSuccess event.
image

It might be I made somewhere incorrect configuration. Let's understand the problem together, please.

Thanks!

variant unclear and error: Cannot read property 'substr' of undefined

I am trying Cloudflare Images for storing the media. I might have do something wrong, because when uploading the first image I got error: Cannot read property 'substr' of undefined
TypeError: Cannot read property 'substr' of undefined
and a lot more .
I tested uploading an image with the account id and token from the command line and that works well. So on the side of cloudflare it seems fine. I think I made a mistake with adding / configuring the plugin.

Also it seems unclear from the readme what I have to do with the so-called 'variant' configuration. I used the default 'cms' from the example.

Strapi 4.1.12 (node v14.19.3)

Support for CustomID

Cloudflare supports custom IDs (so the ID of the URL is meaningful instead of "upload_ba7d23f8ddb581bcb1a9999a99990ff9" like you get now). Strapi supports giving you a hash which can be used, which includes the filename for easier readability/SEO.

Cloudinary plugin uses the hash already for this (see https://github.com/strapi/strapi/blob/main/packages/providers/upload-cloudinary/src/index.ts#L34

For docs, see: https://developers.cloudflare.com/images/cloudflare-images/upload-images/custom-id/

code change looks to be as simple as:

Turning this:

https://github.com/mettle-studio/strapi-provider-upload-cloudflare-2/blob/main/lib/uploadImage.js#L8

into

 var data = new FormData();
 data.append("file", file.stream ?? file.buffer, `${file.hash}${file.ext}`);
 data.append("id", file.hash);

(i.e. add the id as a specific field)

Any concerns? would you want this to be optional?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.