{"size": 1655, "ext": "js", "max_stars_repo_path": "imports/modules/server/s3.js", "max_stars_repo_name": "themeteorchef/enhanced-deployments-with-galaxy-pro", "max_stars_repo_stars_event_min_datetime": "2017-07-14T21:03:30.000Z", "max_stars_repo_stars_event_max_datetime": "2019-04-16T12:08:54.000Z", "max_issues_repo_path": "imports/modules/server/s3.js", "max_issues_repo_name": "themeteorchef/enhanced-deployments-with-galaxy-pro", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "imports/modules/server/s3.js", "max_forks_repo_name": "themeteorchef/enhanced-deployments-with-galaxy-pro", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.4615384615, "max_line_length": 127, "alphanum_fraction": 0.5250755287}
/* eslint-disable */

import { Meteor } from 'meteor/meteor';
import AWS from 'aws-sdk';
import moment from 'moment';

const { aws } = Meteor.settings.private;

AWS.config = new AWS.Config();

AWS.config.accessKeyId = aws.akid;
AWS.config.secretAccessKey = aws.sak;

const s3 = new AWS.S3();

const isValidFile = (file) => {
  const typeOkay = ['image/png', 'image/jpeg', 'image/jpg', 'image/svg', 'image/gif', 'image/svg+xml'].indexOf(file.type) > -1;
  const sizeOkay = file.size <= (2 * 1024 * 1024); // Max 2MB upload.
  return typeOkay && sizeOkay;
};

export default {
  putObject(file) {
    return new Promise((resolve, reject) => {
      if (file && !isValidFile(file)) reject('Only images less than 2MB are accepted, cowpoke. Try again.');
      if (file) {
        s3.putObject({
          Bucket: 'i-heart-meteor',
          Key: file.name,
          Body: new Buffer(file.data.split(',')[1], 'base64'),
          ACL: 'private',
          ContentEncoding: 'base64',
          ContentType: file.type,
        }, (error, response) => {
          if (error) {
            reject(error);
          } else {
            resolve(`https://s3.amazonaws.com/i-heart-meteor/${file.name}`);
          }
        });
      } else {
        resolve();
      }
    });
  },
  deleteObject(Key) {
    return new Promise((resolve, reject) => {
      if (Key) {
        s3.deleteObject({
          Bucket: 'i-heart-meteor',
          Key,
        }, Meteor.bindEnvironment((error) => {
          if (error) {
            reject(error);
          } else {
            resolve();
          }
        }));
      } else {
        resolve();
      }
    });
  },
}
