Browse Source

- Moved TeknikEntities from global field to disposed local instances.

- Added additional logging/handling of errors.
- Added processed/total bytes for uploads, downloads, and encryption/decryption.
- Fixed paste CSS bundle using a script handler.
- Fixed bad js when viewing a vault
pull/111/head
Teknikode 5 years ago
parent
commit
e163e0ca8c
  1. 81
      ServerMaint/Program.cs
  2. 2
      Teknik/Areas/API/Controllers/APIController.cs
  3. 165
      Teknik/Areas/API/Controllers/APIv1Controller.cs
  4. 23
      Teknik/Areas/Contact/Controllers/ContactController.cs
  5. 24
      Teknik/Areas/Contact/ViewModels/ContactViewModel.cs
  6. 34
      Teknik/Areas/Paste/Controllers/PasteController.cs
  7. 2
      Teknik/Areas/Paste/PasteAreaRegistration.cs
  8. 3
      Teknik/Areas/Paste/PasteHelper.cs
  9. 391
      Teknik/Areas/Podcast/Controllers/PodcastController.cs
  10. 136
      Teknik/Areas/RSS/Controllers/RSSController.cs
  11. 52
      Teknik/Areas/Shortener/Controllers/ShortenerController.cs
  12. 4
      Teknik/Areas/Shortener/Shortener.cs
  13. 206
      Teknik/Areas/Status/Controllers/StatusController.cs
  14. 289
      Teknik/Areas/Upload/Controllers/UploadController.cs
  15. 4
      Teknik/Areas/Upload/Scripts/Download.js
  16. 4
      Teknik/Areas/Upload/Scripts/Upload.js
  17. 798
      Teknik/Areas/User/Controllers/UserController.cs
  18. 484
      Teknik/Areas/Vault/Controllers/VaultController.cs
  19. 3
      Teknik/Areas/Vault/Views/Vault/ViewVault.cshtml
  20. 137
      Teknik/Global.asax.cs
  21. 20
      Teknik/Hubs/IRCClientHub.cs
  22. 12
      Teknik/Scripts/Common.js
  23. 5
      Teknik/Security/TeknikPrincipal.cs
  24. 4
      Teknik/Web.config
  25. 13
      TeknikStreaming/Server.cs
  26. 1
      Utilities/Utilities/FileGenerateResult.cs
  27. 18
      Utilities/Utilities/ResponseHelper.cs

81
ServerMaint/Program.cs

@ -47,56 +47,57 @@ namespace ServerMaint @@ -47,56 +47,57 @@ namespace ServerMaint
if (Directory.Exists(configPath))
{
Config config = Config.Load(configPath);
TeknikEntities db = new TeknikEntities();
Output(string.Format("[{0}] Started Server Maintenance Process.", DateTime.Now));
// Scan all the uploads for viruses, and remove the bad ones
if (options.ScanUploads && config.UploadConfig.VirusScanEnable)
using (TeknikEntities db = new TeknikEntities())
{
ScanUploads(config, db);
}
// Scan all the uploads for viruses, and remove the bad ones
if (options.ScanUploads && config.UploadConfig.VirusScanEnable)
{
ScanUploads(config, db);
}
// Warns all the invalid accounts via email
if (options.WarnAccounts)
{
WarnInvalidAccounts(config, db);
}
// Warns all the invalid accounts via email
if (options.WarnAccounts)
{
WarnInvalidAccounts(config, db);
}
// Cleans all inactive users
if (options.CleanUsers)
{
CleanAccounts(config, db, options.DaysBeforeDeletion);
}
// Cleans all inactive users
if (options.CleanUsers)
{
CleanAccounts(config, db, options.DaysBeforeDeletion);
}
// Cleans the email for unused accounts
if (options.CleanEmails)
{
CleanEmail(config, db);
}
// Cleans the email for unused accounts
if (options.CleanEmails)
{
CleanEmail(config, db);
}
// Cleans all the git accounts that are unused
if (options.CleanGit)
{
CleanGit(config, db);
}
// Cleans all the git accounts that are unused
if (options.CleanGit)
{
CleanGit(config, db);
}
// Generates a file for all of the user's last seen dates
if (options.GenerateLastSeen)
{
GenerateLastSeen(config, db, options.LastSeenFile);
}
// Generates a file for all of the user's last seen dates
if (options.GenerateLastSeen)
{
GenerateLastSeen(config, db, options.LastSeenFile);
}
// Generates a file for all of the invalid accounts
if (options.GenerateInvalid)
{
GenerateInvalidAccounts(config, db, options.InvalidFile);
}
// Generates a file for all of the invalid accounts
if (options.GenerateInvalid)
{
GenerateInvalidAccounts(config, db, options.InvalidFile);
}
// Generates a file for all of the accounts to be cleaned
if (options.GenerateCleaning)
{
GenerateCleaningList(config, db, options.CleaningFile, options.DaysBeforeDeletion);
// Generates a file for all of the accounts to be cleaned
if (options.GenerateCleaning)
{
GenerateCleaningList(config, db, options.CleaningFile, options.DaysBeforeDeletion);
}
}
Output(string.Format("[{0}] Finished Server Maintenance Process.", DateTime.Now));

2
Teknik/Areas/API/Controllers/APIController.cs

@ -16,8 +16,6 @@ namespace Teknik.Areas.API.Controllers @@ -16,8 +16,6 @@ namespace Teknik.Areas.API.Controllers
[TeknikAuthorize]
public class APIController : DefaultController
{
private TeknikEntities db = new TeknikEntities();
[AllowAnonymous]
public ActionResult Index()
{

165
Teknik/Areas/API/Controllers/APIv1Controller.cs

@ -24,8 +24,6 @@ namespace Teknik.Areas.API.Controllers @@ -24,8 +24,6 @@ namespace Teknik.Areas.API.Controllers
[TeknikAuthorize(AuthType.Basic)]
public class APIv1Controller : DefaultController
{
private TeknikEntities db = new TeknikEntities();
[AllowAnonymous]
public ActionResult Index()
{
@ -81,49 +79,52 @@ namespace Teknik.Areas.API.Controllers @@ -81,49 +79,52 @@ namespace Teknik.Areas.API.Controllers
if (model.blockSize <= 0)
model.blockSize = Config.UploadConfig.BlockSize;
// Save the file data
Upload.Models.Upload upload = Uploader.SaveFile(db, Config, model.file.InputStream, model.contentType, contentLength, model.encrypt, fileExt, model.iv, model.key, model.keySize, model.blockSize);
if (upload != null)
using (TeknikEntities db = new TeknikEntities())
{
// Associate this with the user if they provided an auth key
if (User.Identity.IsAuthenticated)
// Save the file data
Upload.Models.Upload upload = Uploader.SaveFile(db, Config, model.file.InputStream, model.contentType, contentLength, model.encrypt, fileExt, model.iv, model.key, model.keySize, model.blockSize);
if (upload != null)
{
User foundUser = UserHelper.GetUser(db, User.Identity.Name);
if (foundUser != null)
// Associate this with the user if they provided an auth key
if (User.Identity.IsAuthenticated)
{
upload.UserId = foundUser.UserId;
User foundUser = UserHelper.GetUser(db, User.Identity.Name);
if (foundUser != null)
{
upload.UserId = foundUser.UserId;
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();
}
}
// Generate delete key only if asked to
if (!model.genDeletionKey)
{
upload.DeleteKey = string.Empty;
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();
}
}
// Generate delete key only if asked to
if (!model.genDeletionKey)
{
upload.DeleteKey = string.Empty;
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();
// Pull all the information together
string fullUrl = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url });
var returnData = new
{
url = (model.saveKey || string.IsNullOrEmpty(model.key)) ? fullUrl : fullUrl + "#" + model.key,
fileName = upload.Url,
contentType = model.contentType,
contentLength = contentLength,
key = model.key,
keySize = model.keySize,
iv = model.iv,
blockSize = model.blockSize,
deletionKey = upload.DeleteKey
};
return Json(new { result = returnData });
}
// Pull all the information together
string fullUrl = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url });
var returnData = new
{
url = (model.saveKey || string.IsNullOrEmpty(model.key)) ? fullUrl : fullUrl + "#" + model.key,
fileName = upload.Url,
contentType = model.contentType,
contentLength = contentLength,
key = model.key,
keySize = model.keySize,
iv = model.iv,
blockSize = model.blockSize,
deletionKey = upload.DeleteKey
};
return Json(new { result = returnData });
return Json(new { error = new { message = "Unable to save file" } });
}
return Json(new { error = new { message = "Unable to save file" } });
}
else
{
@ -149,33 +150,36 @@ namespace Teknik.Areas.API.Controllers @@ -149,33 +150,36 @@ namespace Teknik.Areas.API.Controllers
{
if (model != null && model.code != null)
{
Paste.Models.Paste paste = PasteHelper.CreatePaste(model.code, model.title, model.syntax, model.expireUnit, model.expireLength, model.password, model.hide);
// Associate this with the user if they are logged in
if (User.Identity.IsAuthenticated)
using (TeknikEntities db = new TeknikEntities())
{
User foundUser = UserHelper.GetUser(db, User.Identity.Name);
if (foundUser != null)
Paste.Models.Paste paste = PasteHelper.CreatePaste(db, model.code, model.title, model.syntax, model.expireUnit, model.expireLength, model.password, model.hide);
// Associate this with the user if they are logged in
if (User.Identity.IsAuthenticated)
{
paste.UserId = foundUser.UserId;
User foundUser = UserHelper.GetUser(db, User.Identity.Name);
if (foundUser != null)
{
paste.UserId = foundUser.UserId;
}
}
}
db.Pastes.Add(paste);
db.SaveChanges();
db.Pastes.Add(paste);
db.SaveChanges();
return Json(new
{
result = new
return Json(new
{
id = paste.Url,
url = Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url, password = model.password }),
title = paste.Title,
syntax = paste.Syntax,
expiration = paste.ExpireDate,
password = model.password
}
});
result = new
{
id = paste.Url,
url = Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url, password = model.password }),
title = paste.Title,
syntax = paste.Syntax,
expiration = paste.ExpireDate,
password = model.password
}
});
}
}
return Json(new { error = new { message = "Invalid Paste Request" } });
}
@ -194,35 +198,38 @@ namespace Teknik.Areas.API.Controllers @@ -194,35 +198,38 @@ namespace Teknik.Areas.API.Controllers
{
if (model.url.IsValidUrl())
{
ShortenedUrl newUrl = Shortener.Shortener.ShortenUrl(model.url, Config.ShortenerConfig.UrlLength);
// Associate this with the user if they are logged in
if (User.Identity.IsAuthenticated)
using (TeknikEntities db = new TeknikEntities())
{
User foundUser = UserHelper.GetUser(db, User.Identity.Name);
if (foundUser != null)
ShortenedUrl newUrl = Shortener.Shortener.ShortenUrl(db, model.url, Config.ShortenerConfig.UrlLength);
// Associate this with the user if they are logged in
if (User.Identity.IsAuthenticated)
{
newUrl.UserId = foundUser.UserId;
User foundUser = UserHelper.GetUser(db, User.Identity.Name);
if (foundUser != null)
{
newUrl.UserId = foundUser.UserId;
}
}
}
db.ShortenedUrls.Add(newUrl);
db.SaveChanges();
db.ShortenedUrls.Add(newUrl);
db.SaveChanges();
string shortUrl = string.Format("{0}://{1}/{2}", HttpContext.Request.Url.Scheme, Config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
if (Config.DevEnvironment)
{
shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
}
return Json(new
{
result = new
string shortUrl = string.Format("{0}://{1}/{2}", HttpContext.Request.Url.Scheme, Config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
if (Config.DevEnvironment)
{
shortUrl = shortUrl,
originalUrl = model.url
shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
}
});
return Json(new
{
result = new
{
shortUrl = shortUrl,
originalUrl = model.url
}
});
}
}
return Json(new { error = new { message = "Must be a valid Url" } });
}

23
Teknik/Areas/Contact/Controllers/ContactController.cs

@ -17,8 +17,6 @@ namespace Teknik.Areas.Contact.Controllers @@ -17,8 +17,6 @@ namespace Teknik.Areas.Contact.Controllers
[TeknikAuthorize]
public class ContactController : DefaultController
{
private TeknikEntities db = new TeknikEntities();
// GET: Contact/Contact
[TrackPageView]
[AllowAnonymous]
@ -40,15 +38,18 @@ namespace Teknik.Areas.Contact.Controllers @@ -40,15 +38,18 @@ namespace Teknik.Areas.Contact.Controllers
{
try
{
// Insert the message into the DB
Models.Contact newContact = db.Contact.Create();
newContact.Name = model.Name;
newContact.Email = model.Email;
newContact.Subject = model.Subject;
newContact.Message = model.Message;
newContact.DateAdded = DateTime.Now;
db.Contact.Add(newContact);
db.SaveChanges();
using (TeknikEntities db = new TeknikEntities())
{
// Insert the message into the DB
Models.Contact newContact = db.Contact.Create();
newContact.Name = model.Name;
newContact.Email = model.Email;
newContact.Subject = model.Subject;
newContact.Message = model.Message;
newContact.DateAdded = DateTime.Now;
db.Contact.Add(newContact);
db.SaveChanges();
}
// Let's also email the message to support
SmtpClient client = new SmtpClient();

24
Teknik/Areas/Contact/ViewModels/ContactViewModel.cs

@ -10,8 +10,6 @@ namespace Teknik.Areas.Contact.ViewModels @@ -10,8 +10,6 @@ namespace Teknik.Areas.Contact.ViewModels
{
public class ContactViewModel : ViewModelBase
{
private TeknikEntities db = new TeknikEntities();
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
@ -27,27 +25,5 @@ namespace Teknik.Areas.Contact.ViewModels @@ -27,27 +25,5 @@ namespace Teknik.Areas.Contact.ViewModels
[Required]
[Display(Name = "Message")]
public string Message { get; set; }
public bool Insert()
{
bool success = true;
try
{
Models.Contact newContact = db.Contact.Create();
newContact.Name = Name;
newContact.Email = Email;
newContact.Subject = Subject;
newContact.Message = Message;
newContact.DateAdded = DateTime.Now;
db.Contact.Add(newContact);
db.SaveChanges();
}
catch (Exception ex)
{
success = false;
}
return success;
}
}
}

34
Teknik/Areas/Paste/Controllers/PasteController.cs

@ -21,8 +21,6 @@ namespace Teknik.Areas.Paste.Controllers @@ -21,8 +21,6 @@ namespace Teknik.Areas.Paste.Controllers
[TeknikAuthorize]
public class PasteController : DefaultController
{
private TeknikEntities db = new TeknikEntities();
[TrackPageView]
[AllowAnonymous]
public ActionResult Index()
@ -37,6 +35,7 @@ namespace Teknik.Areas.Paste.Controllers @@ -37,6 +35,7 @@ namespace Teknik.Areas.Paste.Controllers
[AllowAnonymous]
public ActionResult ViewPaste(string type, string url, string password)
{
TeknikEntities db = new TeknikEntities();
Models.Paste paste = db.Pastes.Where(p => p.Url == url).FirstOrDefault();
if (paste != null)
{
@ -156,26 +155,29 @@ namespace Teknik.Areas.Paste.Controllers @@ -156,26 +155,29 @@ namespace Teknik.Areas.Paste.Controllers
{
try
{
Models.Paste paste = PasteHelper.CreatePaste(model.Content, model.Title, model.Syntax, model.ExpireUnit, model.ExpireLength ?? 1, model.Password, model.Hide);
if (model.ExpireUnit == "view")
using (TeknikEntities db = new TeknikEntities())
{
paste.Views = -1;
}
Models.Paste paste = PasteHelper.CreatePaste(db, model.Content, model.Title, model.Syntax, model.ExpireUnit, model.ExpireLength ?? 1, model.Password, model.Hide);
if (User.Identity.IsAuthenticated)
{
Users.Models.User user = UserHelper.GetUser(db, User.Identity.Name);
if (user != null)
if (model.ExpireUnit == "view")
{
paste.UserId = user.UserId;
paste.Views = -1;
}
}
db.Pastes.Add(paste);
db.SaveChanges();
if (User.Identity.IsAuthenticated)
{
Users.Models.User user = UserHelper.GetUser(db, User.Identity.Name);
if (user != null)
{
paste.UserId = user.UserId;
}
}
return Redirect(Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url }));
db.Pastes.Add(paste);
db.SaveChanges();
return Redirect(Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url }));
}
}
catch (Exception ex)
{

2
Teknik/Areas/Paste/PasteAreaRegistration.cs

@ -86,7 +86,7 @@ namespace Teknik.Areas.Paste @@ -86,7 +86,7 @@ namespace Teknik.Areas.Paste
BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/highlight", config.CdnHost).Include(
"~/Scripts/Highlight/highlight.pack.js"));
// Register Style Bundles
BundleTable.Bundles.Add(new CdnScriptBundle("~/Content/paste", config.CdnHost).Include(
BundleTable.Bundles.Add(new CdnStyleBundle("~/Content/paste", config.CdnHost).Include(
"~/Content/Highlight/github-gist.css",
"~/Areas/Paste/Content/Paste.css"));
}

3
Teknik/Areas/Paste/PasteHelper.cs

@ -11,9 +11,8 @@ namespace Teknik.Areas.Paste @@ -11,9 +11,8 @@ namespace Teknik.Areas.Paste
{
public static class PasteHelper
{
public static Models.Paste CreatePaste(string content, string title = "", string syntax = "text", string expireUnit = "never", int expireLength = 1, string password = "", bool hide = false)
public static Models.Paste CreatePaste(TeknikEntities db, string content, string title = "", string syntax = "text", string expireUnit = "never", int expireLength = 1, string password = "", bool hide = false)
{
TeknikEntities db = new TeknikEntities();
Config config = Config.Load();
Models.Paste paste = db.Pastes.Create();
paste.DatePosted = DateTime.Now;

391
Teknik/Areas/Podcast/Controllers/PodcastController.cs

@ -19,8 +19,6 @@ namespace Teknik.Areas.Podcast.Controllers @@ -19,8 +19,6 @@ namespace Teknik.Areas.Podcast.Controllers
[TeknikAuthorize]
public class PodcastController : DefaultController
{
private TeknikEntities db = new TeknikEntities();
[TrackPageView]
[AllowAnonymous]
public ActionResult Index()
@ -33,15 +31,18 @@ namespace Teknik.Areas.Podcast.Controllers @@ -33,15 +31,18 @@ namespace Teknik.Areas.Podcast.Controllers
ViewBag.Title = Config.PodcastConfig.Title + " - " + Config.Title;
ViewBag.Description = Config.PodcastConfig.Description;
bool editor = User.IsInRole("Podcast");
var foundPodcasts = db.Podcasts.Where(p => (p.Published || editor)).FirstOrDefault();
if (foundPodcasts != null)
{
model.HasPodcasts = (foundPodcasts != null);
}
else
using (TeknikEntities db = new TeknikEntities())
{
model.Error = true;
model.ErrorMessage = "No Podcasts Available";
var foundPodcasts = db.Podcasts.Where(p => (p.Published || editor)).FirstOrDefault();
if (foundPodcasts != null)
{
model.HasPodcasts = (foundPodcasts != null);
}
else
{
model.Error = true;
model.ErrorMessage = "No Podcasts Available";
}
}
return View("~/Areas/Podcast/Views/Podcast/Main.cshtml", model);
@ -63,13 +64,16 @@ namespace Teknik.Areas.Podcast.Controllers @@ -63,13 +64,16 @@ namespace Teknik.Areas.Podcast.Controllers
PodcastViewModel model = new PodcastViewModel();
// find the podcast specified
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.Episode == episode)).FirstOrDefault();
if (foundPodcast != null)
using (TeknikEntities db = new TeknikEntities())
{
model = new PodcastViewModel(foundPodcast);
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.Episode == episode)).FirstOrDefault();
if (foundPodcast != null)
{
model = new PodcastViewModel(foundPodcast);
ViewBag.Title = model.Title + " - Teknikast - " + Config.Title;
return View("~/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml", model);
ViewBag.Title = model.Title + " - Teknikast - " + Config.Title;
return View("~/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml", model);
}
}
model.Error = true;
model.ErrorMessage = "No Podcasts Available";
@ -79,112 +83,130 @@ namespace Teknik.Areas.Podcast.Controllers @@ -79,112 +83,130 @@ namespace Teknik.Areas.Podcast.Controllers
[AllowAnonymous]
public ActionResult Download(int episode, string fileName)
{
// find the podcast specified
var foundPodcast = db.Podcasts.Where(p => (p.Published && p.Episode == episode)).FirstOrDefault();
if (foundPodcast != null)
using (TeknikEntities db = new TeknikEntities())
{
PodcastFile file = foundPodcast.Files.Where(f => f.FileName == fileName).FirstOrDefault();
if (file != null)
// find the podcast specified
var foundPodcast = db.Podcasts.Where(p => (p.Published && p.Episode == episode)).FirstOrDefault();
if (foundPodcast != null)
{
if (System.IO.File.Exists(file.Path))
PodcastFile file = foundPodcast.Files.Where(f => f.FileName == fileName).FirstOrDefault();
if (file != null)
{
FileStream fileStream = new FileStream(file.Path, FileMode.Open, FileAccess.Read);
if (System.IO.File.Exists(file.Path))
{
FileStream fileStream = new FileStream(file.Path, FileMode.Open, FileAccess.Read);
Response.AddHeader("Content-Length", file.ContentLength.ToString());
Response.AddHeader("Content-Length", file.ContentLength.ToString());
var cd = new System.Net.Mime.ContentDisposition
{
FileName = file.FileName,
Inline = true
};
var cd = new System.Net.Mime.ContentDisposition
{
FileName = file.FileName,
Inline = true
};
Response.AppendHeader("Content-Disposition", cd.ToString());
Response.AppendHeader("Content-Disposition", cd.ToString());
return new FileGenerateResult(file.FileName, file.ContentType, (response) => ResponseHelper.StreamToOutput(response, true, fileStream, file.ContentLength, 4 * 1024), false);
//return File(data, file.ContentType);
return new FileGenerateResult(file.FileName, file.ContentType, (response) => ResponseHelper.StreamToOutput(response, true, fileStream, file.ContentLength, 4 * 1024), false);
//return File(data, file.ContentType);
}
}
}
return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
}
return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
}
[HttpPost]
[AllowAnonymous]
public ActionResult GetPodcasts(int startPodcastID, int count)
{
bool editor = User.IsInRole("Podcast");
var podcasts = db.Podcasts.Where(p => p.Published || editor).OrderByDescending(p => p.DatePosted).Skip(startPodcastID).Take(count).ToList();
List<PodcastViewModel> podcastViews = new List<PodcastViewModel>();
if (podcasts != null)
using (TeknikEntities db = new TeknikEntities())
{
foreach (Models.Podcast podcast in podcasts)
bool editor = User.IsInRole("Podcast");
var podcasts = db.Podcasts.Where(p => p.Published || editor).OrderByDescending(p => p.DatePosted).Skip(startPodcastID).Take(count).ToList();
List<PodcastViewModel> podcastViews = new List<PodcastViewModel>();
if (podcasts != null)
{
podcastViews.Add(new PodcastViewModel(podcast));
foreach (Models.Podcast podcast in podcasts)
{
podcastViews.Add(new PodcastViewModel(podcast));
}
}
return PartialView("~/Areas/Podcast/Views/Podcast/Podcasts.cshtml", podcastViews);
}
return PartialView("~/Areas/Podcast/Views/Podcast/Podcasts.cshtml", podcastViews);
}
[HttpPost]
[AllowAnonymous]
public ActionResult GetPodcastEpisode(int podcastId)
{
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
if (foundPodcast != null)
using (TeknikEntities db = new TeknikEntities())
{
return Json(new { result = foundPodcast.Episode });
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
if (foundPodcast != null)
{
return Json(new { result = foundPodcast.Episode });
}
return Json(new { error = "No podcast found" });
}
return Json(new { error = "No podcast found" });
}
[HttpPost]
[AllowAnonymous]
public ActionResult GetPodcastTitle(int podcastId)
{
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
if (foundPodcast != null)
using (TeknikEntities db = new TeknikEntities())
{
return Json(new { result = foundPodcast.Title });
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
if (foundPodcast != null)
{
return Json(new { result = foundPodcast.Title });
}
return Json(new { error = "No podcast found" });
}
return Json(new { error = "No podcast found" });
}
[HttpPost]
[AllowAnonymous]
public ActionResult GetPodcastDescription(int podcastId)
{
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
if (foundPodcast != null)
using (TeknikEntities db = new TeknikEntities())
{
return Json(new { result = foundPodcast.Description });
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
if (foundPodcast != null)
{
return Json(new { result = foundPodcast.Description });
}
return Json(new { error = "No podcast found" });
}
return Json(new { error = "No podcast found" });
}
[HttpPost]
[AllowAnonymous]
public ActionResult GetPodcastFiles(int podcastId)
{
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
if (foundPodcast != null)
using (TeknikEntities db = new TeknikEntities())
{
List<object> files = new List<object>();
foreach (PodcastFile file in foundPodcast.Files)
bool editor = User.IsInRole("Podcast");
var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
if (foundPodcast != null)
{
object fileObj = new
List<object> files = new List<object>();
foreach (PodcastFile file in foundPodcast.Files)
{
name = file.FileName,
id = file.PodcastFileId
};
files.Add(fileObj);
object fileObj = new
{
name = file.FileName,
id = file.PodcastFileId
};
files.Add(fileObj);
}
return Json(new { result = new { files = files } });
}
return Json(new { result = new { files = files } });
return Json(new { error = "No podcast found" });
}
return Json(new { error = "No podcast found" });
}
[HttpPost]
@ -194,25 +216,28 @@ namespace Teknik.Areas.Podcast.Controllers @@ -194,25 +216,28 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (User.IsInRole("Podcast"))
{
// Grab the next episode number
Models.Podcast lastPod = db.Podcasts.Where(p => p.Episode == episode).FirstOrDefault();
if (lastPod == null)
using (TeknikEntities db = new TeknikEntities())
{
// Create the podcast object
Models.Podcast podcast = db.Podcasts.Create();
podcast.Episode = episode;
podcast.Title = title;
podcast.Description = description;
podcast.DatePosted = DateTime.Now;
podcast.DatePublished = DateTime.Now;
podcast.DateEdited = DateTime.Now;
podcast.Files = SaveFiles(Request.Files, episode);
// Grab the next episode number
Models.Podcast lastPod = db.Podcasts.Where(p => p.Episode == episode).FirstOrDefault();
if (lastPod == null)
{
// Create the podcast object
Models.Podcast podcast = db.Podcasts.Create();
podcast.Episode = episode;
podcast.Title = title;
podcast.Description = description;
podcast.DatePosted = DateTime.Now;
podcast.DatePublished = DateTime.Now;
podcast.DateEdited = DateTime.Now;
podcast.Files = SaveFiles(Request.Files, episode);
db.Podcasts.Add(podcast);
db.SaveChanges();
return Json(new { result = true });
db.Podcasts.Add(podcast);
db.SaveChanges();
return Json(new { result = true });
}
return Json(new { error = "That episode already exists" });
}
return Json(new { error = "That episode already exists" });
}
return Json(new { error = "You don't have permission to create a podcast" });
}
@ -226,49 +251,52 @@ namespace Teknik.Areas.Podcast.Controllers @@ -226,49 +251,52 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (User.IsInRole("Podcast"))
{
Models.Podcast podcast = db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault();
if (podcast != null)
using (TeknikEntities db = new TeknikEntities())
{
if (db.Podcasts.Where(p => p.Episode != episode).FirstOrDefault() == null || podcast.Episode == episode)
Models.Podcast podcast = db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault();
if (podcast != null)
{
podcast.Episode = episode;
podcast.Title = title;
podcast.Description = description;
podcast.DateEdited = DateTime.Now;
// Remove any files not in fileIds
List<string> fileIdList = new List<string>();
if (!string.IsNullOrEmpty(fileIds))
{
fileIdList = fileIds.Split(',').ToList();
}
for (int i = 0; i < podcast.Files.Count; i++)
if (db.Podcasts.Where(p => p.Episode != episode).FirstOrDefault() == null || podcast.Episode == episode)
{
PodcastFile curFile = podcast.Files.ElementAt(i);
if (!fileIdList.Exists(id => id == curFile.PodcastFileId.ToString()))
podcast.Episode = episode;
podcast.Title = title;
podcast.Description = description;
podcast.DateEdited = DateTime.Now;
// Remove any files not in fileIds
List<string> fileIdList = new List<string>();
if (!string.IsNullOrEmpty(fileIds))
{
if (System.IO.File.Exists(curFile.Path))
fileIdList = fileIds.Split(',').ToList();
}
for (int i = 0; i < podcast.Files.Count; i++)
{
PodcastFile curFile = podcast.Files.ElementAt(i);
if (!fileIdList.Exists(id => id == curFile.PodcastFileId.ToString()))
{
System.IO.File.Delete(curFile.Path);
if (System.IO.File.Exists(curFile.Path))
{
System.IO.File.Delete(curFile.Path);
}
db.PodcastFiles.Remove(curFile);
podcast.Files.Remove(curFile);
}
db.PodcastFiles.Remove(curFile);
podcast.Files.Remove(curFile);
}
}
// Add any new files
List<PodcastFile> newFiles = SaveFiles(Request.Files, episode);
foreach (PodcastFile file in newFiles)
{
podcast.Files.Add(file);
}
// Add any new files
List<PodcastFile> newFiles = SaveFiles(Request.Files, episode);
foreach (PodcastFile file in newFiles)
{
podcast.Files.Add(file);
}
// Save podcast
db.Entry(podcast).State = EntityState.Modified;
db.SaveChanges();
return Json(new { result = true });
// Save podcast
db.Entry(podcast).State = EntityState.Modified;
db.SaveChanges();
return Json(new { result = true });
}
return Json(new { error = "That episode already exists" });
}
return Json(new { error = "That episode already exists" });
return Json(new { error = "No podcast found" });
}
return Json(new { error = "No podcast found" });
}
return Json(new { error = "You don't have permission to edit this podcast" });
}
@ -282,17 +310,20 @@ namespace Teknik.Areas.Podcast.Controllers @@ -282,17 +310,20 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (User.IsInRole("Podcast"))
{
Models.Podcast podcast = db.Podcasts.Find(podcastId);
if (podcast != null)
using (TeknikEntities db = new TeknikEntities())
{
podcast.Published = publish;
if (publish)
podcast.DatePublished = DateTime.Now;
db.Entry(podcast).State = EntityState.Modified;
db.SaveChanges();
return Json(new { result = true });
Models.Podcast podcast = db.Podcasts.Find(podcastId);
if (podcast != null)
{
podcast.Published = publish;
if (publish)
podcast.DatePublished = DateTime.Now;
db.Entry(podcast).State = EntityState.Modified;
db.SaveChanges();
return Json(new { result = true });
}
return Json(new { error = "No podcast found" });
}
return Json(new { error = "No podcast found" });
}
return Json(new { error = "You don't have permission to publish this podcast" });
}
@ -306,18 +337,21 @@ namespace Teknik.Areas.Podcast.Controllers @@ -306,18 +337,21 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (User.IsInRole("Podcast"))
{
Models.Podcast podcast = db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault();
if (podcast != null)
using (TeknikEntities db = new TeknikEntities())
{
foreach (PodcastFile file in podcast.Files)
Models.Podcast podcast = db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault();
if (podcast != null)
{
System.IO.File.Delete(file.Path);
foreach (PodcastFile file in podcast.Files)
{
System.IO.File.Delete(file.Path);
}
db.Podcasts.Remove(podcast);
db.SaveChanges();
return Json(new { result = true });
}
db.Podcasts.Remove(podcast);
db.SaveChanges();
return Json(new { result = true });
return Json(new { error = "No podcast found" });
}
return Json(new { error = "No podcast found" });
}
return Json(new { error = "You don't have permission to delete this podcast" });
}
@ -330,28 +364,34 @@ namespace Teknik.Areas.Podcast.Controllers @@ -330,28 +364,34 @@ namespace Teknik.Areas.Podcast.Controllers
[AllowAnonymous]
public ActionResult GetComments(int podcastId, int startCommentID, int count)
{
var comments = db.PodcastComments.Where(p => (p.PodcastId == podcastId)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
List<CommentViewModel> commentViews = new List<CommentViewModel>();
if (comments != null)
using (TeknikEntities db = new TeknikEntities())
{
foreach (PodcastComment comment in comments)
var comments = db.PodcastComments.Where(p => (p.PodcastId == podcastId)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
List<CommentViewModel> commentViews = new List<CommentViewModel>();
if (comments != null)
{
commentViews.Add(new CommentViewModel(comment));
foreach (PodcastComment comment in comments)
{
commentViews.Add(new CommentViewModel(comment));
}
}
return PartialView("~/Areas/Podcast/Views/Podcast/Comments.cshtml", commentViews);
}
return PartialView("~/Areas/Podcast/Views/Podcast/Comments.cshtml", commentViews);
}
[HttpPost]
[AllowAnonymous]
public ActionResult GetCommentArticle(int commentID)
{
PodcastComment comment = db.PodcastComments.Where(p => (p.PodcastCommentId == commentID)).FirstOrDefault();
if (comment != null)
using (TeknikEntities db = new TeknikEntities())
{
return Json(new { result = comment.Article });
PodcastComment comment = db.PodcastComments.Where(p => (p.PodcastCommentId == commentID)).FirstOrDefault();
if (comment != null)
{
return Json(new { result = comment.Article });
}
return Json(new { error = "No article found" });
}
return Json(new { error = "No article found" });
}
[HttpPost]
@ -359,20 +399,23 @@ namespace Teknik.Areas.Podcast.Controllers @@ -359,20 +399,23 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (ModelState.IsValid)
{
if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
using (TeknikEntities db = new TeknikEntities())
{
PodcastComment comment = db.PodcastComments.Create();
comment.PodcastId = podcastId;
comment.UserId = UserHelper.GetUser(db, User.Identity.Name).UserId;
comment.Article = article;
comment.DatePosted = DateTime.Now;
comment.DateEdited = DateTime.Now;
if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
{
PodcastComment comment = db.PodcastComments.Create();
comment.PodcastId = podcastId;
comment.UserId = UserHelper.GetUser(db, User.Identity.Name).UserId;
comment.Article = article;
comment.DatePosted = DateTime.Now;
comment.DateEdited = DateTime.Now;
db.PodcastComments.Add(comment);
db.SaveChanges();
return Json(new { result = true });
db.PodcastComments.Add(comment);
db.SaveChanges();
return Json(new { result = true });
}
return Json(new { error = "That podcast does not exist" });
}
return Json(new { error = "That podcast does not exist" });
}
return Json(new { error = "Invalid Parameters" });
}
@ -382,20 +425,23 @@ namespace Teknik.Areas.Podcast.Controllers @@ -382,20 +425,23 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (ModelState.IsValid)
{
PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
if (comment != null)
using (TeknikEntities db = new TeknikEntities())
{
if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
if (comment != null)
{
comment.Article = article;
comment.DateEdited = DateTime.Now;
db.Entry(comment).State = EntityState.Modified;
db.SaveChanges();
return Json(new { result = true });
if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
{
comment.Article = article;
comment.DateEdited = DateTime.Now;
db.Entry(comment).State = EntityState.Modified;
db.SaveChanges();
return Json(new { result = true });
}
return Json(new { error = "You don't have permission to edit this comment" });
}
return Json(new { error = "You don't have permission to edit this comment" });
return Json(new { error = "No comment found" });
}
return Json(new { error = "No comment found" });
}
return Json(new { error = "Invalid Parameters" });
}
@ -405,18 +451,21 @@ namespace Teknik.Areas.Podcast.Controllers @@ -405,18 +451,21 @@ namespace Teknik.Areas.Podcast.Controllers
{
if (ModelState.IsValid)
{
PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
if (comment != null)
using (TeknikEntities db = new TeknikEntities())
{
if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
if (comment != null)
{
db.PodcastComments.Remove(comment);
db.SaveChanges();
return Json(new { result = true });
if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
{
db.PodcastComments.Remove(comment);
db.SaveChanges();
return Json(new { result = true });
}
return Json(new { error = "You don't have permission to delete this comment" });
}
return Json(new { error = "You don't have permission to delete this comment" });
return Json(new { error = "No comment found" });
}
return Json(new { error = "No comment found" });
}
return Json(new { error = "Invalid Parameters" });
}

136
Teknik/Areas/RSS/Controllers/RSSController.cs

@ -19,8 +19,6 @@ namespace Teknik.Areas.RSS.Controllers @@ -19,8 +19,6 @@ namespace Teknik.Areas.RSS.Controllers
[TeknikAuthorize(AuthType.Basic)]
public class RSSController : DefaultController
{
private TeknikEntities db = new TeknikEntities();
[AllowAnonymous]
public ActionResult Index()
{
@ -33,102 +31,108 @@ namespace Teknik.Areas.RSS.Controllers @@ -33,102 +31,108 @@ namespace Teknik.Areas.RSS.Controllers
[AllowAnonymous]
public ActionResult Blog(string username)
{
// If empty, grab the main blog
List<BlogPost> posts = new List<BlogPost>();
string blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
string title = string.Empty;
string description = string.Empty;
bool isSystem = string.IsNullOrEmpty(username);
if (isSystem)
{
posts = db.BlogPosts.Where(p => (p.System && p.Published)).ToList();
blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
}
else
{
Blog.Models.Blog blog = db.Blogs.Where(p => p.User.Username == username && p.BlogId != Config.BlogConfig.ServerBlogId).FirstOrDefault();
posts = db.BlogPosts.Where(p => (p.BlogId == blog.BlogId && !p.System) && p.Published).ToList();
blogUrl = Url.SubRouteUrl("blog", "Blog.Blog", new { username = username });
}
if (posts.Any())
using (TeknikEntities db = new TeknikEntities())
{
// If empty, grab the main blog
List<BlogPost> posts = new List<BlogPost>();
string blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
string title = string.Empty;
string description = string.Empty;
bool isSystem = string.IsNullOrEmpty(username);
if (isSystem)
{
title = Config.BlogConfig.Title;
description = Config.BlogConfig.Description;
posts = db.BlogPosts.Where(p => (p.System && p.Published)).ToList();
blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
}
else
{
Users.Models.User user = UserHelper.GetUser(db, username);
if (user != null)
Blog.Models.Blog blog = db.Blogs.Where(p => p.User.Username == username && p.BlogId != Config.BlogConfig.ServerBlogId).FirstOrDefault();
posts = db.BlogPosts.Where(p => (p.BlogId == blog.BlogId && !p.System) && p.Published).ToList();
blogUrl = Url.SubRouteUrl("blog", "Blog.Blog", new { username = username });
}
if (posts.Any())
{
if (isSystem)
{
title = user.BlogSettings.Title;
description = user.BlogSettings.Description;
title = Config.BlogConfig.Title;
description = Config.BlogConfig.Description;
}
else
{
SyndicationFeed badUserFeed = new SyndicationFeed("No Blog Available", "The specified user does not exist", new Uri(blogUrl));
Users.Models.User user = UserHelper.GetUser(db, username);
if (user != null)
{
title = user.BlogSettings.Title;
description = user.BlogSettings.Description;
}
else
{
SyndicationFeed badUserFeed = new SyndicationFeed("No Blog Available", "The specified user does not exist", new Uri(blogUrl));
return new RssResult(badUserFeed);
return new RssResult(badUserFeed);
}
}
}
List<SyndicationItem> items = new List<SyndicationItem>();
List<SyndicationItem> items = new List<SyndicationItem>();
foreach (BlogPost post in posts.OrderByDescending(p => p.BlogPostId))
{
if (post.Published && post.System == isSystem)
foreach (BlogPost post in posts.OrderByDescending(p => p.BlogPostId))
{
items.Add(new SyndicationItem(
post.Title,
MarkdownHelper.Markdown(post.Article).ToHtmlString(),
new Uri(Url.SubRouteUrl("blog", "Blog.Post", new { username = post.Blog.User.Username, id = post.BlogPostId })),
post.BlogPostId.ToString(),
post.DateEdited
));
if (post.Published && post.System == isSystem)
{
items.Add(new SyndicationItem(
post.Title,
MarkdownHelper.Markdown(post.Article).ToHtmlString(),
new Uri(Url.SubRouteUrl("blog", "Blog.Post", new { username = post.Blog.User.Username, id = post.BlogPostId })),
post.BlogPostId.ToString(),
post.DateEdited
));
}
}
}
SyndicationFeed feed = new SyndicationFeed(title, description, new Uri(blogUrl), items);
SyndicationFeed feed = new SyndicationFeed(title, description, new Uri(blogUrl), items);
return new RssResult(feed);
}
SyndicationFeed badFeed = new SyndicationFeed("No Blog Available", "The specified blog does not exist", new Uri(blogUrl));
return new RssResult(feed);
}
SyndicationFeed badFeed = new SyndicationFeed("No Blog Available", "The specified blog does not exist", new Uri(blogUrl));
return new RssResult(badFeed);
return new RssResult(badFeed);
}
}
[TrackDownload]
[AllowAnonymous]
public ActionResult Podcast()
{
List<SyndicationItem> items = new List<SyndicationItem>();
List<Podcast.Models.Podcast> podcasts = db.Podcasts.Where(p => p.Published).OrderByDescending(p => p.Episode).ToList();
if (podcasts != null)
using (TeknikEntities db = new TeknikEntities())
{
foreach (Podcast.Models.Podcast podcast in podcasts)
List<SyndicationItem> items = new List<SyndicationItem>();
List<Podcast.Models.Podcast> podcasts = db.Podcasts.Where(p => p.Published).OrderByDescending(p => p.Episode).ToList();
if (podcasts != null)
{
SyndicationItem item = new SyndicationItem(
podcast.Title,
MarkdownHelper.Markdown(podcast.Description).ToHtmlString(),
new Uri(Url.SubRouteUrl("podcast", "Podcast.View", new { episode = podcast.Episode })),
podcast.Episode.ToString(),
podcast.DateEdited
);
foreach (Podcast.Models.PodcastFile file in podcast.Files)
foreach (Podcast.Models.Podcast podcast in podcasts)
{
SyndicationLink enclosure = SyndicationLink.CreateMediaEnclosureLink(new Uri(Url.SubRouteUrl("podcast", "Podcast.Download", new { episode = podcast.Episode, fileName = file.FileName })), file.ContentType, file.ContentLength);
item.Links.Add(enclosure);
}
SyndicationItem item = new SyndicationItem(
podcast.Title,
MarkdownHelper.Markdown(podcast.Description).ToHtmlString(),
new Uri(Url.SubRouteUrl("podcast", "Podcast.View", new { episode = podcast.Episode })),
podcast.Episode.ToString(),
podcast.DateEdited
);
foreach (Podcast.Models.PodcastFile file in podcast.Files)
{
SyndicationLink enclosure = SyndicationLink.CreateMediaEnclosureLink(new Uri(Url.SubRouteUrl("podcast", "Podcast.Download", new { episode = podcast.Episode, fileName = file.FileName })), file.ContentType, file.ContentLength);
item.Links.Add(enclosure);
}
items.Add(item);
items.Add(item);
}
}
}
SyndicationFeed feed = new SyndicationFeed(Config.PodcastConfig.Title, Config.PodcastConfig.Description, new Uri(Url.SubRouteUrl("podcast", "Podcast.Index")), items);
SyndicationFeed feed = new SyndicationFeed(Config.PodcastConfig.Title, Config.PodcastConfig.Description, new Uri(Url.SubRouteUrl("podcast", "Podcast.Index")), items);
return new RssResult(feed);
return new RssResult(feed);
}
}
}
}

52
Teknik/Areas/Shortener/Controllers/ShortenerController.cs

@ -17,8 +17,6 @@ namespace Teknik.Areas.Shortener.Controllers @@ -17,8 +17,6 @@ namespace Teknik.Areas.Shortener.Controllers
[TeknikAuthorize]
public class ShortenerController : DefaultController
{
private TeknikEntities db = new TeknikEntities();
[TrackPageView]
[AllowAnonymous]
public ActionResult Index()
@ -31,15 +29,18 @@ namespace Teknik.Areas.Shortener.Controllers @@ -31,15 +29,18 @@ namespace Teknik.Areas.Shortener.Controllers
[AllowAnonymous]
public ActionResult RedirectToUrl(string url)
{
ShortenedUrl shortUrl = db.ShortenedUrls.Where(s => s.ShortUrl == url).FirstOrDefault();
if (shortUrl != null)
using (TeknikEntities db = new TeknikEntities())
{
shortUrl.Views += 1;
db.Entry(shortUrl).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return Redirect(shortUrl.OriginalUrl);
ShortenedUrl shortUrl = db.ShortenedUrls.Where(s => s.ShortUrl == url).FirstOrDefault();
if (shortUrl != null)
{
shortUrl.Views += 1;
db.Entry(shortUrl).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return Redirect(shortUrl.OriginalUrl);
}
return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
}
return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
}
[HttpPost]
@ -48,27 +49,30 @@ namespace Teknik.Areas.Shortener.Controllers @@ -48,27 +49,30 @@ namespace Teknik.Areas.Shortener.Controllers
{