Blame view

utils/helpers.js 3.25 KB
姜枫 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
'use strict';

const _ = require('lodash');

let cdnDomains = require('../config/dns-prefetch');

function cdnReplace(cdn, imgUrl) {
    if (cdn === 'qcloud') {
        Object.keys(cdnDomains).forEach(key => {
            if (imgUrl.indexOf(key) >= 0) {
                imgUrl = imgUrl.replace(key, cdnDomains[key]);
            }
        });
    }
    return imgUrl;
}

module.exports = {
htoooth authored
19
    image2: function(imageUrl, opts) {
姜枫 authored
20 21 22
        let cdn = _.get(opts, 'data.root.cdn');

        if (imageUrl && _.isString(imageUrl)) {
姜枫 authored
23
            let params = opts.hash || {};
姜枫 authored
24 25 26 27 28 29 30 31 32 33 34
            let urls = imageUrl.split('?');
            let query = urls[1] || '';
            let uri = urls[0];

            if (uri.indexOf('http:') === 0) {
                uri = uri.replace('http:', '');
            }

            uri = cdnReplace(cdn, uri);

            if (query) {
yyq authored
35 36
                query = query.replace(/{width}/g, params.w).replace(/{height}/g,
                    params.h).replace(/{mode}/g, (params.mode || 2));
姜枫 authored
37 38 39 40

                if (query.indexOf('imageView2') === 0) {
                    if (params.q && query.indexOf('/q/') > 0) {
                        query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
htoooth authored
41
                    } else if (params.q) {
姜枫 authored
42 43 44 45 46
                        query += '/q/' + params.q;
                    }
                } else if (query.indexOf('imageMogr2') === 0) {
                    if (params.q && query.indexOf('/quality/') > 0) {
                        query = query.replace(/\/quality\/\d+/g, '/quality/' + params.q);
姜枫 authored
47
                    } else if (params.q) {
姜枫 authored
48 49 50 51 52
                        query += '/quality/' + params.q;
                    }
                } else if (query.indexOf('imageView/') === 0) {
                    if (params.q && query.indexOf('/q/') > 0) {
                        query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
姜枫 authored
53
                    } else if (params.q) {
姜枫 authored
54 55 56 57 58 59 60 61 62 63
                        query += '/q/' + params.q;
                    }

                    if (params.mode) {
                        query = query.replace(/imageView\/\d{1}\//, 'imageView/' + params.mode + '/');
                    }
                }
            } else {
                query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
            }
周少峰 authored
64
            return uri + '?' + query;
姜枫 authored
65 66 67
        } else {
            return '';
        }
yyq authored
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    },
    ifand: function() {
        let args = Array.prototype.slice.call(arguments);
        let opt = args[args.length - 1];
        let isTrue = true;

        for (let i = 0; i < args.length - 1; i++) {
            if (!args[i]) {
                isTrue = false;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
王水玲 authored
86 87 88 89
    },
    htmlEncode: function(str) {
        const re = /(\r\n)|["\'<>]/g;
王水玲 authored
90
        str = str + '' || '';
王水玲 authored
91 92 93 94 95 96 97 98 99 100 101 102 103 104
        return str.replace(re, function(s) {
            switch (s) {
                case '"':
                    return '&quot;';
                case '\'':
                    return '&apos;';
                case '<':
                    return '&lt;';
                case '>':
                    return '&gt;';
                default:
                    return s;
            }
        });
姜枫 authored
105 106
    }
};