yavsc/web/ApiControllers/BlogsController.cs
Paul Schneider 9494d6f353 Hallo now edits my images,
and each post can display a dedicated photo.

* NpgsqlBlogProvider.cs: implements a blog post photo storage

* BlogsController.cs: implements a method to update the photo url

* style.css: yastyle

* AdminController.cs: refactoring the notification:
Introduces a static `Notice` method, server side, to populate an array
in `ViewData`, used in the paster page.

* BlogsController.cs: Controls the photo update

* YavscHelpers.cs:
* yavsc.circles.js:
* HomeController.cs:
* GoogleController.cs: notification refactoring

* App.master: - notification refactoring
- html structure in the `nav`

* hallo.js: event 'hallomodified' now also occurs at image
  modifications

* to-markdown.js: ?Fixes? html images alt text and title to Markdown

* yavsc.js: implements the photo in database

* Edit.aspx: A nicer bill edition, with a photo

* UserPost.aspx: Displays the photo

* UserPosts.aspx: Fixes the new usage of `ResultPages`

* Web.config: totem custo

* instdbws.sql: adds a `photo` field in the `blog` table

* BlogEntry.cs: defines the photo in the model

* BlogManager.cs: a new method to set the photo on a blog post.

* BlogProvider.cs: the blog provider now also gives some photo

* LocalizedText.fr.Designer.cs: Reordering the french localisation
  resource

* LocalizedText.fr.resx: Reorders the french localisation resource
2015-10-08 12:43:04 +02:00

226 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Http;
using Npgsql.Web.Blog;
using Yavsc.Model.Blogs;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Diagnostics;
using Yavsc.Formatters;
using Yavsc.Model;
namespace Yavsc.ApiControllers
{
/// <summary>
/// Blogs API controller.
/// </summary>
public class BlogsController : YavscApiController
{
private const string adminRoleName = "Admin";
/// <summary>
/// Initialize the specified controllerContext.
/// </summary>
/// <param name="controllerContext">Controller context.</param>
protected override void Initialize (System.Web.Http.Controllers.HttpControllerContext controllerContext)
{
base.Initialize (controllerContext);
if (!Roles.RoleExists (adminRoleName)) {
Roles.CreateRole (adminRoleName);
}
}
/// <summary>
/// Tag the specified postid and tag.
/// </summary>
/// <param name="postid">Postid.</param>
/// <param name="tag">Tag.</param>
public long Tag (long postid,string tag) {
BlogManager.GetForEditing (postid);
return BlogManager.Tag (postid, tag);
}
/// <summary>
/// Removes the post.
/// </summary>
/// <param name="user">User.</param>
/// <param name="title">Title.</param>
[Authorize]
public void RemoveTitle(string user, string title) {
if (Membership.GetUser ().UserName != user)
if (!Roles.IsUserInRole("Admin"))
throw new AuthorizationDenied (user);
BlogManager.RemoveTitle (user, title);
}
/// <summary>
/// Removes the tag.
/// </summary>
/// <param name="tagid">Tagid.</param>
public void RemoveTag(long tagid) {
throw new NotImplementedException ();
}
/// <summary>
/// The allowed media types.
/// </summary>
protected string[] allowedMediaTypes = {
"text/plain",
"text/x-tex",
"text/html",
"image/png",
"image/gif",
"image/jpeg",
"image/x-xcf",
"application/pdf",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
};
/// <summary>
/// Posts the file.
/// </summary>
/// <returns>The file.</returns>
[Authorize,HttpPost]
public async Task<HttpResponseMessage> PostFile(long id) {
if (!(Request.Content.Headers.ContentType.MediaType=="multipart/form-data"))
throw new HttpRequestException ("not a multipart/form-data request");
BlogEntry be = BlogManager.GetPost (id);
if (be.Author != Membership.GetUser ().UserName)
throw new AuthorizationDenied ("b"+id);
string root = HttpContext.Current.Server.MapPath("~/bfiles/"+id);
DirectoryInfo di = new DirectoryInfo (root);
if (!di.Exists) di.Create ();
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider) ;
var invalidChars = Path.GetInvalidFileNameChars();
foreach (string fkey in provider.BodyPartFileNames.Keys)
{
string filename = provider.BodyPartFileNames[fkey];
Trace.WriteLine(filename);
string nicename = HttpUtility.UrlDecode(fkey) ;
if (fkey.StartsWith("\"") && fkey.EndsWith("\"") && fkey.Length > 2)
nicename = fkey.Substring(1,fkey.Length-2);
nicename = new string (nicename.Where( x=> !invalidChars.Contains(x)).ToArray());
nicename = nicename.Replace(' ','_');
var dest = Path.Combine(root,nicename);
var fi = new FileInfo(dest);
if (fi.Exists) fi.Delete();
File.Move(filename, fi.FullName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
}
}
/// <summary>
/// Searchs the files accociated to the given post id,
/// or related to the given terms.
/// </summary>
/// <returns>The file.</returns>
/// <param name="postid">Postid.</param>
/// <param name="terms">Terms.</param>
[Authorize,HttpGet]
public async Task<HttpResponseMessage> SearchFile(long postid, string terms) {
throw new NotImplementedException ();
}
/// <summary>
/// Sets the photo.
/// </summary>
/// <param name="id">Identifier.</param>
/// <param name="photo">Photo.</param>
[Authorize,HttpPost]
public void SetPhoto(long id, string photo)
{
BlogManager.Provider.UpdatePostPhoto (id, photo);
}
/// <summary>
/// Import the specified id.
/// </summary>
/// <param name="id">Identifier.</param>
public async Task<HttpResponseMessage> Import(long id) {
if (!(Request.Content.Headers.ContentType.MediaType=="multipart/form-data"))
throw new HttpRequestException ("not a multipart/form-data request");
BlogEntry be = BlogManager.GetPost (id);
if (be.Author != Membership.GetUser ().UserName)
throw new AuthorizationDenied ("post: "+id);
string root = HttpContext.Current.Server.MapPath("~/bfiles/"+id);
DirectoryInfo di = new DirectoryInfo (root);
if (!di.Exists) di.Create ();
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider) ;
var invalidChars = Path.GetInvalidFileNameChars();
List<string> bodies = new List<string>();
foreach (string fkey in provider.BodyPartFileNames.Keys)
{
string filename = provider.BodyPartFileNames[fkey];
string nicename=fkey;
if (fkey.StartsWith("\"") && fkey.EndsWith("\"") && fkey.Length > 2)
nicename = fkey.Substring(1,fkey.Length-2);
var filtered = new string (nicename.Where( x=> !invalidChars.Contains(x)).ToArray());
FileInfo fi = new FileInfo(filtered);
FileInfo fo = new FileInfo(filtered+".md");
FileInfo fp = new FileInfo (Path.Combine(root,filename));
if (fi.Exists) fi.Delete();
fp.MoveTo(fi.FullName);
using (Process p = new Process ()) {
p.StartInfo.WorkingDirectory = root;
p.StartInfo = new ProcessStartInfo ();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "/usr/bin/pandoc";
p.StartInfo.Arguments =
string.Format (" -o '{0}' -t markdown '{1}'",
fo.FullName,
fi.FullName);
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start ();
p.WaitForExit ();
if (p.ExitCode != 0) {
return Request.CreateResponse (HttpStatusCode.InternalServerError,
"# Import failed with exit code: " + p.ExitCode + "---\n"
+ LocalizedText.ImportException + "---\n"
+ p.StandardError.ReadToEnd() + "---\n"
+ p.StandardOutput.ReadToEnd()
);
}
}
bodies.Add(fo.OpenText().ReadToEnd());
fi.Delete();
fo.Delete();
}
return Request.CreateResponse(HttpStatusCode.OK,string.Join("---\n",bodies),new SimpleFormatter("text/plain"));
}
catch (System.Exception e)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
}
}
}
}