Files
KART_KON/REPORT_API_ver6/Controllers/UriAPI.cs

91 lines
2.7 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Data;
using System.Net;
using System.Text;
using System.Web;
using REPORT_API_ver6.Services;
using REPORT_API_ver6.Models;
namespace REPORT_API_ver6.Controllers
{
[Route("api/v1/uri")]
[ApiController]
public class UriAPI : ControllerBase
{
[Route("short")]
[HttpPost]
public IActionResult ShortURL
(
[FromHeader(Name = "token")] string? token,
[FromHeader(Name = "X-API-Key")] string? xApiKey,
[FromHeader] string skey,
[FromBody] ShortURLParam shortURLParam
)
{
HttpContextAccessor hcAccessor = new HttpContextAccessor();
hcAccessor.HttpContext.Response.ContentType = "text/plain";
var configuration = Common.GetConfiguration();
var value = configuration.GetSection("X-API-Key")[xApiKey ?? ""];
UserInfo? userInfo = String.IsNullOrEmpty(token)? null : Security.ValidateToken(token);
if (userInfo != null || !String.IsNullOrEmpty(value))
{
string okey = "REPORT";
if(userInfo.ID == "KON" || userInfo.ID == "KIWI" || userInfo.ID == "NEWSSAK")
{
okey = userInfo.ID;
}
else if(!String.IsNullOrEmpty(value))
{
okey = value;
}
PkeyInfo pkeyInfo = new PkeyInfo(skey);
Project project = new Project();
DataTable proejctInfo = project.getProjectInfo(pkeyInfo.Pkey, pkeyInfo.isKiwi);
if(proejctInfo.Rows.Count > 0)
{
int mode = shortURLParam.Mode == "P" ? 1 : 0;
string returnString = "https://kart.kric.com/External/" + URI.GetURIKey(shortURLParam.RealURI, mode, skey, proejctInfo.Rows[0]["title"].ToString(), okey);
string cLogContent = "{ \"REAL_URI\": \"" + shortURLParam.RealURI + "\",\"MODE\": \"" + shortURLParam.Mode + "\", \"COPY_URI\": \"" + returnString + "\" }";
Log.InsertSiteLog(userInfo.ID, pkeyInfo.Pkey, "URL Copy", cLogContent);
Response.StatusCode = (int)HttpStatusCode.OK;
return Content(returnString);
}
}
Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return Content(null);
}
[Route("short/{urikey}")]
[HttpGet]
public IActionResult ShortURL([FromHeader] string token, string urikey)
{
HttpContextAccessor hcAccessor = new HttpContextAccessor();
hcAccessor.HttpContext.Response.ContentType = "application/json";
UserInfo userInfo = Security.ValidateToken(token);
if (userInfo != null)
{
DataTable realUritable = URI.GetRealURL(urikey, true);
if (realUritable.Rows.Count > 0)
{
Response.StatusCode = (int)HttpStatusCode.OK;
return Content(Common.GetJsonFromObject(realUritable));
}
}
Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return Content(null);
}
}
}