Browse Source

Add QR support

HonorLee 5 years ago
parent
commit
1897ba42f1
4 changed files with 41 additions and 15 deletions
  1. 2 0
      .gitignore
  2. 10 5
      config.js
  3. 27 10
      handler/api.js
  4. 2 0
      package.json

+ 2 - 0
.gitignore

@@ -1,3 +1,5 @@
 node_modules/
 log/
 database.js
+npm-debug.log
+build/

+ 10 - 5
config.js

@@ -3,6 +3,7 @@ global.URL      = require('url');
 global.FILE     = require('fs-extra');
 global.Format   = require('util').format;
 global.EJS      = require('ejs');
+global.QR       = require('qrious');
 
 //Core Paths
 global.LIBPATH      = ROOTPATH+'/core/lib';
@@ -11,16 +12,18 @@ global.HANDLERPATH  = ROOTPATH+'/handler';
 global.LOGPATH      = ROOTPATH+'/log',
 global.STATICPATH   = ROOTPATH+'/static';
 global.VIEWSPATH    = ROOTPATH+'/view';
+global.QRPATH       = STATICPATH+'/qrs';
 
 //Init Path
 FILE.mkdirsSync(LOGPATH);
+FILE.mkdirsSync(QRPATH);
 
 //Core Libs
-global.Logger = require(LIBPATH+'/Logger.js');
-global.ROUTER = require(LIBPATH+'/router.js');
-global.VIEW   = require(LIBPATH+'/view.js');
-global.STATIC = require(LIBPATH+'/static.js');
-global.JUMPER = require(HANDLERPATH+'/jumper.js');
+global.Logger    = require(LIBPATH+'/Logger.js');
+global.ROUTER    = require(LIBPATH+'/router.js');
+global.VIEW      = require(LIBPATH+'/view.js');
+global.STATIC    = require(LIBPATH+'/static.js');
+global.JUMPER    = require(HANDLERPATH+'/jumper.js');
 global.APIHandle = require(HANDLERPATH+'/api.js');
 
 global.CONFIG = {
@@ -38,3 +41,5 @@ global.COUNT = {
     link:0,
     user:0
 }
+
+global.SystemURL = 'http://l2s.ch/';

+ 27 - 10
handler/api.js

@@ -1,11 +1,12 @@
+'use strict'
 const query  = require("querystring");
 const base64 = require('js-base64').Base64;
 
 var API = {
     parse:function(req,res){
-        var url = req.url;
-        var URLParse = URL.parse(url,true);
-        var URLArr = URLParse.pathname.split('/');
+        let url = req.url;
+        let URLParse = URL.parse(url,true);
+        let URLArr = URLParse.pathname.split('/');
         //Only GET
         req.param = URLParse.query;
         // console.log(URLParse.query);
@@ -30,10 +31,12 @@ var API = {
                 if(req.param['key'] && req.param['key']!='' && req.param['url'] && req.param['url']!=''){
                     UserDB.findOne({key:req.param['key']},function(err,data){
                         if(!err && data){
-                            var oldLink = req.param['url'];
+                            let oldLink = req.param['url'];
+                            let qr = 0;
+                            if(req.param['qr'] && Number(req.param['qr'])==1) qr = 1;
                             if(req.param['base64'] && req.param['base64']==0){
                                 if(oldLink.match(/http(s)?:\/\//)){
-                                    MongoHandler.getlink(req.param['key'],oldLink,res);
+                                    MongoHandler.getlink(req.param['key'],oldLink,res,qr);
                                 }else{
                                     API._onError(ErrorMsg.URL_WRONG,res);
                                     return;
@@ -41,7 +44,7 @@ var API = {
                             }else{
                                 oldLink = base64.decode(oldLink);
                                 if(oldLink.match(/http(s)?:\/\//)){
-                                    MongoHandler.getlink(req.param['key'],oldLink,res);
+                                    MongoHandler.getlink(req.param['key'],oldLink,res,qr);
                                 }else{
                                     API._onError(ErrorMsg.URL_WRONG,res);
                                     return;
@@ -78,24 +81,26 @@ var API = {
 }
 
 var MongoHandler = {
-    getlink:function(key,url,res){
-        var baseLink;
+    getlink:function(key,url,res,qr){
+        let baseLink;
         baseLink = base64.encode(url);
         // console.log(baseLink,newLink);
         LinkDB.findOne({long:baseLink},function(err,oldOne){
             if(!err){
-                var data = {success:1,msg:'',short:'http://l2s.ch/',long:url};
+                let data = {success:1,msg:'',short:SystemURL,long:url};
                 if(oldOne){
                     data.short += oldOne.short;
+                    if(qr) data.qr = SystemURL+'static/qrs'+QRHandler.getQR(oldOne.short);
                     res.writeHead(200, {'Content-Type': 'text/json','Access-Control-Allow-Origin':'*'});
                     res.write(JSON.stringify(data));
                     res.end();
                 }else{
-                    var newLink,count = COUNT.link+1;
+                    let newLink,count = COUNT.link+1;
                     newLink = count.toString(36);
                     LinkDB.update({key:key,long:baseLink},{$set:{short:newLink}},{upsert:true},function(err,result){
                         if(!err && result){
                             data.short += newLink;
+                            if(qr) data.qr = SystemURL+'static/qrs'+QRHandler.getQR(newLink);
                             res.writeHead(200, {'Content-Type': 'text/json','Access-Control-Allow-Origin':'*'});
                             res.write(JSON.stringify(data));
                             res.end();
@@ -113,6 +118,18 @@ var MongoHandler = {
     }
 }
 
+var QRHandler = {
+    getQR:function(tagName){
+        let qrImgPath = '/'+tagName+'.png';
+        if(!FILE.exists(QRPATH+qrImgPath)){
+            let qr = new QR({value:SystemURL+'/'+tagName});
+            let qrData = qr.toDataURL().replace(/^data:image\/\w+;base64,/, "");
+            FILE.writeFileSync(QRPATH+qrImgPath,new Buffer(qrData, 'base64'),'binary');
+        }
+        return qrImgPath;
+    }
+}
+
 const ErrorMsg = {
     NONE:'',
     KEY_LOST    :'Key is missing',

+ 2 - 0
package.json

@@ -7,11 +7,13 @@
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   "dependencies": {
+    "canvas": "^1.6.2",
     "ejs": "^2.5.5",
     "fs-extra": "latest",
     "js-base64": "latest",
     "mime-types": "latest",
     "mongodb": "latest",
+    "qrious": "^2.2.0",
     "tracer": ">=0.8.0"
   },
   "engines": {