Exemplo n.º 1
0
        public static async Task <string> InflateURLAsync(string urlshort)
        {
            //url short has form something like this:
            //aros057.azurewebsites.net/api/FunctionRedirect?UrlShort=Y
            //the last bit is the token to look in the DB.
            urlshort = urlshort.Substring(urlshort.LastIndexOf("=") + 1);

            //prepare tables
            CloudTable     tableUrl          = Common.CreateTable(Common.tableNameURL);
            string         partitionKey      = urlshort.Substring(0, 1);
            TableOperation retrieveOperation = TableOperation.Retrieve <UrlEntity>(partitionKey, urlshort);
            TableResult    result            = await tableUrl.ExecuteAsync(retrieveOperation);

            UrlEntity entity = result.Result as UrlEntity;

            if (entity == null || string.IsNullOrEmpty(entity.UrlFull))
            {
                throw new Exception("Bad link");
            }
            else
            {
                return(entity.UrlFull);
            }
        }
Exemplo n.º 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            //string UrlFull = req.Query["UrlFull"]; //cant use this inbuild function because there might be an & symbol, terminating the parameter
            string UrlFull = null;
            string qry     = req.QueryString.Value;
            string param   = "UrlFull=";
            int    start   = qry.IndexOf(param);

            if (start >= 0)
            {
                UrlFull = qry.Substring(start + param.Length); //manually search for the full URL
                //append 'http' protocol if none is specified
                if (!UrlFull.Contains("://"))
                {
                    UrlFull = "http://" + UrlFull;
                }
            }
            if (string.IsNullOrEmpty(UrlFull))
            {
                return(new BadRequestObjectResult("No input full URL"));
            }

            try
            {
                //prepare tables
                CloudTable tableUrl = Common.CreateTable(Common.tableNameURL);

                //index
                CloudTable tableIndex = Common.CreateTable(Common.tableNameIndex);

                IndexEntity IERetrieved = await Common.RetrieveEntityUsingPointQueryAsync(tableIndex, "0", "index");

                ulong index;
                if (IERetrieved == null)
                {
                    //not yet initialised
                    index = 0;
                }
                else
                {
                    // use stored value
                    index = Convert.ToUInt64(IERetrieved.Index);
                }


                string UrlShort = Common.uLongToBase62(index);
                index++;
                //now save index to the table. Ensure this does not fail, same index can't possibly be used again in case of an exception
                IndexEntity IE       = new IndexEntity(index.ToString());
                var         IndexRes = await Common.InsertOrMergeEntityAsync(tableIndex, IE);

                //insert URL pair to the DB
                UrlEntity UrlMap = new UrlEntity(UrlShort, UrlFull);
                UrlMap = (UrlEntity)await Common.InsertOrMergeEntityAsync(tableUrl, UrlMap);

                string result = req.Host.Value + "/api/FunctionRedirect?UrlShort=" + UrlShort;
                return(new OkObjectResult(result));
            }
            catch
            {
                return(new StatusCodeResult(500));
            }
        }