public IActionResult Index([FromQuery] string k) { // Create the return object QueryResult queryResult = new QueryResult() { URL = null }; // Return empty if no key specified in query string if (k == null || k == string.Empty) { return(View(queryResult)); } // Make a connection to Cassandra and get a session CassandraConnection cassandraConnection = new CassandraConnection(); ISession csSession = cassandraConnection.GetSession(); URLLookup urlLookup = new URLLookup(csSession); // Get the actual long URL if (!urlLookup.LookupURL(k, out string url)) { // Return NotFound page if key doesn't exist return(NotFound()); } // Attache the URL to the result object queryResult.URL = url; // Return result return(View(queryResult)); }
public object Index([FromForm] string url) { // Create object to return APIResult result = new APIResult() { Status = (int)APIStatus.Failure, Message = "", URL = url, Shortcut = string.Empty, Popularity = 0 }; // Get the URL's SHA512 & SHA256 hash string sha512 = SHA512Hash.GetSHA512Hash(url); string sha256 = SHA256Hash.GetSHA256Hash(url); // Open up a connection to Cassandra CassandraConnection csConnection = new CassandraConnection(); // Get connection session ISession csSession = csConnection.GetSession(); // Lookup database and return the URL's signature if it exists SignatureLookup signatureLookup = new SignatureLookup(csSession); if (signatureLookup.LookupSignature(sha512, sha256, out string signature, out long hits)) { result.Shortcut = this.MakeShortcut(signature); result.Popularity = hits; result.Status = (int)APIStatus.Success; return(Json(result)); } // Get total URL count from the running service long id = 0; try { const string BOT = "<~BOT~>"; const string EOT = "<~EOT~>"; const string COMMAND_COUNT = "COUNT"; string message = string.Format("{0}{1}{2}", BOT, COMMAND_COUNT, EOT); string ip = "127.0.0.1"; int port = 7079; AsyncClientSocket asyncClientSocket = new AsyncClientSocket(); asyncClientSocket.Transmit(ip, port, message, out string response); //SyncClientSocket.Transmit(ip, port, message, out string response); if (response == string.Empty) { result.Message = "Service Unavailable!"; return(Json(result)); } id = long.Parse(response); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } // Prepare dictionary char[] dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; // Get signature signature = BaseN.ChangeBase(id, dictionary); // Unique signature is now set result.Shortcut = this.MakeShortcut(signature); // Insert the new URL into the database URLInsertion urlInsertion = new URLInsertion(csSession); if (urlInsertion.InsertURL(url, signature, sha512, sha256)) { result.Popularity = 1; result.Status = (int)APIStatus.Success; } return(Json(result)); }