private string CreateLink(ShareToken shareToken) { var baseUrl = System.Configuration.ConfigurationManager.AppSettings["ShareBaseUrl"]; if (string.IsNullOrEmpty(baseUrl)) { return Url.Action("Index", "Link", new { ShareToken = shareToken.ToBase64String() }, Request.Url.Scheme); } baseUrl = baseUrl.TrimEnd('/'); return string.Concat(baseUrl,Url.Action("Index", "Link", new { ShareToken = shareToken.ToBase64String() })); }
public ActionResult Links(SharePublishModel model) { if (ModelState.IsValid) { using (var db = new BusinessLogic.Data.SharingDataContext()) { var set = db.FileSets.Single(x => x.FileSetID == model.FileSetID); var shareExpires = model.ExpirationDate; var fileLinks = new List<object>(); var shareToken = new ShareToken(BusinessLogic.SecureRandom.GetRandomBytes(20), 0); byte[] passwordBytes = null; if (shareExpires > DateTime.UtcNow.AddDays(this.MaxShareTTL)) { shareExpires = DateTime.UtcNow.AddDays(this.MaxShareTTL); } if (model.RequirePassword) { passwordBytes = Convert.FromBase64String(System.Web.Helpers.Crypto.HashPassword(model.Password)); } set.Name = model.Name; set.IsDraft = false; var setShare = new BusinessLogic.Data.SharedFileSet() { ShareToken = shareToken.GetBytes(), FileSet = set, OwnerUsername = this.User.Identity.Name, ExpirationDateTime = shareExpires, Password = passwordBytes, AllowUpload = model.AllowUpload, CreateUsername = this.User.Identity.Name, CreateDateTime = DateTime.UtcNow }; db.SharedFileSets.Add(setShare); BusinessLogic.Data.SharedFile fileShare; ShareToken fileShareToken; if (set.StoredFiles.Count > 1) { foreach (var file in set.StoredFiles) { fileShareToken = new ShareToken(BusinessLogic.SecureRandom.GetRandomBytes(20), file.FileID); fileShare = new BusinessLogic.Data.SharedFile() { ShareToken = fileShareToken.GetBytes(), StoredFile = file, OwnerUsername = this.User.Identity.Name, ExpirationDateTime = shareExpires, Password = passwordBytes, CreateUsername = this.User.Identity.Name, CreateDateTime = DateTime.UtcNow }; db.SharedFiles.Add(fileShare); fileLinks.Add(new { url = this.CreateLink(fileShareToken), fileName = file.Filename, length = file.Length }); } } db.SaveChanges(); return Json(new { success = true, setLink = this.CreateLink(shareToken), fileLinks = fileLinks }); } } return Json(new { success = false, errorMessage = "Something wasn't right about the data you provided." }); }