Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
var inherits = require('util').inherits;
17
var childProcess = require('child_process');
18
var fs = require('fs');
19
20
var ResourceLoader = require('./ResourceLoader');
21
var ImageResource = require('../resource/Image');
22
var MessageList = require('../MessageList');
23
var getImageSize = require('../parse/getImageSize');
24
25
26
/**
27
* @class Loads and parses __mocks__ / *.js files
28
*
29
* @extends {ResourceLoader}
30
*/
31
function ImageLoader(options) {
32
ResourceLoader.call(this, options);
33
}
34
inherits(ImageLoader, ResourceLoader);
35
ImageLoader.prototype.path = __filename;
36
37
ImageLoader.prototype.getResourceTypes = function() {
38
return [ImageResource];
39
};
40
41
ImageLoader.prototype.getExtensions = function() {
42
return ['.jpg', '.png', '.gif'];
43
};
44
45
46
/**
47
* Creates a new resource for a given path.
48
*
49
* @protected
50
* @param {String} path resource being built
51
* @param {ProjectConfiguration} configuration configuration for the path
52
* @param {Function} callback
53
*/
54
ImageLoader.prototype.loadFromPath =
55
function(path, configuration, callback) {
56
57
var image = new ImageResource(path);
58
var messages = MessageList.create();
59
image.id = path;
60
fs.readFile(path, function(err, buffer) {
61
image.networkSize = buffer.length;
62
var size = getImageSize(buffer);
63
if (size) {
64
image.width = size.width;
65
image.height = size.height;
66
}
67
callback(messages, image);
68
});
69
};
70
71
72
var re = /\.(jpg|gif|png)$/;
73
/**
74
* Only match __mocks__ / *.js files
75
* @static
76
* @param {String} filePath
77
* @return {Boolean}
78
*/
79
ImageLoader.prototype.matchPath = function(filePath) {
80
return re.test(filePath);
81
};
82
83
84
module.exports = ImageLoader;
85
86