all files / js.node.blurry/lib/ Blurry.js

84.62% Statements 33/39
100% Branches 22/22
100% Functions 8/8
84.62% Lines 33/39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                                                                                                                                                                                                         
/**
 * @fileOverview Blurry base64 image Url and SVG image generator.
 * @author Simon Gattner <npm@0x38.de>
 * @license MIT
 * @version 1.0.0
 */
const fs = require('fs');
const path = require('path');
 
module.exports = class Blurry {
  /**
   * @class Blurry
   * @classdesc Blurry base64 image Url and SVG image generator.
   * @param {object} options The blurify options.
   * @param {string} options.file The image file to blurify.
   * @param {string} options.type The image type to blurify.
   * @param {number} options.width The image width to blurify.
   * @param {number} options.height The image height to blurify.
   * @param {string} options.template The SVG Template file to use for blurify.
   */
  constructor(options) {
    this.options = options || {};
    this.file = this.options.file;
    this.type = this.options.type || 'jpeg';
    this.width = this.options.width || 1140;
    this.height = this.options.height || 640;
    this.base64File = this.makeBase64File();
    this.base64FileUrl = this.makeBase64FileUrl();
    this.template = this.makeTemplate();
    this.svg = this.makeBase64Svg();
    this.base64SvgUrl = this.makeBase64SvgUrl();
  }
  /**
   * Make blurry base64 File.
   * @function Blurry.makeBase64File
   * @return {string} The base64 File.
   */
  makeBase64File() {
    try {
      if (this.file !== undefined) {
        return fs.readFileSync(this.file, 'base64');
      }
    } catch (error) {
      return undefined;
    }
  }
  /**
   * Make blurry base64 File Url.
   * @function Blurry.makeBase64FileUrl
   * @return {string} The base64 File Url.
   */
  makeBase64FileUrl() {
    try {
      if (this.base64File !== undefined) {
        return 'data:image/' + this.type +
          ';base64,' + this.base64File;
      }
    } catch (error) {
      return undefined;
    }
  }
  /**
   * Make blurry Template.
   * @function Blurry.makeTemplate
   * @return {string} The Template.
   */
  makeTemplate() {
    try {
      if (this.options.template) {
        return fs.readFileSync(this.options.template);
      } else {
        return fs.readFileSync(path.join(__dirname, 'blurry.tmpl'));
      }
    } catch (error) {
      return undefined;
    }
  }
  /**
   * Make blurry base64 Svg.
   * @function Blurry.makeBase64Svg
   * @return {string} The base64 Svg.
   */
  makeBase64Svg() {
    try {
      if (this.template !== undefined && this.base64FileUrl !== undefined) {
        return this.template
          .toString()
          .replace('{{width}}', this.width)
          .replace('{{height}}', this.height)
          .replace('{{base64FileUrl}}', this.base64FileUrl);
      }
    } catch (error) {
      return undefined;
    }
  }
  /**
   * Make blurry base64 Svg Url.
   * @function Blurry.makeBase64SvgUrl
   * @return {string} The base64 Svg Url.
   */
  makeBase64SvgUrl() {
    try {
      if (this.svg !== undefined) {
        return 'data:image/svg+xml;base64,' +
          new Buffer(this.svg).toString('base64');
      }
    } catch (error) {
      return undefined;
    }
  }
  /**
   * Get blurry base64 SVG Url.
   * @function Blurry.getUrl
   * @return {string} The base64 SVG Url.
   */
  getUrl() {
    try {
      if (this.base64SvgUrl !== undefined) {
        return this.base64SvgUrl;
      }
    } catch (error) {
      return undefined;
    }
  }
  /**
   * Get blurry SVG Image.
   * @function Blurry.getSvg
   * @return {string} The SVG Image.
   */
  getSvg() {
    return this.svg;
  }
};