bremen_short_url/BackEnd/api/urlData.ashx

441 lines
17 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<%@ WebHandler Language="C#" Class="urlData" %>
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 Facebook;
using System.Net;
using System.Collections.Specialized;
public class urlData : IHttpHandler, IReadOnlySessionState {
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 acceptEncoding = context.Request.Headers["Accept-Encoding"].ToString().ToUpperInvariant();
if (!String.IsNullOrEmpty(acceptEncoding))
{
if (acceptEncoding.Contains("GZIP"))
{
//输出流头部GZIP压缩
context.Response.AppendHeader("Content-encoding", "gzip");
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))
{
//输出流头部DEFLATE压缩
context.Response.AppendHeader("Content-encoding", "deflate");
context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
}
}
string method = (context.Request["method"] == null) ? "" : context.Request["method"].ToString();
string project_uid = (context.Request["project_uid"] == null) ? "" : context.Request["project_uid"].ToString();
string url_uid = (context.Request["url_uid"] == null) ? "" : context.Request["url_uid"].ToString();
string url_descript = (context.Request["url_descript"] == null) ? "" : context.Request["url_descript"].ToString();
string url_directToUrl = (context.Request["url_directToUrl"] == null) ? "" : context.Request["url_directToUrl"].ToString();
string url_Code = (context.Request["url_Code"] == null) ? "" : context.Request["url_Code"].ToString();
string url_gifTag = (context.Request["url_gifTag"] == null) ? "N" : context.Request["url_gifTag"].ToString();
string url_gifLink = (context.Request["url_gifLink"] == null) ? "" : context.Request["url_gifLink"].ToString();
string url_fbTag = (context.Request["url_fbTag"] == null) ? "N" : context.Request["url_fbTag"].ToString();
string url_fbTitle = (context.Request["url_fbTitle"] == null) ? "" : context.Request["url_fbTitle"].ToString();
string url_fbDescript = (context.Request["url_fbDescript"] == null) ? "" : context.Request["url_fbDescript"].ToString();
string url_fbPixel = (context.Request["url_fbPixel"] == null) ? "N" : context.Request["url_fbPixel"].ToString();
string url_fbPixelCode = (context.Request["url_fbPixelCode"] == null) ? "" : context.Request["url_fbPixelCode"].ToString();
string url_googleAds = (context.Request["url_googleAds"] == null) ? "N" : context.Request["url_googleAds"].ToString();
string url_googleAwConversionId = (context.Request["url_googleAwConversionId"] == null) ? "" : context.Request["url_googleAwConversionId"].ToString();
string url_playBtn = (context.Request["url_playBtn"] == null) ? "N" : context.Request["url_playBtn"].ToString();
string pic_data = (context.Request["pic_data"] == null) ? "" : context.Request["pic_data"].ToString();
string url_uploadFile = (context.Request["url_uploadFile"] == null) ? "N" : context.Request["url_uploadFile"].ToString();
string url_uploadFileClientName = (context.Request["url_uploadFileClientName"] == null) ? "" : context.Request["url_uploadFileClientName"].ToString();
string url_uploadFileLinkName = (context.Request["url_uploadFileLinkName"] == null) ? "" : context.Request["url_uploadFileLinkName"].ToString();
string url_uploadFileType = (context.Request["url_uploadFileType"] == null) ? "" : context.Request["url_uploadFileType"].ToString();
string url_photoId = "";
authPermission objAuth = new authPermission();
if (!objAuth.isLogin())
{
objRet.ret = "no";
objRet.err_code = "0001";
objRet.message = "尚未登入,請登入後使用";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (method == "")
{
objRet.ret = "no";
objRet.err_code = "0999";
objRet.message = "沒有Method無法使用";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
string errmsg = "";
if (method != "add") {
if (url_uid == "") {
errmsg += "沒有系統編號!\n";
}
}
if (errmsg.Length > 0) {
objRet.ret = "no";
objRet.err_code = "0005";
objRet.message = errmsg;
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (method == "get")
{
urlObj objUrl = new urlObj(url_uid);
objRet.ret = "yes";
objRet.data = objUrl;
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (method == "delete")
{
if (url_uid == "") {
errmsg += "沒有系統編號!\n";
objRet.ret = "no";
objRet.err_code = "0005";
objRet.message = errmsg;
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
string delQuotationString = string.Format("update url set url_isRevoke = 'Y' where project_uid = '{0}' and url_uid = '{1}'", project_uid, url_uid);
autoExecSQL objDel = new autoExecSQL(delQuotationString);
if (objDel.effectDataCount > 0)
{
objRet.ret = "yes";
}
else {
objRet.ret = "no";
objRet.err_code = "0033";
objRet.message = "無短網址被刪除,請重新整理後重新刪除!";
}
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (url_descript == "") {
errmsg += "沒有短網址說明!\n";
}
if (url_Code == "") {
errmsg += "沒有短網址碼!\n";
}
if (url_directToUrl == "") {
errmsg += "沒有目的地網址!\n";
}
if (project_uid == "") {
errmsg += "沒有專案代碼!\n";
}
if (url_gifTag == "Y") {
if (url_gifLink == "") {
errmsg += "沒有gif的網路連結位置!\n";
}
}
if (url_fbTag == "Y") {
if (pic_data == "")
{
errmsg += "沒有FB預覽圖片\n";
}
if (url_fbTitle == "")
{
errmsg += "沒有FB預覽標題!\n";
}
if (url_descript == "")
{
errmsg += "沒有FB預覽內容!\n";
}
}
if (errmsg.Length > 0) {
objRet.ret = "no";
objRet.err_code = "0005";
objRet.message = errmsg;
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (method == "add") {
string checkString = string.Format("select * from url where url_Code Collate SQL_Latin1_General_CP1_CS_AS = '{0}'", url_Code);
autoBindDataTable checkSQL = new autoBindDataTable(checkString);
if (checkSQL.dataRows.Count > 0) {
objRet.ret = "no";
objRet.err_code = "0006";
objRet.message = "此短網址已經被使用了,請換一組短網址碼再試一次!";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
url_uid = globalClass.CreateRandomCode(32);
url_photoId = globalClass.CreateRandomCode(36);
urlObj objUrl = new urlObj();
objUrl.url_uid = url_uid;
objUrl.project_uid = project_uid;
objUrl.url_descript = url_descript;
objUrl.url_directToUrl = url_directToUrl;
objUrl.url_Code = url_Code;
objUrl.url_playBtn = url_playBtn;
objUrl.url_fbTag = url_fbTag;
objUrl.url_gifTag = url_gifTag;
objUrl.url_gifLink = url_gifLink;
objUrl.url_fbTitle = url_fbTitle;
objUrl.url_fbDescript = url_fbDescript;
objUrl.url_fbPixel = url_fbPixel;
objUrl.url_fbPixelCode = url_fbPixelCode;
objUrl.url_googleAds = url_googleAds;
objUrl.url_googleAwConversionId = url_googleAwConversionId;
objUrl.url_createUid = objAuth.user_uid;
objUrl.url_photoId = url_photoId;
objUrl.url_uploadFile = url_uploadFile;
objUrl.url_uploadFileClientName = url_uploadFileClientName;
objUrl.url_uploadFileLinkName = url_uploadFileLinkName;
objUrl.url_uploadFileType = url_uploadFileType;
string picChange = "N";
JToken jsonObj;
string base64Img = "";
try
{
jsonObj = JValue.Parse(pic_data);
picChange = jsonObj[0]["meta"]["picChange"].ToString();
base64Img = jsonObj[0]["output"]["image"].ToString();
}
catch {
}
if (picChange == "Y")
{
//base64Img = jsonObj[0]["output"]["image"].ToString();
objUrl.pic_image = globalClass.Base64ToImage(base64Img);
//if (objUrl.pic_image.Width > 702) {
// float ratoF = 702f / float.Parse(objUrl.pic_image.Width.ToString());
// int newW = Convert.ToInt32(objUrl.pic_image.Width * ratoF);
// int newH = Convert.ToInt32(objUrl.pic_image.Height * ratoF);
// objUrl.pic_image = globalClass.resizeImage(objUrl.pic_image, newW, newH);
//}
}
try
{
objUrl.updateData();
objRet.data = objUrl;
objRet.ret = "yes";
}
catch (Exception ex)
{
objRet.ret = "no";
objRet.err_code = "9999";
objRet.message = ex.Message;
}
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (method == "edit") {
Boolean isChange = false;
urlObj objUrl = new urlObj(url_uid);
if (objUrl.url_Code == "") {
objRet.ret = "no";
objRet.err_code = "0009";
objRet.message = "無此url_uid資料!";
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
if (objUrl.url_gifLink != url_gifLink) {
isChange = true;
}
if (objUrl.url_fbTag != url_fbTag) {
isChange = true;
}
if (objUrl.url_playBtn != url_playBtn) {
isChange = true;
}
if (objUrl.url_directToUrl != url_directToUrl) {
isChange = true;
}
if (objUrl.url_fbTitle != url_fbTitle) {
isChange = true;
}
if (objUrl.url_fbDescript != url_fbDescript) {
isChange = true;
}
string base64Img = "";
string picChange = "N";
JToken jsonObj;
try
{
jsonObj = JValue.Parse(pic_data);
picChange = jsonObj[0]["meta"]["picChange"].ToString();
}
catch {
jsonObj = null;
}
if (picChange == "Y")
{
isChange = true;
base64Img = jsonObj[0]["output"]["image"].ToString();
objUrl.pic_image = globalClass.Base64ToImage(base64Img);
//if (objUrl.pic_image.Width > 702) {
// float ratoF = 702f / float.Parse(objUrl.pic_image.Width.ToString());
// int newW = Convert.ToInt32(objUrl.pic_image.Width * ratoF);
// int newH = Convert.ToInt32(objUrl.pic_image.Height * ratoF);
// objUrl.pic_image = globalClass.resizeImage(objUrl.pic_image, newW, newH);
//}
}
objUrl.url_descript = url_descript;
objUrl.url_directToUrl = url_directToUrl;
objUrl.url_playBtn = url_playBtn;
objUrl.url_gifTag = url_gifTag;
objUrl.url_gifLink = url_gifLink;
objUrl.url_fbTag = url_fbTag;
objUrl.url_fbTitle = url_fbTitle;
objUrl.url_fbDescript = url_fbDescript;
objUrl.url_fbPixel = url_fbPixel;
objUrl.url_fbPixelCode = url_fbPixelCode;
objUrl.url_googleAds = url_googleAds;
objUrl.url_googleAwConversionId = url_googleAwConversionId;
objUrl.url_uploadFileLinkName = url_uploadFileLinkName;
objUrl.url_uploadFile = url_uploadFile;
objUrl.url_uploadFileClientName = url_uploadFileClientName;
objUrl.url_uploadFileType = url_uploadFileType;
if (objUrl.updateData())
{
if (isChange) {
try
{
string AppID = "563584377383502";
string AppSecret = "0a8211ad839ec0ffbd5955ef3d4709ec";
WebClient wc = new WebClient(); //每次重新取一次token
string result = wc.DownloadString("https://graph.facebook.com/oauth/access_token?client_id=" + AppID + "&client_secret=" + AppSecret + "&grant_type=client_credentials");//自動取得發文授權碼
dynamic resultJson = JValue.Parse(result);
string access_token = resultJson["access_token"];
string url = string.Format("https://graph.facebook.com/?id={0}&scrape=true", HttpUtility.UrlEncode(objUrl.url_shortUrl));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
//request.ContentType = "application/x-www-form-urlencoded";
//必須透過ParseQueryString()來建立NameValueCollection物件之後.ToString()才能轉換成queryString
NameValueCollection postParams = System.Web.HttpUtility.ParseQueryString(string.Empty);
postParams.Add("access_token", access_token);
//Console.WriteLine(postParams.ToString());// 將取得"version=1.0&action=preserveCodeCheck&pCode=pCode&TxID=guid&appId=appId", key和value會自動UrlEncode
//要發送的字串轉為byte[]
byte[] byteArray = Encoding.UTF8.GetBytes(postParams.ToString());
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(byteArray, 0, byteArray.Length);
}//end using
//API回傳的字串
string responseStr = "";
//發出Request
using (WebResponse response = request.GetResponse())
{
using (StreamReader sr = new StreamReader(response.GetResponseStream(),Encoding.UTF8))
{
responseStr = sr.ReadToEnd();
}//end using
}
dynamic resultStrJson = JValue.Parse(responseStr);
}
catch
{
}
}
objRet.ret = "yes";
objRet.data = objUrl;
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
else {
objRet.ret = "no";
objRet.message = objUrl.update_result;
objRet.data = objUrl;
json.WriteObject(context.Response.OutputStream, objRet);
return;
}
}
}
public class result
{
public string ret = "no";
public string err_code = "0000";
public string message = "";
public urlObj data = new urlObj();
}
public bool IsReusable {
get {
return false;
}
}
}