Skip to content

Commit

Permalink
add try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
navyxie committed Jul 28, 2016
1 parent 877c9a0 commit 6396038
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 58 deletions.
122 changes: 66 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,81 +34,91 @@ var extList = ['.BMP', '.JPG', '.JPEG', '.PNG', '.GIF'];

function formatUrl (filePath, url, options) {
var cssDirname = path.dirname(filePath);

// options
var modify = options.modify; // function you can modify return url before prepend or append
var prepend = options.prepend;
var append = options.append;
var skip = options.skip || [];
var outputImage = options.outputImage; // output images
var outputImage_path = options.outputImage_path || './.gulp_dist_output_images'; // output images filepath
var relative_root = options.staticfile_relative_website_rootpath || ''; //static filepath relative absolute filepath's physical position. (eg: /images/demo.png, absoulte filepath is /Users/Navy/Desktop/code/demo/assets/images/demo.png, process.cwd() is /Users/Navy/Desktop/code/demo, so the relative_root is assets)
var processPath = options.processPath || process.cwd();

var formattedUrl = url; // image origin filepath
var imgAbsolutePath = formattedUrl; // image absolute filepath
try {
//if images url is data:base64 , continue
if (formattedUrl.indexOf('data:') === 0) {
return formattedUrl;
}
// if images url is netword file , continue
if (/^(http|https|ftp|\/\/)/gi.test(formattedUrl)) {
return formattedUrl;
}

//if images url is data:base64 , continue
if (formattedUrl.indexOf('data:') === 0) {
return formattedUrl;
}
// if images url is netword file , continue
if (/^(http|https|ftp|\/\/)/gi.test(formattedUrl)) {
return formattedUrl;
}
//skip when url contain skip character
for (var i = 0 ; i < skip.length ; i++) {
if (formattedUrl.indexOf(skip[i]) !== -1) {
return formattedUrl;
}
}

if (_.indexOf(extList, path.extname(formattedUrl).toUpperCase()) === -1) {
return formattedUrl;
}
// skip when static file not in extList
if (_.indexOf(extList, path.extname(formattedUrl).toUpperCase()) === -1) {
return formattedUrl;
}

// image filepath is relative to website root
if (/^(\/)/gi.test(formattedUrl)) {
imgAbsolutePath = path.join(processPath, relative_root, formattedUrl);
} else {
// image filepath is a relative path
imgAbsolutePath = path.resolve(cssDirname, formattedUrl);
}
// image filepath is relative to website root
if (/^(\/)/gi.test(formattedUrl)) {
imgAbsolutePath = path.join(processPath, relative_root, formattedUrl);
} else {
// image filepath is a relative path
imgAbsolutePath = path.resolve(cssDirname, formattedUrl);
}

// images filename info
var imgExt = path.extname(formattedUrl);
var imgFileFullName = path.basename(formattedUrl);
var imgFileName = path.basename(formattedUrl, imgExt);
// image filepath relative website root path.
var img_relative_website_root_path = path.dirname(path.relative(relative_root, path.relative(processPath, imgAbsolutePath)));

// calculate image file crc32 value
var crc32 = bufferCrc32.unsigned(syncFileToBuffer(imgAbsolutePath));

// get image size info
var imgWH = imageSize(imgAbsolutePath);

//rename image, the format is ('imgFileName' + '_' + 'imageWidth' + '_' + 'imageHeight' + 'imageCrc32Value' + 'imageExt');
formattedUrl = imgFileName + '_' + imgWH.width + '_' + imgWH.height + '.' + crc32 + imgExt;

// if need to output match's images, do it.
if (outputImage) {
var outputBasename = path.basename(formattedUrl);
var outputDirname = path.join(outputImage_path, img_relative_website_root_path);
var outputFilepath = path.join(processPath, outputDirname, outputBasename);
console.info('output image file from: ', imgAbsolutePath, ' to: ', outputFilepath);
// copy image form imgAbsolutePath to outputFilepath
vfs.src(imgAbsolutePath).pipe(rename({basename:path.basename(outputFilepath, imgExt)})).pipe(vfs.dest(path.dirname(outputFilepath)));
}
// images filename info
var imgExt = path.extname(formattedUrl);
var imgFileFullName = path.basename(formattedUrl);
var imgFileName = path.basename(formattedUrl, imgExt);
// image filepath relative website root path.
var img_relative_website_root_path = path.dirname(path.relative(relative_root, path.relative(processPath, imgAbsolutePath)));
// calculate image file crc32 value
var crc32 = bufferCrc32.unsigned(syncFileToBuffer(imgAbsolutePath));

// get image size info
var imgWH = imageSize(imgAbsolutePath);

//rename image, the format is ('imgFileName' + '_' + 'imageWidth' + '_' + 'imageHeight' + 'imageCrc32Value' + 'imageExt');
formattedUrl = imgFileName + '_' + imgWH.width + '_' + imgWH.height + '.' + crc32 + imgExt;

// if need to output match's images, do it.
if (outputImage) {
var outputBasename = path.basename(formattedUrl);
var outputDirname = path.join(outputImage_path, img_relative_website_root_path);
var outputFilepath = path.join(processPath, outputDirname, outputBasename);
console.info('output image file from: ', imgAbsolutePath, ' to: ', outputFilepath);
// copy image form imgAbsolutePath to outputFilepath
vfs.src(imgAbsolutePath).pipe(rename({basename:path.basename(outputFilepath, imgExt)})).pipe(vfs.dest(path.dirname(outputFilepath)));
}

// mofify url
if (_.isFunction(modify)) {
formattedUrl = modify(formattedUrl, filePath, '/' + img_relative_website_root_path, {hash: crc32, width: imgWH.width, height: imgWH.height, orgin_filename: imgFileFullName});
}
// mofify url
if (_.isFunction(modify)) {
formattedUrl = modify(formattedUrl, filePath, '/' + img_relative_website_root_path, {hash: crc32, width: imgWH.width, height: imgWH.height, orgin_filename: imgFileFullName});
}

// prepend string
if (typeof prepend === 'string') {
formattedUrl = prepend + formattedUrl;
}
// prepend string
if (typeof prepend === 'string') {
formattedUrl = prepend + formattedUrl;
}

// append string
if (typeof append === 'string') {
formattedUrl += append;
}
// append string
if (typeof append === 'string') {
formattedUrl += append;
}
} catch (e) {

}
return formattedUrl;
}

Expand Down
5 changes: 3 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('gulp-custom-css-urls', function() {
},
outputImage: true,
ext: 'jade',
// skip: ['{'],
outputImage_path: './.test_dist_img'
}))
.pipe(vfs.dest('./.test_dist_jade'))
Expand All @@ -81,7 +82,7 @@ describe('gulp-custom-css-urls', function() {
return path.join(imageRelativeWebsiteRootPath, imageRelativePath);
},
outputImage: true,
ext: 'jade',
ext: 'html',
outputImage_path: './.test_dist_img'
}))
.pipe(vfs.dest('./.test_dist_jade'))
Expand All @@ -91,6 +92,6 @@ describe('gulp-custom-css-urls', function() {
execSync("rm -r " + path.join(process.cwd(), './.test_dist_img'));
execSync("rm -r " + path.join(process.cwd(), './.test_dist_jade'));
done();
},3000);
},5000);
})
});
2 changes: 2 additions & 0 deletions test/views/test.jade
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
#header
img(src="/images/example.png")
#body
img(src="/images/example{{test}}.png")
img(src="/images/example{test}.png")
#footer
img(src='/images/example.png')

0 comments on commit 6396038

Please sign in to comment.