{"size": 1132, "ext": "js", "max_stars_repo_path": "lib/imagine-file.js", "max_stars_repo_name": "brewster/imagine-file", "max_stars_repo_stars_event_min_datetime": "2018-10-12T22:41:19.000Z", "max_stars_repo_stars_event_max_datetime": "2018-10-12T22:41:19.000Z", "max_issues_repo_path": "lib/imagine-file.js", "max_issues_repo_name": "brewster/imagine-file", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/imagine-file.js", "max_forks_repo_name": "brewster/imagine-file", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.1020408163, "max_line_length": 75, "alphanum_fraction": 0.6139575972}
"use strict";

var crypto = require('crypto');
var fs = require('fs');
var winston = require('winston');
var Uploader = require('./imagine-file/uploader');
var Downloader = require('./imagine-file/downloader');

var ImagineFile = function (config) {
  config = config || {};
  this.path = config.path || './data/';
  this.configure();
};

ImagineFile.prototype = {

  ready: function () {
    return !!this.configured;
  },

  uploader: function (key) {
    var path = this.path + key;
    return new Uploader(path);
  },

  downloader: function (key) {
    var path = this.path + key;
    return new Downloader(path);
  },

  configure: function () {
    // Create the folder for storing images if it doesn't already exist
    var that = this;
    fs.mkdir(this.path, function (error) {
      // Disregard errors that complain about a pre-existing folder
      if (error && error.code !== 'EEXIST') {
        // Log all other errors
        winston.error('error creating image folder', { code: error.code });
      } else {
        // All good
        that.configured = true;
      }
    });
  }

};

module.exports = ImagineFile;
