var express = require('express');
var router = express.Router();
var request = require('superagent');
var db = require('../utils/dbUtils');
var config = require('../configs/config');
const CDN = 'https://static1.ioliu.cn/';

/* GET photo listing. */
router.get('/:photo', function(req, res, next) {
    var force = req.query.force || '';
    var photo = req.params.photo;
    var isAjax = !!req.headers['x-requested-with'];
    switch (force) {
        case 'like':
            if (isAjax) {
                var ck = req.cookies['likes'] || '';
                ck = ck.split('_');
                if (ck.indexOf(photo) > -1) {
                    res.json({
                        msg: '',
                        code: 200
                    });
                    return;
                }
                var sql = `update bing as a join (select likes,id from bing WHERE id='${photo}') as b on a.id=b.id set a.likes=(b.likes+1)`;
                db.commonQuery(sql, function(rows) {
                    var ret = {
                        msg: '',
                        code: 200
                    };
                    if (rows.affectedRows == 0) {
                        ret.msg = 'something happend.'
                    }
                    res.json(ret);
                });
            } else {}
            return;
            break;
        case 'download':
            var ua = req.get('User-Agent');
            if (!isAjax && !/(spider|bot)/ig.test(ua)) {
                var sql = `update bing as a join (select downloads,id from bing WHERE qiniu_url='${photo}') as b on a.id=b.id set a.downloads=(b.downloads+1)`;
                db.commonQuery(sql, function(rows) {});
                res.set({
                    'Content-Type': 'application/octet-stream',
                    'Content-Disposition': 'attachment; filename=' + encodeURI(`${photo}_1920x1080.jpg`)
                });
                try {
                    res.redirect(`${CDN}bing/${photo}_1920x1080.jpg`)
                } catch (error) {
                    console.log(error)
                }
                // request.get(`${CDN}bing/${photo}_1920x1080.jpg`)
                //     // .set({
                //     //     'User-Agent': ua,
                //     //     referer: 'https://bing.ioliu.cn'
                //     // })
                //     .pipe(res);
                //console.log(`${CDN}bing/${photo}_1920x1080.jpg`)
            } else {
                res.json({
                    code: 200,
                    msg: 'bad request'
                })
            }
            return;
            break;
    }

    var sql = `select id,title,attribute,description,copyright,qiniu_url as photo,city,country,continent,DATE_FORMAT(enddate, '%Y-%m-%d') as dt,likes,views,downloads,thumbnail_pic from bing 
            where qiniu_url='${photo}'`;
    if (isAjax) {
        res.send({
            code: 200,
            msg: 'bad request'
        });
    } else {
        // 修改展示量
        db.commonQuery(`update bing as a join (select views,id from bing WHERE qiniu_url='${photo}') as b on a.id=b.id set a.views=(b.views+1)`, function(rows) {});
        // 返回数据
        db.commonQuery(sql, function(rows) {
            if (rows.length > 0) {
                var doc = rows[0];
                doc['thumbnail'] = `${CDN}bing/${photo}_1920x1080.jpg`;
                if (force.indexOf('_') > -1) {
                    var rt = force.split('_');
                    doc['back_url'] = rt[0] === 'ranking' ? '/ranking?p=' + rt[1] : '/?p=' + rt[1];
                } else {
                    doc['back_url'] = '/';
                }
                res.render('detail', { doc: doc });
            } else {
                res.redirect(`/`);
            }
        });
    }
});
/**
 * 如果没有参数，则跳转到首页
 */
router.get('/', function(req, res, next) {
    res.redirect('/');
});

module.exports = router;
@@ -1990,31 +1990,26 @@
         
-    res.redirec
+request.ge
 t(%60$%7BCDN
@@ -2060,24 +2060,13 @@
     
-%7D catch (error) 
+.set(
 %7B%0D%0A 
@@ -2088,114 +2088,79 @@
     
-console.log(error)%0D%0A                %7D%0D%0A                // request.get(%60$%7BCDN%7Dbing/$%7Bphoto%7D_1920x1080.jpg%60)
+'User-Agent': ua,%0D%0A                    referer: 'https://bing.ioliu.cn'
 %0D%0A  
@@ -2177,24 +2177,16 @@
     
-//     // .set(%7B
+%7D).end()
 %0D%0A  
@@ -2203,103 +2203,54 @@
     
-//     //     'User-Agent': ua,%0D%0A                //     //     referer: 'https://bing.ioliu.cn'
+.pipe(res);%0D%0A                %7D catch (error) %7B
 %0D%0A  
@@ -2267,19 +2267,28 @@
     
-//
     
- // %7D
+consle.log(error
 )%0D%0A 
@@ -2306,26 +2306,9 @@
     
-//     .pipe(res);
+%7D
 %0D%0A  

change sence