- 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 vaulttags/3.0.0
@@ -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)); |
@@ -16,8 +16,6 @@ namespace Teknik.Areas.API.Controllers | |||
[TeknikAuthorize] | |||
public class APIController : DefaultController | |||
{ | |||
private TeknikEntities db = new TeknikEntities(); | |||
[AllowAnonymous] | |||
public ActionResult Index() | |||
{ |
@@ -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 | |||
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 | |||
{ | |||
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 | |||
{ | |||
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" } }); | |||
} |
@@ -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 | |||
{ | |||
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(); |
@@ -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 | |||
[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; | |||
} | |||
} | |||
} |
@@ -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 | |||
[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 | |||
{ | |||
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) | |||
{ |
@@ -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")); | |||
} |
@@ -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; |
@@ -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 | |||
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 | |||
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 | |||
[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 | |||
{ | |||
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 | |||
{ | |||
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 | |||
{ | |||
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 | |||
{ | |||
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 | |||
[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 | |||
{ | |||
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 | |||
{ | |||
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 | |||
{ | |||
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" }); | |||
} |
@@ -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 | |||
[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); | |||
} | |||
} | |||
} | |||
} |
@@ -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 | |||
[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 | |||
{ | |||
if (url.IsValidUrl()) | |||
{ | |||
ShortenedUrl newUrl = Shortener.ShortenUrl(url, Config.ShortenerConfig.UrlLength); | |||
if (User.Identity.IsAuthenticated) | |||
using (TeknikEntities db = new TeknikEntities()) | |||
{ | |||
Users.Models.User foundUser = UserHelper.GetUser(db, User.Identity.Name); | |||
if (foundUser != null) | |||
ShortenedUrl newUrl = Shortener.ShortenUrl(db, url, Config.ShortenerConfig.UrlLength); | |||
if (User.Identity.IsAuthenticated) | |||
{ | |||
newUrl.UserId = foundUser.UserId; | |||
Users.Models.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 }); | |||
} | |||
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 { shortUrl = shortUrl, originalUrl = url } }); | |||
return Json(new { result = new { shortUrl = shortUrl, originalUrl = url } }); | |||
} | |||
} | |||
return Json(new { error = "Must be a valid Url" }); | |||
} |
@@ -11,10 +11,8 @@ namespace Teknik.Areas.Shortener | |||
{ | |||
public static class Shortener | |||
{ | |||
public static ShortenedUrl ShortenUrl(string url, int length) | |||
public static ShortenedUrl ShortenUrl(TeknikEntities db, string url, int length) | |||
{ | |||
TeknikEntities db = new TeknikEntities(); | |||
// Generate the shortened url | |||
string shortUrl = StringHelper.RandomString(length); | |||
while (db.ShortenedUrls.Where(s => s.ShortUrl == shortUrl).FirstOrDefault() != null) |
@@ -19,8 +19,6 @@ namespace Teknik.Areas.Status.Controllers | |||
[TeknikAuthorize] | |||
public class StatusController : DefaultController | |||
{ | |||
private TeknikEntities db = new TeknikEntities(); | |||
[TrackPageView] | |||
[AllowAnonymous] | |||
public ActionResult Index() | |||
@@ -30,134 +28,136 @@ namespace Teknik.Areas.Status.Controllers | |||
StatusViewModel model = new StatusViewModel(); | |||
// Load initial status info | |||
#region Statistics | |||
Upload.Models.Upload upload = db.Uploads.OrderByDescending(u => u.UploadId).FirstOrDefault(); | |||
model.UploadCount = (upload != null) ? upload.UploadId : 0; | |||
model.UploadSize = (upload != null) ? db.Uploads.Sum(u => (long)u.ContentLength) : 0; | |||
using (TeknikEntities db = new TeknikEntities()) | |||
{ | |||
// Load initial status info | |||
#region Statistics | |||
Upload.Models.Upload upload = db.Uploads.OrderByDescending(u => u.UploadId).FirstOrDefault(); | |||
model.UploadCount = (upload != null) ? upload.UploadId : 0; | |||
model.UploadSize = (upload != null) ? db.Uploads.Sum(u => (long)u.ContentLength) : 0; | |||
Paste.Models.Paste paste = db.Pastes.OrderByDescending(p => p.PasteId).FirstOrDefault(); | |||
model.PasteCount = (paste != null) ? paste.PasteId : 0; | |||
Paste.Models.Paste paste = db.Pastes.OrderByDescending(p => p.PasteId).FirstOrDefault(); | |||
model.PasteCount = (paste != null) ? paste.PasteId : 0; | |||
Users.Models.User user = db.Users.OrderByDescending(u => u.UserId).FirstOrDefault(); | |||
model.UserCount = (user != null) ? user.UserId : 0; | |||
Users.Models.User user = db.Users.OrderByDescending(u => u.UserId).FirstOrDefault(); | |||
model.UserCount = (user != null) ? user.UserId : 0; | |||
Shortener.Models.ShortenedUrl url = db.ShortenedUrls.OrderByDescending(s => s.ShortenedUrlId).FirstOrDefault(); | |||
model.ShortenedUrlCount = (url != null) ? url.ShortenedUrlId : 0; | |||
Shortener.Models.ShortenedUrl url = db.ShortenedUrls.OrderByDescending(s => s.ShortenedUrlId).FirstOrDefault(); | |||
model.ShortenedUrlCount = (url != null) ? url.ShortenedUrlId : 0; | |||
Vault.Models.Vault vault = db.Vaults.OrderByDescending(v => v.VaultId).FirstOrDefault(); | |||
model.VaultCount = (url != null) ? vault.VaultId : 0; | |||
#endregion | |||
Vault.Models.Vault vault = db.Vaults.OrderByDescending(v => v.VaultId).FirstOrDefault(); | |||
model.VaultCount = (url != null) ? vault.VaultId : 0; | |||
#endregion | |||
// Get Transaction Inforomation | |||
#region Transactions | |||
DateTime curTime = DateTime.Now; | |||
// Get Transaction Inforomation | |||
#region Transactions | |||
DateTime curTime = DateTime.Now; | |||
var billSums = db.Transactions.OfType<Bill>().GroupBy(b => new { b.Currency, b.DateSent.Month, b.DateSent.Year}).Select(b => new { month = b.Key.Month, year = b.Key.Year, currency = b.Key.Currency, total = b.Sum(c => c.Amount) }).ToList(); | |||
foreach (var sum in billSums) | |||
{ | |||
decimal exchangeRate = CurrencyHelper.GetExchangeRate(sum.currency); | |||
decimal realValue = sum.total * exchangeRate; | |||
model.Transactions.TotalBills += realValue; | |||
model.Transactions.TotalNet += realValue; | |||
if (curTime.Month == sum.month && curTime.Year == sum.year) | |||
var billSums = db.Transactions.OfType<Bill>().GroupBy(b => new { b.Currency, b.DateSent.Month, b.DateSent.Year }).Select(b => new { month = b.Key.Month, year = b.Key.Year, currency = b.Key.Currency, total = b.Sum(c => c.Amount) }).ToList(); | |||
foreach (var sum in billSums) | |||
{ | |||
model.Transactions.CurrentMonthBills += Math.Abs(realValue); | |||
decimal exchangeRate = CurrencyHelper.GetExchangeRate(sum.currency); | |||
decimal realValue = sum.total * exchangeRate; | |||
model.Transactions.TotalBills += realValue; | |||
model.Transactions.TotalNet += realValue; | |||
if (curTime.Month == sum.month && curTime.Year == sum.year) | |||
{ | |||
model.Transactions.CurrentMonthBills += Math.Abs(realValue); | |||
} | |||
} | |||
} | |||
var oneSums = db.Transactions.OfType<OneTime>().GroupBy(b => new { b.Currency, b.DateSent.Month, b.DateSent.Year }).Select(b => new { month = b.Key.Month, year = b.Key.Year, currency = b.Key.Currency, total = b.Sum(c => c.Amount) }).ToList(); | |||
foreach (var sum in oneSums) | |||
{ | |||
decimal exchangeRate = CurrencyHelper.GetExchangeRate(sum.currency); | |||
decimal realValue = sum.total * exchangeRate; | |||
model.Transactions.TotalOneTimes += realValue; | |||
model.Transactions.TotalNet += realValue; | |||
if (curTime.Month == sum.month && curTime.Year == sum.year) | |||
var oneSums = db.Transactions.OfType<OneTime>().GroupBy(b => new { b.Currency, b.DateSent.Month, b.DateSent.Year }).Select(b => new { month = b.Key.Month, year = b.Key.Year, currency = b.Key.Currency, total = b.Sum(c => c.Amount) }).ToList(); | |||
foreach (var sum in oneSums) | |||
{ | |||
model.Transactions.CurrentMonthBills += Math.Abs(realValue); | |||
decimal exchangeRate = CurrencyHelper.GetExchangeRate(sum.currency); | |||
decimal realValue = sum.total * exchangeRate; | |||
model.Transactions.TotalOneTimes += realValue; | |||
model.Transactions.TotalNet += realValue; | |||
if (curTime.Month == sum.month && curTime.Year == sum.year) | |||
{ | |||
model.Transactions.CurrentMonthBills += Math.Abs(realValue); | |||
} | |||
} | |||
} | |||
var donationSums = db.Transactions.OfType<Donation>().GroupBy(b => new { b.Currency, b.DateSent.Month, b.DateSent.Year }).Select(b => new { month = b.Key.Month, year = b.Key.Year, currency = b.Key.Currency, total = b.Sum(c => c.Amount) }).ToList(); | |||
foreach (var sum in donationSums) | |||
{ | |||
decimal exchangeRate = CurrencyHelper.GetExchangeRate(sum.currency); | |||
decimal realValue = sum.total * exchangeRate; | |||
model.Transactions.TotalDonations += realValue; | |||
model.Transactions.TotalNet += realValue; | |||
if (curTime.Month == sum.month && curTime.Year == sum.year) | |||
var donationSums = db.Transactions.OfType<Donation>().GroupBy(b => new { b.Currency, b.DateSent.Month, b.DateSent.Year }).Select(b => new { month = b.Key.Month, year = b.Key.Year, currency = b.Key.Currency, total = b.Sum(c => c.Amount) }).ToList(); | |||
foreach (var sum in donationSums) | |||
{ | |||
model.Transactions.CurrentMonthIncome += Math.Abs(realValue); | |||
decimal exchangeRate = CurrencyHelper.GetExchangeRate(sum.currency); | |||
decimal realValue = sum.total * exchangeRate; | |||
model.Transactions.TotalDonations += realValue; | |||
model.Transactions.TotalNet += realValue; | |||
if (curTime.Month == sum.month && curTime.Year == sum.year) | |||
{ | |||
model.Transactions.CurrentMonthIncome += Math.Abs(realValue); | |||
} | |||
} | |||
} | |||
List<Bill> bills = db.Transactions.OfType<Bill>().OrderByDescending(b => b.DateSent).ToList(); | |||
if (bills != null) | |||
{ | |||
foreach (Bill bill in bills) | |||
List<Bill> bills = db.Transactions.OfType<Bill>().OrderByDescending(b => b.DateSent).ToList(); | |||
if (bills != null) | |||
{ | |||
BillViewModel billModel = new BillViewModel(); | |||
billModel.Amount = bill.Amount; | |||
billModel.Currency = bill.Currency; | |||
billModel.Reason = bill.Reason; | |||
billModel.DateSent = bill.DateSent; | |||
billModel.Recipient = bill.Recipient; | |||
model.Transactions.Bills.Add(billModel); | |||
foreach (Bill bill in bills) | |||
{ | |||
BillViewModel billModel = new BillViewModel(); | |||
billModel.Amount = bill.Amount; | |||
billModel.Currency = bill.Currency; | |||
billModel.Reason = bill.Reason; | |||
billModel.DateSent = bill.DateSent; | |||
billModel.Recipient = bill.Recipient; | |||
model.Transactions.Bills.Add(billModel); | |||
} | |||
} | |||
} | |||
List<OneTime> oneTimes = db.Transactions.OfType<OneTime>().OrderByDescending(b => b.DateSent).ToList(); | |||
if (oneTimes != null) | |||
{ | |||
foreach (OneTime oneTime in oneTimes) | |||
List<OneTime> oneTimes = db.Transactions.OfType<OneTime>().OrderByDescending(b => b.DateSent).ToList(); | |||
if (oneTimes != null) | |||
{ | |||
OneTimeViewModel oneTimeModel = new OneTimeViewModel(); | |||
oneTimeModel.Amount = oneTime.Amount; | |||
oneTimeModel.Currency = oneTime.Currency; | |||
oneTimeModel.Reason = oneTime.Reason; | |||
oneTimeModel.DateSent = oneTime.DateSent; | |||
oneTimeModel.Recipient = oneTime.Recipient; | |||
model.Transactions.OneTimes.Add(oneTimeModel); | |||
foreach (OneTime oneTime in oneTimes) | |||
{ | |||
OneTimeViewModel oneTimeModel = new OneTimeViewModel(); | |||
oneTimeModel.Amount = oneTime.Amount; | |||
oneTimeModel.Currency = oneTime.Currency; | |||
oneTimeModel.Reason = oneTime.Reason; | |||
oneTimeModel.DateSent = oneTime.DateSent; | |||
oneTimeModel.Recipient = oneTime.Recipient; | |||
model.Transactions.OneTimes.Add(oneTimeModel); | |||
} | |||
} | |||
} | |||
List<Donation> donations = db.Transactions.OfType<Donation>().OrderByDescending(b => b.DateSent).ToList(); | |||
if (donations != null) | |||
{ | |||
foreach (Donation donation in donations) | |||
List<Donation> donations = db.Transactions.OfType<Donation>().OrderByDescending(b => b.DateSent).ToList(); | |||
if (donations != null) | |||
{ | |||
DonationViewModel donationModel = new DonationViewModel(); | |||
donationModel.Amount = donation.Amount; | |||
donationModel.Currency = donation.Currency; | |||
donationModel.Reason = donation.Reason; | |||
donationModel.DateSent = donation.DateSent; | |||
donationModel.Sender = donation.Sender; | |||
model.Transactions.Donations.Add(donationModel); | |||
foreach (Donation donation in donations) | |||
{ | |||
DonationViewModel donationModel = new DonationViewModel(); | |||
donationModel.Amount = donation.Amount; | |||
donationModel.Currency = donation.Currency; | |||
donationModel.Reason = donation.Reason; | |||
donationModel.DateSent = donation.DateSent; | |||
donationModel.Sender = donation.Sender; | |||
model.Transactions.Donations.Add(donationModel); | |||
} | |||
} | |||
} | |||
#endregion | |||
#endregion | |||
// Takedown information | |||
#region Takedowns | |||
List<Takedown> takedowns = db.Takedowns.OrderByDescending(b => b.DateRequested).ToList(); | |||
if (takedowns != null) | |||
{ | |||
foreach (Takedown takedown in takedowns) | |||
// Takedown information | |||
#region Takedowns | |||
List<Takedown> takedowns = db.Takedowns.OrderByDescending(b => b.DateRequested).ToList(); | |||
if (takedowns != null) | |||
{ | |||
TakedownViewModel takedownModel = new TakedownViewModel(); | |||
takedownModel.Requester = takedown.Requester; | |||
takedownModel.RequesterContact = takedown.RequesterContact; | |||
takedownModel.Reason = takedown.Reason; | |||
takedownModel.ActionTaken = takedown.ActionTaken; | |||
takedownModel.DateRequested = takedown.DateRequested; | |||
takedownModel.DateActionTaken = takedown.DateActionTaken; | |||
model.Takedowns.Add(takedownModel); | |||
foreach (Takedown takedown in takedowns) | |||
{ | |||
TakedownViewModel takedownModel = new TakedownViewModel(); | |||
takedownModel.Requester = takedown.Requester; | |||
takedownModel.RequesterContact = takedown.RequesterContact; | |||
takedownModel.Reason = takedown.Reason; | |||
takedownModel.ActionTaken = takedown.ActionTaken; | |||
takedownModel.DateRequested = takedown.DateRequested; | |||
takedownModel.DateActionTaken = takedown.DateActionTaken; | |||
model.Takedowns.Add(takedownModel); | |||
} | |||
} | |||
#endregion | |||
} | |||
#endregion | |||
return View(model); | |||
} | |||
@@ -26,8 +26,6 @@ namespace Teknik.Areas.Upload.Controllers | |||
[TeknikAuthorize] | |||
public class UploadController : DefaultController | |||
{ | |||
private TeknikEntities db = new TeknikEntities(); | |||
// GET: Upload/Upload | |||
[HttpGet] | |||
[TrackPageView] | |||
@@ -37,15 +35,18 @@ namespace Teknik.Areas.Upload.Controllers | |||
ViewBag.Title = "Teknik Upload - End to End Encryption"; | |||
UploadViewModel model = new UploadViewModel(); | |||
model.CurrentSub = Subdomain; | |||
Users.Models.User user = UserHelper.GetUser(db, User.Identity.Name); | |||
if (user != null) | |||
{ | |||
model.Encrypt = user.UploadSettings.Encrypt; | |||
model.Vaults = user.Vaults.ToList(); | |||
} | |||
else | |||
using (TeknikEntities db = new TeknikEntities()) | |||
{ | |||
model.Encrypt = false; | |||
Users.Models.User user = UserHelper.GetUser(db, User.Identity.Name); | |||
if (user != null) | |||
{ | |||
model.Encrypt = user.UploadSettings.Encrypt; | |||
model.Vaults = user.Vaults.ToList(); | |||
} | |||
else | |||
{ | |||
model.Encrypt = false; | |||
} | |||
} | |||
return View(model); | |||
} | |||
@@ -82,23 +83,26 @@ namespace Teknik.Areas.Upload.Controllers | |||
return Json(new { error = new { message = string.Format("Unknown result while scanning the file upload for viruses. {0}", scanResult.RawResult) } }); | |||
} | |||
} | |||
Models.Upload upload = Uploader.SaveFile(db, Config, data.InputStream, fileType, contentLength, encrypt, fileExt, iv, null, keySize, blockSize); | |||
if (upload != null) | |||
using (TeknikEntities db = new TeknikEntities()) | |||
{ | |||
if (User.Identity.IsAuthenticated) | |||
Models.Upload upload = Uploader.SaveFile(db, Config, data.InputStream, fileType, contentLength, encrypt, fileExt, iv, null, keySize, blockSize); | |||
if (upload != null) | |||
{ | |||
Users.Models.User user = UserHelper.GetUser(db, User.Identity.Name); | |||
if (user != null) | |||
if (User.Identity.IsAuthenticated) | |||
{ | |||
upload.UserId = user.UserId; | |||
db.Entry(upload).State = EntityState.Modified; | |||
db.SaveChanges(); | |||
Users.Models.User user = UserHelper.GetUser(db, User.Identity.Name); | |||
if (user != null) | |||
{ | |||
upload.UserId = user.UserId; | |||
db.Entry(upload).State = EntityState.Modified; | |||
db.SaveChanges(); | |||
} | |||
} | |||
return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url }), contentType = upload.ContentType, contentLength = StringHelper.GetBytesReadable(upload.ContentLength), deleteUrl = Url.SubRouteUrl("u", "Upload.Delete", new { file = upload.Url, key = upload.DeleteKey }) } }, "text/plain"); | |||
} | |||
return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url }), contentType = upload.ContentType, contentLength = StringHelper.GetBytesReadable(upload.ContentLength), deleteUrl = Url.SubRouteUrl("u", "Upload.Delete", new { file = upload.Url, key = upload.DeleteKey }) } }, "text/plain"); | |||
return Json(new { error = new { message = "Unable to upload file" } }); | |||
} | |||
return Json(new { error = new { message = "Unable to upload file" } }); | |||
} | |||
else | |||
{ | |||
@@ -122,98 +126,108 @@ namespace Teknik.Areas.Upload.Controllers | |||
if (Config.UploadConfig.DownloadEnabled) | |||
{ | |||
ViewBag.Title = "Teknik Download - " + file; | |||
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault(); | |||
if (upload != null) | |||
using (TeknikEntities db = new TeknikEntities()) | |||
{ | |||
upload.Downloads += 1; | |||
db.Entry(upload).State = EntityState.Modified; | |||
db.SaveChanges(); | |||
// We don't have the key, so we need to decrypt it client side | |||
if (string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV)) | |||
{ | |||
DownloadViewModel model = new DownloadViewModel(); | |||
model.FileName = file; | |||
model.ContentType = upload.ContentType; | |||
model.ContentLength = upload.ContentLength; | |||
model.IV = upload.IV; | |||
return View(model); | |||
} | |||
else // We have the key, so that means server side decryption | |||
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault(); | |||
if (upload != null) | |||
{ | |||
// Are they downloading it by range? | |||
bool byRange = !string.IsNullOrEmpty(Request.ServerVariables["HTTP_RANGE"]); // We do not support ranges | |||
// Check to see if they have a cache | |||
bool isCached = !string.IsNullOrEmpty(Request.Headers["If-Modified-Since"]); | |||
upload.Downloads += 1; | |||
db.Entry(upload).State = EntityState.Modified; | |||
db.SaveChanges(); | |||
if (isCached) | |||
// We don't have the key, so we need to decrypt it client side | |||
if (string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV)) | |||
{ | |||
// The file is cached, let's just 304 this | |||
Response.StatusCode = 304; | |||
Response.StatusDescription = "Not Modified"; | |||
Response.AddHeader("Content-Length", "0"); | |||
return Content(string.Empty); | |||
DownloadViewModel model = new DownloadViewModel(); | |||
model.FileName = file; | |||
model.ContentType = upload.ContentType; | |||
model.ContentLength = upload.ContentLength; | |||
model.IV = upload.IV; | |||
return View(model); | |||
} | |||
else | |||
else // We have the key, so that means server side decryption | |||
{ | |||
string subDir = upload.FileName[0].ToString(); | |||
string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, subDir, upload.FileName); | |||
if (System.IO.File.Exists(filePath)) | |||
// Are they downloading it by range? | |||
bool byRange = !string.IsNullOrEmpty(Request.ServerVariables["HTTP_RANGE"]); // We do not support ranges | |||
// Check to see if they have a cache | |||
bool isCached = !string.IsNullOrEmpty(Request.Headers["If-Modified-Since"]); | |||
if (isCached) | |||
{ | |||
// Add cache parameters | |||
Response.Cache.SetCacheability(HttpCacheability.Public); | |||
Response.Cache.SetMaxAge(new TimeSpan(365, 0, 0, 0)); | |||
Response.Cache.SetLastModified(upload.DateUploaded); | |||
// The file is cached, let's just 304 this | |||
Response.StatusCode = 304; | |||
Response.StatusDescription = "Not Modified"; | |||
Response.AddHeader("Content-Length", "0"); | |||
return Content(string.Empty); | |||
} | |||
else | |||
{ | |||
string subDir = upload.FileName[0].ToString(); | |||
string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, subDir, upload.FileName); | |||
if (System.IO.File.Exists(filePath)) | |||
{ | |||
// Add cache parameters | |||
Response.Cache.SetCacheability(HttpCacheability.Public); | |||
Response.Cache.SetMaxAge(new TimeSpan(365, 0, 0, 0)); | |||
Response.Cache.SetLastModified(upload.DateUploaded); | |||
// Notify the client the content length we'll be outputting | |||
Response.AddHeader("Content-Length", upload.ContentLength.ToString()); | |||
// Notify the client the content length we'll be outputting | |||
Response.AddHeader("Content-Length", upload.ContentLength.ToString()); | |||
// Create content disposition | |||
var cd = new System.Net.Mime.ContentDisposition | |||
{ | |||
FileName = upload.Url, | |||
Inline = true | |||
}; | |||
// Create content disposition | |||
var cd = new System.Net.Mime.ContentDisposition | |||
{ | |||
FileName = upload.Url, | |||
Inline = true | |||
}; | |||
Response.AddHeader("Content-Disposition", cd.ToString()); | |||
Response.AddHeader("Content-Disposition", cd.ToString()); | |||
string contentType = upload.ContentType; | |||
// We need to prevent html (make cleaner later) | |||
if (contentType == "text/html") | |||
{ | |||
contentType = "text/plain"; | |||
} | |||
string contentType = upload.ContentType; | |||
// We need to prevent html (make cleaner later) | |||
if (contentType == "text/html") | |||
{ | |||
contentType = "text/plain"; | |||
} | |||
// Read in the file | |||
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); | |||
// Read in the file | |||
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); | |||
// If the IV is set, and Key is set, then decrypt it while sending | |||
if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV)) | |||
{ | |||
byte[] keyBytes = Encoding.UTF8.GetBytes(upload.Key); | |||
byte[] ivBytes = Encoding.UTF8.GetBytes(upload.IV); | |||
try | |||
{ | |||
// If the IV is set, and Key is set, then decrypt it while sending | |||
if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV)) | |||
{ | |||
byte[] keyBytes = Encoding.UTF8.GetBytes(upload.Key); | |||
byte[] ivBytes = Encoding.UTF8.GetBytes(upload.IV); | |||
return new FileGenerateResult(upload.Url, | |||
contentType, | |||
(response) => ResponseHelper.StreamToOutput(response, true, new AESCryptoStream(fs, false, keyBytes, ivBytes, "CTR", "NoPadding"), (int)upload.ContentLength, Config.UploadConfig.ChunkSize), | |||
false); | |||
} | |||
else // Otherwise just send it | |||
{ | |||
// Don't buffer the response | |||
Response.Buffer = false; | |||
// Send the file | |||
return new FileGenerateResult(upload.Url, | |||
contentType, | |||
(response) => ResponseHelper.StreamToOutput(response, true, fs, (int)upload.ContentLength, Config.UploadConfig.ChunkSize), | |||
false); | |||
return new FileGenerateResult(upload.Url, | |||
contentType, | |||
(response) => ResponseHelper.StreamToOutput(response, true, new AESCryptoStream(fs, false, keyBytes, ivBytes, "CTR", "NoPadding"), (int)upload.ContentLength, Config.UploadConfig.ChunkSize), | |||
false); | |||
} | |||
else // Otherwise just send it | |||
{ | |||
// Don't buffer the response | |||
Response.Buffer = false; | |||
// Send the file | |||
return new FileGenerateResult(upload.Url, | |||
contentType, | |||
(response) => ResponseHelper.StreamToOutput(response, true, fs, (int)upload.ContentLength, Config.UploadConfig.ChunkSize), | |||
false); | |||
} | |||
} | |||
catch (Exception ex) | |||
{ | |||
Logging.Logger.WriteEntry(Logging.LogLevel.Warning, "Error in Download", ex); | |||
} | |||
} | |||
} | |||
} | |||
} | |||
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); | |||
} | |||
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); | |||
} | |||
return Redirect(Url.SubRouteUrl("error", "Error.Http403")); | |||
} | |||
@@ -224,19 +238,22 @@ namespace Teknik.Areas.Upload.Controllers | |||
{ | |||
if (Config.UploadConfig.DownloadEnabled) | |||
{ | |||
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault(); | |||
if (upload != null) | |||
using (TeknikEntities db = new TeknikEntities()) | |||
{ | |||
string subDir = upload.FileName[0].ToString(); | |||
string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, subDir, upload.FileName); | |||
if (System.IO.File.Exists(filePath)) | |||
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault(); | |||
if (upload != null) | |||
{ | |||
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); | |||
return File(fileStream, System.Net.Mime.MediaTypeNames.Application.Octet, file); | |||
string subDir = upload.FileName[0].ToString(); | |||
string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, subDir, upload.FileName); | |||
if (System.IO.File.Exists(filePath)) | |||
{ | |||
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); | |||
return File(fileStream, System.Net.Mime.MediaTypeNames.Application.Octet, file); | |||
} | |||
} | |||
Redirect(Url.SubRouteUrl("error", "Error.Http404")); | |||
return null; | |||
} | |||
Redirect(Url.SubRouteUrl("error", "Error.Http404")); | |||
return null; | |||
} | |||
Redirect(Url.SubRouteUrl("error", "Error.Http403")); | |||
return null; | |||
@@ -246,52 +263,58 @@ namespace Teknik.Areas.Upload.Controllers | |||
[AllowAnonymous] | |||
public ActionResult Delete(string file, string key) | |||
{ | |||
ViewBag.Title = "File Delete - " + file + " - " + Config.Title; | |||
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault(); | |||
if (upload != null) | |||
using (TeknikEntities db = new TeknikEntities()) | |||
{ | |||
DeleteViewModel model = new DeleteViewModel(); | |||
model.File = file; | |||
if (!string.IsNullOrEmpty(upload.DeleteKey) && upload.DeleteKey == key) | |||
ViewBag.Title = "File Delete - " + file + " - " + Config.Title; | |||
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault(); | |||
if (upload != null) | |||
{ | |||
string filePath = upload.FileName; | |||
// Delete from the DB | |||
db.Uploads.Remove(upload); | |||
db.SaveChanges(); | |||
DeleteViewModel model = new DeleteViewModel(); | |||
model.File = file; | |||
if (!string.IsNullOrEmpty(upload.DeleteKey) && upload.DeleteKey == key) | |||
{ | |||
string filePath = upload.FileName; | |||
// Delete from the DB | |||
db.Uploads.Remove(upload); | |||
db.SaveChanges(); | |||
// Delete the File | |||
if (System.IO.File.Exists(filePath)) | |||
// Delete the File | |||
if (System.IO.File.Exists(filePath)) | |||
{ | |||
System.IO.File.Delete(filePath); | |||
} | |||
model.Deleted = true; | |||
} | |||
else | |||
{ | |||
System.IO.File.Delete(filePath); | |||
model.Deleted = false; | |||
} | |||
model.Deleted = true; | |||
return View(model); | |||
} | |||
else | |||
{ | |||
model.Deleted = false; | |||
} | |||
return View(model); | |||
return RedirectToRoute("Error.Http404"); | |||
} | |||
return RedirectToRoute("Error.Http404"); | |||
} | |||
[HttpPost] | |||
public ActionResult GenerateDeleteKey(string file) | |||
{ | |||
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault(); | |||
if (upload != null) | |||
using (TeknikEntities db = new TeknikEntities()) | |||
{ | |||
if (upload.User.Username == User.Identity.Name) | |||
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault(); | |||
if (upload != null) | |||
{ | |||
string delKey = StringHelper.RandomString(Config.UploadConfig.DeleteKeyLength); | |||
upload.DeleteKey = delKey; | |||
db.Entry(upload).State = EntityState.Modified; | |||
db.SaveChanges(); | |||
return Json(new { result = new { url = Url.SubRouteUrl("u", "Upload.Delete", new { file = file, key = delKey }) } }); | |||
if (upload.User.Username == User.Identity.Name) | |||
{ | |||
string delKey = StringHelper.RandomString(Config.UploadConfig.DeleteKeyLength); | |||
upload.DeleteKey = delKey; | |||
db.Entry(upload).State = EntityState.Modified; | |||
db.SaveChanges(); | |||
return Json(new { result = new { url = Url.SubRouteUrl("u", "Upload.Delete", new { file = file, key = delKey }) } }); | |||
} | |||
return Json(new { error = new { message = "You do not own this upload" } }); | |||
} | |||
return Json(new { error = new { message = "You do not own this upload" } }); | |||
return Json(new { error = new { message = "Invalid URL" } }); | |||
} | |||
return Json(new { error = new { message = "Invalid URL" } }); | |||
} | |||
} | |||
} |
@@ -46,7 +46,7 @@ function processDownload(key) { | |||
lastTime = curTime; | |||
lastData = e.data.processed; | |||
var percentComplete = Math.round(e.data.processed * 100 / e.data.total); | |||
setProgress(percentComplete, 'progress-bar-success progress-bar-striped active', percentComplete + '%', 'Decrypting [' + getReadableBandwidthString(speed * 8) + ']'); | |||
setProgress(percentComplete, 'progress-bar-success progress-bar-striped active', percentComplete + '%', 'Decrypting [' + getReadableFileSizeString(e.data.processed) + ' / ' + getReadableFileSizeString(e.data.total) + ' @ ' + getReadableBandwidthString(speed * 8) + ']'); | |||
} | |||
break; | |||
case 'finish': | |||
@@ -92,7 +92,7 @@ function processDownload(key) { | |||
lastTime = curTime; | |||
lastData = e.loaded; | |||
var percentComplete = Math.round(e.loaded * 100 / e.total); | |||
setProgress(percentComplete, 'progress-bar-success progress-bar-striped active', percentComplete + '%', 'Downloading File [' + getReadableBandwidthString(speed * 8) + ']'); | |||
setProgress(percentComplete, 'progress-bar-success progress-bar-striped active', percentComplete + '%', 'Downloading File [' + getReadableFileSizeString(e.loaded) + ' / ' + getReadableFileSizeString(e.total) + ' @ ' + getReadableBandwidthString(speed * 8) + ']'); | |||
} | |||
}; | |||
@@ -217,7 +217,7 @@ function encryptFile(file, callback) { | |||
lastTime = curTime; | |||
lastData = e.data.processed; | |||
var percentComplete = Math.round(e.data.processed * 100 / e.data.total); | |||
setProgress(fileID, percentComplete, 'progress-bar-success progress-bar-striped active', percentComplete + '%', 'Encrypting [' + getReadableBandwidthString(speed * 8) + ']'); | |||
setProgress(fileID, percentComplete, 'progress-bar-success progress-bar-striped active', percentComplete + '%', 'Encrypting [' + getReadableFileSizeString(e.data.processed) + ' / ' + getReadableFileSizeString(e.data.total) + ' @ ' + getReadableBandwidthString(speed * 8) + ']'); | |||
} | |||
break; | |||
case 'finish': | |||
@@ -306,7 +306,7 @@ function uploadProgress(fileID, lastTime, lastData, evt) { | |||
setProgress(fileID, 100, 'progress-bar-success progress-bar-striped active', '', 'Processing Upload'); | |||
} | |||
else { | |||
setProgress(fileID, percentComplete, 'progress-bar-success progress-bar-striped active', percentComplete + '%', 'Uploading to Server [' + getReadableBandwidthString(speed * 8) + ']'); | |||
setProgress(fileID, percentComplete, 'progress-bar-success progress-bar-striped active', percentComplete + '%', 'Uploading to Server [' + getReadableFileSizeString(evt.loaded) + ' / ' + getReadableFileSizeString(evt.total) + ' @ ' + getReadableBandwidthString(speed * 8) + ']'); | |||
} | |||
} | |||
} |