예제 #1
0
 public UrlResource GenerateShortUrl(UrlRequest urlRequest)
 {
     if (Validator.UrlValidator(urlRequest.Url))
     {
         string      shortUrl;
         Task <bool> check;
         do
         {
             shortUrl = RandomStringGenerator.GeneratRandomString(8);
             check    = dbContext.UrlResources.AnyAsync(foo => foo.ShortenUrl == shortUrl);
             check.Wait();
         } while(check.Result);
         UrlResource resource = new UrlResource
         {
             ShortenUrl = shortUrl,
             Url        = urlRequest.Url
         };
         dbContext.UrlResources.Add(resource);
         dbContext.SaveChanges();
         return(resource);
     }
     else
     {
         return(null);
     }
 }
예제 #2
0
        public string UrlFinder(string shortUrl)
        {
            string url   = null;
            bool   check = dbContext.UrlResources.Any(foo => foo.ShortenUrl == shortUrl);

            if (check)
            {
                UrlResource find = dbContext.UrlResources.Where(resource => resource.ShortenUrl == shortUrl).Single();
                url = find.Url;
            }
            return(url);
        }
예제 #3
0
        public string LengthenUrl(string url)
        {
            string resultado = string.Empty;
            UrlResource client = new UrlResource();
            //client.Endpoint= new System.ServiceModel.Description.ServiceEndpoint(new Sy
            // Shorten url according the parameter below.
            var response = client.Insert(
                new ShortenRequest { LongUrl = url });

            // Print short url. Ex: http://goo.gl/sOme
            Console.WriteLine(response.Id);
            resultado = response.Id;
            // Print long url. Ex: http://gshortener.codeplex.com
            Console.WriteLine(response.LongUrl);
            return resultado;
        }
        public IActionResult post([FromBody] UrlRequest request)
        {
            if (!new Regex(@"((\w)+\:\/\/)").IsMatch(request.Url))
            {
                request.Url = "http://" + request.Url;
            }
            UrlResource resource = shortUrlService.GenerateShortUrl(request);

            if (resource == null)
            {
                return(BadRequest());
            }
            else
            {
                return(Ok(resource));
            }
        }
예제 #5
0
        public async Task <IActionResult> PostAsync([FromBody] UrlResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var url     = _mapper.Map <UrlResource, Url>(resource);
            var results = await _urlService.AddAsync(url);

            if (!results.Success)
            {
                return(BadRequest(results.Message));
            }

            return(Ok(url));
        }
예제 #6
0
        public string LengthenUrl(string url)
        {
            string      resultado = string.Empty;
            UrlResource client    = new UrlResource();
            //client.Endpoint= new System.ServiceModel.Description.ServiceEndpoint(new Sy
            // Shorten url according the parameter below.
            var response = client.Insert(
                new ShortenRequest {
                LongUrl = url
            });

            // Print short url. Ex: http://goo.gl/sOme
            Console.WriteLine(response.Id);
            resultado = response.Id;
            // Print long url. Ex: http://gshortener.codeplex.com
            Console.WriteLine(response.LongUrl);
            return(resultado);
        }
예제 #7
0
 public Resource loadFileAsResource(String fileName)
 {
     try
     {
         Path     filePath = this.fileStorageLocation.resolve(fileName).normalize();
         Resource resource = new UrlResource(filePath.toUri());
         if (resource.exists())
         {
             return(resource);
         }
         else
         {
             throw new MyFileNotFoundException("File not found " + fileName);
         }
     }
     catch (MalformedURLException ex)
     {
         throw new MyFileNotFoundException("File not found " + fileName, ex);
     }
 }
 /// <summary>Constructs a new service.</summary>
 /// <param name="initializer">The service initializer.</param>
 public UrlshortenerService(Google.Apis.Services.BaseClientService.Initializer initializer)
     : base(initializer)
 {
     url = new UrlResource(this);
 }