public ActionResult Create(FormCollection collection) { ViewBag.storyId = storyId; ViewBag.collageId = collageId; Models.Story story = new Models.Story(); Models.StoryBlock block = new Models.StoryBlock(); if (!doc.Element("collages").Elements("collage").Elements("story").Elements("block").Any()) { block.id = 0; } else { block.id = (int)(from S in doc.Descendants("block") orderby (int)S.Element("id") descending select (int)S.Element("id")).FirstOrDefault() + 1; } blockId = block.id; ViewBag.blockId = blockId; block.blockCaption = collection["BlockCaption"]; block.blockContent = collection["BlockContent"]; block.imgPath = ""+block.id; block.bolckOrder = collection["bolckOrder"]; story.Create(storyId, block); return View("Upload"); }
public ActionResult Create(FormCollection collection) { ViewBag.storyId = storyId; ViewBag.collageId = collageId; Models.Story story = new Models.Story(); Models.StoryBlock block = new Models.StoryBlock(); if (!doc.Element("collages").Elements("collage").Elements("story").Elements("block").Any()) { block.id = 0; } else { block.id = (int)(from S in doc.Descendants("block") orderby(int) S.Element("id") descending select(int) S.Element("id")).FirstOrDefault() + 1; } blockId = block.id; ViewBag.blockId = blockId; block.blockCaption = collection["BlockCaption"]; block.blockContent = collection["BlockContent"]; block.imgPath = "" + block.id; block.bolckOrder = collection["bolckOrder"]; story.Create(storyId, block); return(View("Upload")); }
//----< GET api/File?fileName=foobar.txt&open=true >--------------- //----< attempt to open or close FileStream >---------------------- public HttpResponseMessage Get(string fileName, string open) { string sessionId; var response = new HttpResponseMessage(); Models.Session session = new Models.Session(); CookieHeaderValue cookie = Request.Headers.GetCookies("session-id").FirstOrDefault(); if (cookie == null) { sessionId = session.incrSessionId(); cookie = new CookieHeaderValue("session-id", sessionId); cookie.Expires = DateTimeOffset.Now.AddDays(1); cookie.Domain = Request.RequestUri.Host; cookie.Path = "/"; } else { sessionId = cookie["session-id"].Value; } try { FileStream fs; string path = System.Web.HttpContext.Current.Server.MapPath("~\\Img\\"); if (open == "download") // attempt to open requested fileName { PrepDownloadController pd = new PrepDownloadController(); pd.Index(Int32.Parse(fileName), "api"); path = path + "collage"; string currentFileSpec = path + "\\" + fileName + ".zip" ; fs = new FileStream(currentFileSpec, FileMode.Open); session.saveStream(fs, sessionId); } else if(open == "upload") { string[] items = fileName.Split('?'); string storyId = items[0]; string blkTitle = items[1]; string blkContent = items[2]; string blkOrder = items[3]; string imgPath = items[4]; string XMLpath = System.Web.HttpContext.Current.Server.MapPath("~\\App_Data\\Collages.xml"); XDocument doc = XDocument.Load(XMLpath); Models.Story story = new Models.Story(); Models.StoryBlock block = new Models.StoryBlock(); if (!doc.Element("collages").Elements("collage").Elements("story").Elements("block").Any()) { block.id = 0; } else { block.id = (int)(from S in doc.Descendants("block") orderby (int)S.Element("id") descending select (int)S.Element("id")).FirstOrDefault() + 1; } block.blockCaption = blkTitle; block.blockContent = blkContent; block.imgPath = storyId + "_" + block.id + Path.GetExtension(imgPath); block.bolckOrder = blkOrder; story.Create(Int32.Parse(storyId), block); path = path + storyId; if (!Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } string currentFileSpec = path + "\\" + block.imgPath; fs = new FileStream(currentFileSpec, FileMode.OpenOrCreate); session.saveStream(fs, sessionId); } else // close FileStream { fs = session.getStream(sessionId); session.removeStream(sessionId); fs.Close(); } response.StatusCode = (HttpStatusCode)200; } catch { response.StatusCode = (HttpStatusCode)400; } finally // return cookie to save current sessionId { response.Headers.AddCookies(new CookieHeaderValue[] { cookie }); } return response; }
//----< GET api/File?fileName=foobar.txt&open=true >--------------- //----< attempt to open or close FileStream >---------------------- public HttpResponseMessage Get(string fileName, string open) { string sessionId; var response = new HttpResponseMessage(); Models.Session session = new Models.Session(); CookieHeaderValue cookie = Request.Headers.GetCookies("session-id").FirstOrDefault(); if (cookie == null) { sessionId = session.incrSessionId(); cookie = new CookieHeaderValue("session-id", sessionId); cookie.Expires = DateTimeOffset.Now.AddDays(1); cookie.Domain = Request.RequestUri.Host; cookie.Path = "/"; } else { sessionId = cookie["session-id"].Value; } try { FileStream fs; string path = System.Web.HttpContext.Current.Server.MapPath("~\\Img\\"); if (open == "download") // attempt to open requested fileName { PrepDownloadController pd = new PrepDownloadController(); pd.Index(Int32.Parse(fileName), "api"); path = path + "collage"; string currentFileSpec = path + "\\" + fileName + ".zip"; fs = new FileStream(currentFileSpec, FileMode.Open); session.saveStream(fs, sessionId); } else if (open == "upload") { string[] items = fileName.Split('?'); string storyId = items[0]; string blkTitle = items[1]; string blkContent = items[2]; string blkOrder = items[3]; string imgPath = items[4]; string XMLpath = System.Web.HttpContext.Current.Server.MapPath("~\\App_Data\\Collages.xml"); XDocument doc = XDocument.Load(XMLpath); Models.Story story = new Models.Story(); Models.StoryBlock block = new Models.StoryBlock(); if (!doc.Element("collages").Elements("collage").Elements("story").Elements("block").Any()) { block.id = 0; } else { block.id = (int)(from S in doc.Descendants("block") orderby(int) S.Element("id") descending select(int) S.Element("id")).FirstOrDefault() + 1; } block.blockCaption = blkTitle; block.blockContent = blkContent; block.imgPath = storyId + "_" + block.id + Path.GetExtension(imgPath); block.bolckOrder = blkOrder; story.Create(Int32.Parse(storyId), block); path = path + storyId; if (!Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } string currentFileSpec = path + "\\" + block.imgPath; fs = new FileStream(currentFileSpec, FileMode.OpenOrCreate); session.saveStream(fs, sessionId); } else // close FileStream { fs = session.getStream(sessionId); session.removeStream(sessionId); fs.Close(); } response.StatusCode = (HttpStatusCode)200; } catch { response.StatusCode = (HttpStatusCode)400; } finally // return cookie to save current sessionId { response.Headers.AddCookies(new CookieHeaderValue[] { cookie }); } return(response); }