//----< GET api/File?blockSize=2048 - get a block of bytes >-------

        public HttpResponseMessage Get(int blockSize)
        {
            // get FileStream and read block

            Models.Session    session   = new Models.Session();
            CookieHeaderValue cookie    = Request.Headers.GetCookies("session-id").FirstOrDefault();
            string            sessionId = cookie["session-id"].Value;
            FileStream        down      = session.getStream(sessionId);

            byte[] Block     = new byte[blockSize];
            int    bytesRead = down.Read(Block, 0, blockSize);

            if (bytesRead < blockSize) // compress block
            {
                byte[] returnBlock = new byte[bytesRead];
                for (int i = 0; i < bytesRead; ++i)
                {
                    returnBlock[i] = Block[i];
                }
                Block = returnBlock;
            }
            // make response message containing block and cookie

            HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);

            message.Headers.AddCookies(new CookieHeaderValue[] { cookie });
            message.Content = new ByteArrayContent(Block);
            return(message);
        }
        // POST api/file
        public HttpResponseMessage Post(int blockSize)
        {
            Task <byte[]> task = Request.Content.ReadAsByteArrayAsync();

            byte[]              Block    = task.Result;
            CookieHeaderValue   cookie   = Request.Headers.GetCookies("session-id").FirstOrDefault();
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NoContent);

            try
            {
                Models.Session session   = new Models.Session();
                string         sessionId = cookie["session-id"].Value;

                FileStream up = session.getStream(sessionId);
                up.Write(Block, 0, Block.Length);

                response.StatusCode = (HttpStatusCode)200;
            }
            catch
            {
                response.StatusCode = (HttpStatusCode)400;
            }
            finally
            {
                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);
        }
        //----< GET api/File?fileName=foobar.txt&open=true >---------------
        //----< attempt to open or close FileStream >----------------------

        public HttpResponseMessage Get(string fileName, string open)            // Cookie part is not understood
        {
            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("~\\Content\\MyStories\\download\\");
                if (open == "download")  // attempt to open requested fileName
                {
                    fs = new FileStream(path + fileName, FileMode.Open);
                    session.saveStream(fs, sessionId);
                }
                else if (open == "upload")
                {
                    fs = new FileStream(path + fileName, 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 #5
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;
        }
        // POST api/file
        public HttpResponseMessage Post(int blockSize)
        {
            Task <byte[]> task = Request.Content.ReadAsByteArrayAsync();

            byte[]            Block     = task.Result;
            Models.Session    session   = new Models.Session();
            CookieHeaderValue cookie    = Request.Headers.GetCookies("session-id").FirstOrDefault();
            string            sessionId = cookie["session-id"].Value;
            FileStream        up        = session.getStream(sessionId);

            up.Write(Block, 0, Block.Count());
            HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);

            message.Headers.AddCookies(new CookieHeaderValue[] { cookie });
            return(message);
        }
        // GET api/file/5
        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
            {
                string path = System.Web.HttpContext.Current.Server.MapPath("~\\Content\\image");
                string currentFileSpec = path + "\\" + fileName;
                FileStream down;
                if (open == "true")  // attempt to open requested fileName
                {
                    down = new FileStream(currentFileSpec, FileMode.Create);
                    session.saveStream(down, sessionId);
                }
                else  // close FileStream
                {
                    down = session.getStream(sessionId);
                    down.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?blockSize=2048 - get a block of bytes >-------

        public HttpResponseMessage Get(int blockSize)                           // What is session-ID and block size?
        {
            // get FileStream and read block

            Models.Session session = new Models.Session();
            CookieHeaderValue cookie = Request.Headers.GetCookies("session-id").FirstOrDefault();            
            string sessionId = cookie["session-id"].Value;
            FileStream down = session.getStream(sessionId);
            byte[] Block = new byte[blockSize];
            int bytesRead = down.Read(Block, 0, blockSize);
            if (bytesRead < blockSize)  // compress block
            {
                byte[] returnBlock = new byte[bytesRead];
                for (int i = 0; i < bytesRead; ++i)
                    returnBlock[i] = Block[i];
                Block = returnBlock;
            }
            // make response message containing block and cookie

            HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
            message.Headers.AddCookies(new CookieHeaderValue[] { cookie });
            message.Content = new ByteArrayContent(Block);
            return message;
        }
        // POST api/file
        public HttpResponseMessage Post(int blockSize)
        {
            Task<byte[]> task = Request.Content.ReadAsByteArrayAsync();
            byte[] Block = task.Result;
            CookieHeaderValue cookie = Request.Headers.GetCookies("session-id").FirstOrDefault();
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NoContent);

            try
            {
                Models.Session session = new Models.Session();
                string sessionId = cookie["session-id"].Value;

                FileStream up = session.getStream(sessionId);
                up.Write(Block, 0, Block.Length);

                response.StatusCode = (HttpStatusCode)200;
            }
            catch
            {
                response.StatusCode = (HttpStatusCode)400;
            }
            finally
            {
                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;
        }
 // POST api/file
 public HttpResponseMessage Post(int blockSize)
 {
     Task<byte[]> task = Request.Content.ReadAsByteArrayAsync();
       byte[] Block = task.Result;
       Models.Session session = new Models.Session();
       CookieHeaderValue cookie = Request.Headers.GetCookies("session-id").FirstOrDefault();
       string sessionId = cookie["session-id"].Value;
       FileStream up = session.getStream(sessionId);
       up.Write(Block, 0, Block.Count());
       HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
       message.Headers.AddCookies(new CookieHeaderValue[] { cookie });
       return message;
 }
        // POST api/file
        public void Post(HttpRequestMessage request, string FileName, string description, string siteName,string AddOrNot)
        {
            Task<byte[]> taskb = request.Content.ReadAsByteArrayAsync();
            byte[] Block = taskb.Result;
            Models.Session session = new Models.Session();
            string sessionId = session.getSessionId();
            FileStream down = session.getStream(sessionId);//save it into the session,get it from the 
            int blockSize = 512;
            if (Block.Length == 0 || blockSize <= 0)
                return;
            down.Write(Block, 0, Block.Length);
            if(Block.Length<512&&AddOrNot=="true")
            {
                Models.Site NewData = new Models.Site();
                NewData.Description = description;
                NewData.Image = FileName;
                NewData.SiteName = siteName;
                db.Sites.Add(NewData);
                db.SaveChanges();         
            }

        }
        //----< 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);
        }