bremen_short_url/getGifShortUrl.ashx

173 lines
6.2 KiB
Plaintext

<%@ WebHandler Language="C#" Class="getGifShortUrl" %>
using System;
using System.Web;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web.Services.Protocols;
using System.Security.Cryptography;
using System.Text;
using System.Web.SessionState;
using System.Data;
using System.Collections.Generic;
using System.IO.Compression;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
public class getGifShortUrl : IHttpHandler {
public void ProcessRequest (HttpContext context) {
result objRet = new result();
DataContractJsonSerializer json = new DataContractJsonSerializer(objRet.GetType());
context.Response.ContentType = "application/json;charset=utf-8";
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
string appID = (context.Request["id"] == null) ? "" : context.Request["id"].ToString();
string appKey = (context.Request["key"] == null) ? "" : context.Request["key"].ToString();
string url = (context.Request["url"] == null) ? "" : context.Request["url"].ToString();
string descript = (context.Request["txt"] == null) ? "" : context.Request["txt"].ToString();
string gifUrl = (context.Request["gifurl"] == null) ? "" : context.Request["gifurl"].ToString();
string project_string = string.Format("select * from projects where project_apiId = '{0}' and project_apiKey = '{1}' and project_isRevoke = 'N' ", appID.Replace(";",""), appKey.Replace(";", ""));
autoBindDataTable tbProject = new autoBindDataTable(project_string);
if (tbProject.dataRows.Count == 0) {
objRet.ret = "no";
objRet.err_code = "0001";
objRet.message = "此專案不存在或已過期!";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (url.ToLower().IndexOf("http") < 0) {
objRet.ret = "no";
objRet.err_code = "0002";
objRet.message = "無網址傳入!";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (gifUrl.ToLower().IndexOf("http") < 0) {
objRet.ret = "no";
objRet.err_code = "0003";
objRet.message = "無gif連結網址傳入!";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
string project_uid = tbProject.dataRows[0]["project_uid"].ToString();
string sourceURL = (context.Request.UrlReferrer == null) ? "" : context.Request.UrlReferrer.Host.ToString();
string sourceIP = globalClass.GetIPAddress();
Boolean urlCheck = false;
Boolean ipCheck = false;
string urlFind = string.Format("select * from projectApiDomain where project_uid = '{0}' and projectApiDomain_type = 'domain' and projectApiDomain_address = '{1}'", project_uid, sourceURL);
autoBindDataTable tbUrlFind = new autoBindDataTable(urlFind);
if (tbUrlFind.dataRows.Count > 0) {
urlCheck = true;
}
string ipFind = string.Format("select * from projectApiDomain where project_uid = '{0}' and projectApiDomain_type = 'ip' and projectApiDomain_address = '{1}'", project_uid, sourceIP);
autoBindDataTable tbIpFind = new autoBindDataTable(ipFind);
if (tbIpFind.dataRows.Count > 0) {
ipCheck = true;
}
if (urlCheck == false && ipCheck == false) {
objRet.ret = "no";
objRet.err_code = "0003";
objRet.message = "呼叫的來源Domain或IP不在允許清單內! 目前來源ip或domain為[" + sourceIP + ", " + sourceURL + "]";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
descript = "(WebApi)[" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "] " + descript;
if (descript.Length > 90)
{
descript = descript.Substring(0, 90);
}
string short_Code = globalClass.CreateCaseRandomCode(7);
Boolean isCheck = false;
string checkString = string.Format("select * from url where url_code Collate SQL_Latin1_General_CP1_CS_AS = '{0}'", short_Code);
autoBindDataTable checkSQL = new autoBindDataTable(checkString);
while (isCheck == false) {
if (checkSQL.dataRows.Count == 0)
{
isCheck = true;
break;
}
else {
short_Code = globalClass.CreateCaseRandomCode(7);
}
checkString = string.Format("select * from url where url_code Collate SQL_Latin1_General_CP1_CS_AS = '{0}'", short_Code);
checkSQL = new autoBindDataTable(checkString);
}
urlBase shortUrlObj = new urlBase();
shortUrlObj.project_uid = project_uid;
shortUrlObj.url_Code = short_Code;
shortUrlObj.url_directToUrl = url;
shortUrlObj.url_gifTag = "Y";
shortUrlObj.url_gifLink = gifUrl;
shortUrlObj.url_descript = descript;
shortUrlObj.url_createUid = "webapi";
DataRow rowShort = checkSQL.newRow;
checkSQL.dataRows.Add(rowShort);
rowShort["url_uid"] = globalClass.CreateRandomCode(32);
rowShort["project_uid"] = project_uid;
rowShort["url_descript"] = descript;
rowShort["url_directToUrl"] = url;
rowShort["url_Code"] = short_Code;
rowShort["url_gifTag"] = "Y";
rowShort["url_gifLink"] = gifUrl;
rowShort["url_createUid"] = "webapi";
try
{
checkSQL.updateDataTable();
objRet.ret = "yes";
objRet.short_url = shortUrlObj.url_shortUrl;
}
catch (Exception ex) {
objRet.ret = "no";
objRet.err_code = "004";
objRet.message = ex.Message;
}
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
public class result
{
public string ret = "no";
public string err_code = "0000";
public string message = "";
public string short_url = "";
}
public bool IsReusable {
get {
return false;
}
}
}