Code coverage report for kobold-core/lib/storageAdapter/fileStorageAdapter.js

Statements: 70.49% (43 / 61)      Branches: 37.5% (6 / 16)      Functions: 76.92% (30 / 39)      Lines: 70.49% (43 / 61)      Ignored: none     

All files » kobold-core/lib/storageAdapter/ » fileStorageAdapter.js
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373      1 1   1 1   1                                       1                   21   21   21 21 21   21 21                                                                                                                                                                                 4 4 12                         18                       11                       3                     3                     4                     1 1                       1 1                           1                     1 1                       1 1                           1                       1 1                       1 1                           1                       1 1                       1 1                         1 1                             1  
// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
 
var path = require('path');
var fs = require('fs');
 
var Promise = require('promise');
var PNGImage = require('pngjs-image');
 
var StorageAdapter = require('./storageAdapter');
 
 
/**
 * File storage adapter
 *
 * @class FileStorageAdapter
 * @extends StorageAdapter
 * @constructor
 * @param {Object} options
 * @param {String} options.path
 * @param {String} options.approvedFolderName
 * @param {String} options.buildFolderName
 * @param {String} options.highlightFolderName
 *
 * @property {string} _path
 * @property {string} _approvedFolderName
 * @property {string} _buildFolderName
 * @property {string} _highlightFolderName
 */
var FileStorageAdapter = StorageAdapter.extend(
 
	/** @lends FileStorageAdapter.prototype */
	{
		/**
		 * Initializes the source-adapter
		 *
		 * @method initialize
		 */
		initialize: function () {
			this.__super();
 
			this._path = this._options.path;
 
			this._approvedFolderName = this._options.approvedFolderName || 'approved';
			this._buildFolderName = this._options.buildFolderName || 'build';
			this._highlightFolderName = this._options.highlightFolderName || 'highlight';
 
			this.setPromise(this.getPromise().then(function () {
				this._prepareFolder();
			}.bind(this)).then(null, function (err) {
				console.log(err.stack);
			}));
		},
 
 
		/**
		 * Prepares the output folder
		 *
		 * @method _prepareFolder
		 * @private
		 */
		_prepareFolder: function () {
			if (!fs.existsSync(this._getApprovedPath())) {
				fs.mkdirSync(this._getApprovedPath());
			}
			if (!fs.existsSync(this._getBuildPath())) {
				fs.mkdirSync(this._getBuildPath());
			}
			if (!fs.existsSync(this._getHighlightPath())) {
				fs.mkdirSync(this._getHighlightPath());
			}
		},
 
		/**
		 * Reads an image and returns a promise
		 *
		 * @method _readImage
		 * @param {string} path
		 * @return {Promise} With {PNGImage} Image
		 * @private
		 */
		_readImage: function (path) {
 
			return new Promise(function (resolve, reject) {
				var image = PNGImage.readImage(path, function (err) {
					if (err) {
						reject(err);
					} else {
						resolve(image);
					}
				});
			});
		},
 
		/**
		 * Writes an image and returns a promise
		 *
		 * @method _writeImage
		 * @param {string} path
		 * @param {PNGImage} image
		 * @return {Promise}
		 * @private
		 */
		_writeImage: function (path, image) {
 
			return new Promise(function (resolve, reject) {
				image.writeImage(path, function (err) {
					if (err) {
						reject(err);
					} else {
						resolve();
					}
				});
			});
		},
 
		/**
		 * Reads a directory an returns all files found in the folder
		 *
		 * @method _readDir
		 * @param {string} path
		 * @returns {string[]}
		 * @private
		 */
		_readDir: function (path) {
			return fs.readdirSync(path);
		},
 
		/**
		 * Reads a directory and filters for png files, removing the extensions
		 *
		 * @method _readDirAndFilter
		 * @param {string} path
		 * @returns {string[]}
		 * @private
		 */
		_readDirAndFilter: function (path) {
			var files = this._readDir(path);
			return files.filter(this._pngFilter).map(function (filename) {
				return filename.substr(0, filename.length - 4);
			});
		},
 
		/**
		 * List filter for png extensions
		 *
		 * @method _pngFilter
		 * @param {string} filename
		 * @return {boolean}
		 * @private
		 */
		_pngFilter: function (filename) {
			return (filename.substr(-4).toLowerCase() === '.png');
		},
 
 
		/**
		 * Gets the processing path
		 *
		 * @method _getPath
		 * @return {string}
		 * @private
		 */
		_getPath: function () {
			return this._path;
		},
 
 
		/**
		 * Gets the approved path
		 *
		 * @method _getApprovedPath
		 * @return {string}
		 * @private
		 */
		_getApprovedPath: function () {
			return path.join(this._getPath(), this._approvedFolderName);
		},
 
		/**
		 * Gets the build path
		 *
		 * @method _getBuildPath
		 * @return {string}
		 * @private
		 */
		_getBuildPath: function () {
			return path.join(this._getPath(), this._buildFolderName);
		},
 
		/**
		 * Gets the highlight path
		 *
		 * @method _getHighlightPath
		 * @return {string}
		 * @private
		 */
		_getHighlightPath: function () {
			return path.join(this._getPath(), this._highlightFolderName);
		},
 
 
		/**
		 * Gets a list of currently approve screen names
		 *
		 * @method getCurrentApprovedScreenNames
		 * @return {Promise} With {string[]} List of approved screen names
		 */
		getCurrentApprovedScreenNames: function () {
			return this.getPromise().then(function () {
				return this._readDirAndFilter(this._getApprovedPath());
			}.bind(this));
		},
 
		/**
		 * Gets a specific currently approved screen
		 *
		 * @method getCurrentApprovedScreen
		 * @param {string} name Name of approved screen
		 * @return {Promise} With {PNGImage} Approved screen
		 */
		getCurrentApprovedScreen: function (name) {
			return this.getPromise().then(function () {
				return this._readImage(path.join(this._getApprovedPath(), name + '.png'));
			}.bind(this));
		},
 
		/**
		 * Archives a specific currently approved screen
		 *
		 * @method archiveCurrentApprovedScreen
		 * @param {string} name Name of approved screen
		 * @param {PNGImage} image Screen to archive
		 * @return {Promise}
		 */
		archiveCurrentApprovedScreen: function (name, image) {
			// Do nothing
			return Promise.resolve();
		},
 
 
		/**
		 * Gets a list of approve screen names
		 *
		 * @method getApprovedScreenNames
		 * @return {Promise} With {string[]} List of approved screen names
		 */
		getApprovedScreenNames: function () {
			return this.getPromise().then(function () {
				return [];
			});
		},
 
		/**
		 * Gets a specific approved screen
		 *
		 * @method getApprovedScreen
		 * @param {string} name Name of approved screen
		 * @return {Promise} With {PNGImage} Approved screen
		 */
		getApprovedScreen: function (name) {
			return this.getPromise().then(function () {
				return undefined;
			});
		},
 
		/**
		 * Archives a specific approved screen
		 *
		 * @method archiveApprovedScreen
		 * @param {string} name Name of approved screen
		 * @param {PNGImage} image Screen to archive
		 * @return {Promise}
		 */
		archiveApprovedScreen: function (name, image) {
			// Do nothing
			return Promise.resolve();
		},
 
 
		/**
		 * Gets a list of build screen names
		 *
		 * @method getBuildScreenNames
		 * @return {string[]} List of build screen names
		 * @return {Promise} With {string[]} List of build screen names
		 */
		getBuildScreenNames: function () {
			return this.getPromise().then(function () {
				return this._readDirAndFilter(this._getBuildPath());
			}.bind(this));
		},
 
		/**
		 * Gets a specific build screen
		 *
		 * @method getBuildScreen
		 * @param {string} name Name of build screen
		 * @return {Promise} With {PNGImage}
		 */
		getBuildScreen: function (name) {
			return this.getPromise().then(function () {
				return this._readImage(path.join(this._getBuildPath(), name + '.png'));
			}.bind(this));
		},
 
		/**
		 * Archives a specific build screen
		 *
		 * @method archiveBuildScreen
		 * @param {string} name Name of build screen
		 * @param {PNGImage} image Screen to archive
		 * @return {Promise}
		 */
		archiveBuildScreen: function (name, image) {
			// Do nothing
			return Promise.resolve();
		},
 
 
		/**
		 * Gets a list of highlight screen names
		 *
		 * @method getHighlightScreenNames
		 * @return {string[]} List of highlight screen names
		 * @return {Promise} With {string[]} List of highlight screen names
		 */
		getHighlightScreenNames: function () {
			return this.getPromise().then(function () {
				return this._readDirAndFilter(this._getHighlightPath());
			}.bind(this));
		},
 
		/**
		 * Gets a specific highlight screen
		 *
		 * @method getHighlightScreen
		 * @param {string} name Name of highlight screen
		 * @return {Promise} With {PNGImage}
		 */
		getHighlightScreen: function (name) {
			return this.getPromise().then(function () {
				return this._readImage(path.join(this._getHighlightPath(), name + '.png'));
			}.bind(this));
		},
 
		/**
		 * Archives a specific highlight screen
		 *
		 * @method archiveHighlightScreen
		 * @param {string} name Name of highlight screen
		 * @param {PNGImage} image Screen to archive
		 * @return {Promise}
		 */
		archiveHighlightScreen: function (name, image) {
			return this.getPromise().then(function () {
				return this._writeImage(path.join(this._getHighlightPath(), name + '.png'), image);
			}.bind(this));
		}
	},
 
	{
		/**
		 * Type of class
		 *
		 * @property TYPE
		 * @type string
		 */
		TYPE: 'FileStorageAdapter'
	});
 
module.exports = FileStorageAdapter;