Beispiel #1
0
        public static Response HandleShortenUrl(HttpContext context, string requestedUrl)
        {
            var response = new Response();

            // Perform basic validation
            if (String.IsNullOrEmpty(requestedUrl))
            {
                response.isError      = true;
                response.errorMessage = "Url cannot be empty";
                return(response);
            }

            // Test our URL
            if (!Uri.TryCreate(requestedUrl, UriKind.Absolute, out Uri result))
            {
                response.isError      = true;
                response.errorMessage = "Could not understand URL.";
                return(response);
            }

            var url = result.ToString();

            // Ask for LiteDB and persist a short link
            var db    = context.RequestServices.GetService(typeof(ILiteDatabase)) as ILiteDatabase;
            var links = db.GetCollection <ShortLink>(BsonAutoId.Int32);
            // Temporary short link
            var entry = new ShortLink
            {
                Url = url
            };

            // Insert our short-link
            links.Insert(entry);

            var urlChunk    = entry.GetUrlChunk();
            var responseUri = $"{context.Request.Scheme}://{context.Request.Host}/test/{urlChunk}";

            response.isError    = false;
            response.shortenUrl = responseUri;
            return(response);
        }
Beispiel #2
0
        public static Task HandleRedirect(HttpContext context)
        {
            var db = context.RequestServices.GetService(typeof(ILiteDatabase)) as ILiteDatabase;

            var collection = db.GetCollection <ShortLink>(BsonAutoId.Int32);

            var path  = context.Request.Path.ToUriComponent().Replace("/test", "").Trim('/');
            var id    = ShortLink.GetId(path);
            var entry = collection.Find(p => p.Id == id).FirstOrDefault();

            if (entry != null)
            {
                context.Response.Redirect(entry.Url);
            }
            else
            {
                context.Response.Redirect("/");
            }

            return(Task.CompletedTask);
        }