Browse files

0.1.0 added fs support (need refactoring)

  • Loading branch information...
1 parent 01d0d48 commit 3ec346291fea5261b1baef45ae5615695931c8d6 @Panya committed Aug 31, 2011
View
2 VERSION
@@ -1 +1 @@
-Durilka v0.0.5
+Durilka v0.1.0
View
52 lib/durilka.js
@@ -58,28 +58,25 @@ function encodeImages(src, callback) {
}
}
-function normalizeUri(uri, baseUrl) {
- var normUri = '';
+function encodeImage(uri, callback) {
+ var normUri = '',
+ reHTTP = /^https?/;
if (uri.length && uri.substr(0, 5) != 'data:') {
if (uri.substr(0, 2) == '//') {
normUri = 'http:' + uri;
- } else if (/^https?/.test(uri)) {
+ } else if (reHTTP.test(uri)) {
normUri = uri;
} else {
normUri = baseUrl ? url.resolve(baseUrl, uri) : uri;
}
- }
- return normUri;
-}
-
-function encodeImage(uri, callback) {
- uri = normalizeUri(uri, baseUrl);
- if (!uri) {
+ reHTTP.test(normUri) ? getViaHTTP(normUri, callback) : getViaFS(normUri, callback);
+ } else {
callback && callback();
- return;
}
+}
+function getViaHTTP(uri, callback) {
var urlObj = url.parse(uri),
opts = {
host: urlObj.hostname,
@@ -120,6 +117,39 @@ function encodeImage(uri, callback) {
});
}
+function getViaFS(uri, callback) {
+ try {
+ var fd = fs.openSync(uri, 'r');
+ } catch (err) {
+ console.error('Error while reading file: %s [%s]', err, uri);
+ callback && callback();
+ return;
+ }
+
+ var buf = new Buffer(4), type, data;
+ fs.readSync(fd, buf, 0, 4, 0);
+
+ if (0x47 == buf[0] && 0x49 == buf[1] && 0x46 == buf[2]) {
+ type = 'gif';
+ } else if (0x50 == buf[1] && 0x4E == buf[2] && 0x47 == buf[3]) {
+ type = 'png';
+ } else if (0xff == buf[0] && 0xd8 == buf[1]) {
+ type = 'jpeg';
+ } else {
+ console.error('Unsupported file type [%s]', uri);
+ callback && callback();
+ return;
+ }
+
+ fd = fs.readFileSync(uri);
+ if (Buffer.byteLength(fd.toString('binary')) > maxSize * 1024) {
+ console.warn('Image size greater than %d kilobytes [%s]', maxSize, uri);
+ } else {
+ data = 'data:image/' + type + ';base64,' + fd.toString('base64');
+ }
+ callback && callback(data);
+}
+
function printFile(filename) {
console.log(fs.readFileSync(__dirname.slice(0, __dirname.lastIndexOf('/')) + '/' + filename).toString());
}
View
2 package.json
@@ -1,7 +1,7 @@
{
"name" : "durilka",
"description" : "Simple tool for making data URIs from images in CSS files",
- "version" : "0.0.5",
+ "version" : "0.1.0",
"author" : "Mikhail Korepanov <panyakor@gmail.com>",
"homepage": "http://github.com/panya/durilka",
"repository" : {
View
BIN sample/images/bg/body.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN sample/images/folder/folder/some2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN sample/images/some.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
23 sample/style.css
@@ -0,0 +1,23 @@
+body {
+ background:#fff url(images/bg/body.png);
+}
+
+.some-class {
+ background:url(images/some.gif);
+}
+
+.some-class2 {
+ background:url(images/folder/folder/some2.jpg);
+}
+
+.some-class3 {
+ background:url(http://mailstatic.yandex.net/neo2/3.10.13/static/blocks/b-mail-icon/_type/b-mail-icon_replied.gif);
+}
+
+.some-class4 {
+ background:url(//mailstatic.yandex.net/neo2/3.10.13/static/blocks/b-mail-icon/_type/b-mail-icon_replied.gif)
+}
+
+.some-class5 {
+ background:url(data:image/gif;base64,R0lGODdhLwAeAPcAAAAAAAAAMwAAZgAAmQAAzAAA/wAzAAAzMwAzZgAzmQAzzAAz/wBmAABmMwBmZgBmmQBmzABm/wCZAACZMwCZZgCZmQCZzACZ/wDMAADMMwDMZgDMmQDMzADM/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMzADMzMzMzZjMzmTMzzDMz/zNmADNmMzNmZjNmmTNmzDNm/zOZADOZMzOZZjOZmTOZzDOZ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