public ActionResult <Url> Create(IFormFile file) { Url mainurl = null; if (file != null && ModelState.IsValid) { var urls = _context.Urls.ToList(); var Extension = Path.GetExtension(file.FileName); if (AllExtensions.Contains(Extension.ToLower()) && file.Length < 20 * 1024 * 1024) { var RelativeImagePath = "./UploadedFiles/" + file.FileName; using (var filestream = new FileStream(RelativeImagePath, FileMode.Create)) { file.CopyTo(filestream); } if (!urls.Exists(url => url.FullUrl == RelativeImagePath)) { mainurl = new Url { FullUrl = RelativeImagePath, Extension = Extension, ShortUrl = _shortener.GetUrl(), Created = DateTime.Now }; _context.Urls.Add(mainurl); _context.SaveChanges(); } else { mainurl = _context.Urls.Where(w => w.FullUrl == RelativeImagePath).FirstOrDefault(); } } else { return(BadRequest()); } return(mainurl); } return(BadRequest()); }
public IActionResult Index(string id) { string shortUrl = string.Empty; string result = string.Empty; Shortener shortener = new Shortener(_configuration["connectionString"], _configuration["salt"]); if (string.IsNullOrEmpty(id)) { //here we just show the Index try { shortener.TestConnect(); result = "OK"; } catch (Exception ex) { result = ex.Message; } ViewData["host"] = "https://" + Request.Host; ViewData["result"] = result; ViewData["connectionString"] = _configuration["connectionString"]; return(View()); } else { try { if (id.Length <= 12 && !string.IsNullOrEmpty(shortUrl = shortener.GetUrl(id))) { return(Redirect(shortUrl)); } } catch { } } Response.StatusCode = 404; //ViewData["id"] = Uri.EscapeDataString(id); return(View("NotFound")); }