AdamZikmund / strapi-provider-upload-digitalocean

Digital ocean spaces provider for Strapi upload plugin
MIT License
13 stars 47 forks source link

Hello Adam, #3

Open merijnponzo opened 5 years ago

merijnponzo commented 5 years ago

Thanks for your provider, I'm trying to add some resizing, before uploading create two variants

The files are resized, and uploaded to the space so that part is working But then I got this error within strapi.

/root/.pm2/logs/api-error.log last 15 lines:
0|api      |      'thumb',
0|api      |      'is',
0|api      |      'required',
0|api      |      'url',
0|api      |      'Path',
0|api      |      'url',
0|api      |      'is',
0|api      |      'required' ] }
0|api      | 
0|api      |   Error: Cannot wrap non-Error object
0|api      |       at Object.exports.assert (/usr/lib/node_modules/strapi/node_modules/hoek/lib/index.js:740:11)
0|api      |       at Object.exports.wrap (/usr/lib/node_modules/strapi/node_modules/boom/lib/index.js:95:10)
0|api      |       at strapi.app.use (/usr/lib/node_modules/strapi/lib/middlewares/boom/index.js:40:20)
0|api      |       at process._tickCallback (internal/process/next_tick.js:68:7)
0|api      | 

One file is working , so it has something to do with the resolvers I guess.

This is has nothing to do with your script, but maybe you find it an interesting feature an can you help me.

Thanks!

The addition is based on my script: https://www.npmjs.com/package/strapi-provider-upload-local-resize

"use strict";

/**
 * Module dependencies
 */

/* eslint-disable import/no-unresolved */
/* eslint-disable no-unused-vars */
// Public node modules.
const _ = require("lodash");
const AWS = require("aws-sdk");
const Jimp = require('jimp');

module.exports = {
  provider: "digitaloceanresize",
  name: "Digitalocean Spaces service resize",
  auth: {
    key: {
      label: "Key",
      type: "text"
    },
    secret: {
      label: "Secret",
      type: "text"
    },
    region: {
      label: "Region",
      type: "enum",
      values: ["nyc3", "sgp1", "ams3", "sfo2"]
    },
    space: {
      label: "Space",
      type: "text"
    }
  },
  init: config => {
    const S3 = new AWS.S3({
      accessKeyId: config.key,
      secretAccessKey: config.secret,
      sslEnabled: true,
      endpoint: `${config.region}.digitaloceanspaces.com`,
      params: {
        Bucket: config.space
      }
    });

    return {
      upload: file => {
        return new Promise((resolve, reject) => {
          //resize function
          function resize(file,variant){
            return Jimp.read(file.buffer)
             .then(image => {
               if(variant.scaleType!=="resize"){
                //cover image
                image.cover(variant.maxSize,variant.maxSize)
              }else{
                //width, height
                image.resize(Jimp.AUTO, variant.maxSize)
              }

               image.getBuffer(Jimp.AUTO, (err, buffer) => { 
               const path = file.path ? `${file.path}/` : "";
               S3.upload(
               {
                  Key: `${path}${variant.prefix}${file.hash}${file.ext}`,
                  Body: new Buffer(buffer, "binary"),
                  ACL: "public-read",
                  ContentType: file.mime
               },
                (err, data) => {
                  if (err) {
                    return reject(err);
                  }
                  //file.url = data.Location;
                  file[variant.attributes] = data.Location
                  return true
                }
              );
              }); 
             })
             .catch(err => {
               return reject(err);
             });
            }

            //define variants, props should be defined in plugins/uploads/models/File.settings.json
          let variants = [
            {
                maxSize:900,
                scaleType:"resize",
                prefix:"large_",
                quality:70,
                attributes:'url'
            },
            {
                maxSize:500,
                scaleType:"cover",
                prefix:"thumb_",
                quality:70,
                attributes:'thumb'
            }
            ]
            //resize image in two variants
            var promise = resize(file,variants[0]);
            promise
            .then(function() {
              return resize(file,variants[1]);
            })
            .then(function() {
              return resolve()
            })
            .catch(err => {
              return reject(err);
            });
        });
      },
      delete: file => {
        return new Promise((resolve, reject) => {
          const path = file.path ? `${file.path}/` : "";
          S3.deleteObject(
            {
              Key: `${path}${file.hash}${file.ext}`
            },
            (err, data) => {
              if (err) {
                return reject(err);
              }
              resolve();
            }
          );
        });
      }
    };
  }
};
numfin commented 5 years ago

try to wrap your errors in new Error()