public ActionResult Snippet(FormCollection formVars) { string sessionKey = Request.Params["sessionKey"]; CodePasteServiceBase service = new CodePasteServiceBase(); // load up snippet from posted values CodeSnippet snippet = new CodeSnippet(); this.UpdateModel(snippet); return this.ApiResult(service.PostNewCodeSnippet(snippet, sessionKey)); }
private bool ValidateForSpam(CodeSnippet snippet) { string strLength = Request.Form["qx"]; if (string.IsNullOrEmpty(strLength)) return false; int length = -1; int.TryParse(strLength,out length); // Simplistic form of validation if (snippet.Code.Replace("\r\n","\r").Length != length) return false; return true; }
public ActionResult New(string message = null) { ViewData["UserState"] = AppUserState; var snippet = new CodeSnippet(); snippet.Author = this.AppUserState.Name; string codeUrl = Request.QueryString["url"]; if (!string.IsNullOrEmpty(codeUrl)) { HttpClient client = new HttpClient(); client.Timeout = 4000; snippet.Code = client.DownloadString(codeUrl); snippet.Title = Path.GetFileName(codeUrl); string extension = Path.GetExtension(codeUrl); snippet.Language = CodeFormatFactory.GetStringLanguageFromExtension(extension); Response.Write(snippet.Language); } if (!string.IsNullOrEmpty(message)) this.ErrorDisplay.ShowMessage(message); return this.View("New",snippet); }
/// <summary> /// Allows posting of a new code snippet. /// </summary> /// <param name="snippet"></param> /// <param name="sessionKey"></param> /// <returns></returns> public CodeSnippet PostNewCodeSnippet(CodeSnippet snippet, string sessionKey) { User user = this.ValidateToken(sessionKey); using (busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet()) { if (snippet == null) this.ThrowException("Invalid snippet instance data passed"); CodeSnippet newSnippet = codeSnippet.NewEntity(); // Force userId regardless of what the user has set newSnippet.UserId = user.Id; newSnippet.Author = user.Name; if (string.IsNullOrEmpty(newSnippet.Author)) newSnippet.Author = snippet.Author; if (string.IsNullOrEmpty(snippet.Language)) snippet.Language = "NoFormat"; DataUtils.CopyObjectData(snippet, newSnippet, "Id,UserId,Entered,Views,UserId,User,Author,Comments"); if (!codeSnippet.Validate()) this.ThrowException("Snippet validation failed: " + codeSnippet.ValidationErrors.ToString()); if (codeSnippet.IsSpam()) this.ThrowException("Invalid Content."); if (!codeSnippet.Save()) this.ThrowException("Failed to save snippet: " + codeSnippet.ErrorMessage); return newSnippet; } }
/// <summary> /// Internally determines whether the snippet user is the same as the /// logged in user. /// </summary> /// <param name="entity"></param> /// <returns></returns> private bool IsEditAllowed(CodeSnippet entity) { if (string.IsNullOrEmpty(entity.UserId) || this.AppUserState.UserId != entity.UserId) return false; return true; }
/// <summary> /// Creates a GravatarLink for a snippet based on the user Email address. /// </summary> /// <param name="snippet"></param> /// <returns></returns> public static string GravatarLink(CodeSnippet snippet) { if (snippet.User == null) return string.Empty; string DefaultImg = WebUtils.ResolveServerUrl("~/images/space.gif"); return Gravatar.GetGravatarImage(snippet.User.Email, 70, "R", "style='opacity:.75;filter:alpha(opacity=\"75\");margin: 0 0 0 5px;' align='right' hspace='5'", DefaultImg); }