//----< 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("~\\App_Data\\");
                if (open == "download") // attempt to open requested fileName
                {
                    path = path + "DownLoad";
                    string currentFileSpec = path + "\\" + fileName;
                    fs = new FileStream(currentFileSpec, FileMode.Open);
                    session.saveStream(fs, sessionId);
                }
                else if (open == "upload")
                {
                    path = path + "UpLoad";
                    string currentFileSpec = path + "\\" + fileName;
                    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("~\\App_Data\\");
            if (open == "download")  // attempt to open requested fileName
            {
              path = path + "DownLoad";
              string currentFileSpec = path + "\\" + fileName;
              fs = new FileStream(currentFileSpec, FileMode.Open);
              session.saveStream(fs, sessionId);
            }
            else if(open == "upload")
            {
              path = path + "UpLoad";
              string currentFileSpec = path + "\\" + fileName;
              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;
        }
Example #3
0
        //----< 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("~\\ZipFiles");
                if (open == "download") {
                    // attempt to open requested fileName
                    string currentFileSpec = path + "\\" + fileName;
                    fs = new FileStream(currentFileSpec, FileMode.Open);
                    session.saveStream(fs, sessionId);
                } else if (open == "upload") {
                    string path1 = System.Web.HttpContext.Current.Server.MapPath("~\\CollagePages\\UploadedFiles\\");
                    string a = Path.GetFileName(fileName);
                    string currentFileSpec = path1 + "\\" + a;
                    fs = new FileStream(currentFileSpec, FileMode.OpenOrCreate);
                    session.saveStream(fs, sessionId);
                } else {
                    // close FileStream 
                    fs = session.getStream(sessionId);
                    session.removeStream(sessionId);
                    fs.Close();
                    string storyXML = HttpContext.Current.Server.MapPath("~\\CollagePages\\UploadedFiles\\") + "story.xml";
                    if (System.IO.File.Exists(storyXML))
                        addStoryBlock(storyXML);
                }
                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;
        }
        //----< 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);
        }