예제 #1
0
        /// <summary>
        /// Not implemented yet
        /// </summary>
        /// <param name="f"></param>
        /// <param name="newFullPath"></param>
        internal static void RenameFile(File f, string newFullPath)
        {
            string     postData = String.Format("fullpath={0}&newfullpath={1}", f.fullPath, newFullPath);
            WebRequest req      = OrangeCloudServer.NewRequest("rename", "POST", null);


            WebResponse resp = req.GetResponse();
        }
예제 #2
0
        /// <summary>
        /// Add a file to the cloud
        /// </summary>
        /// <param name="f">file to add</param>
        /// <returns>File object with updated version number</returns>
        internal static File Add(File f)
        {
            string postData = "content=" + Newtonsoft.Json.JsonConvert.SerializeObject(f.content) +
                              "&fullpath=" + f.fullPath +
                              "&checksum=" + f.checksum +
                              "&base_version=" + f.version;

            WebRequest req = OrangeCloudServer.NewRequest("add", "POST", null);

            req.ContentType = "application/x-www-form-urlencoded";

            try
            {
                byte[] write = Encoding.UTF8.GetBytes(postData);
                req.ContentLength = postData.Length;
                Stream       dataStream = req.GetRequestStream();
                StreamWriter sw         = new StreamWriter(dataStream, Encoding.UTF8);
                sw.Write(postData);
                sw.Close();
                dataStream.Close();

                WebResponse  resp     = req.GetResponse();
                MemoryStream response = new MemoryStream();

                // Allocate a 1k buffer
                byte[] buffer = new byte[1024];
                int    bytesRead;

                // Simple do/while loop to read from stream until
                // no bytes are returned
                do
                {
                    // Read data (up to 1k) from the stream
                    bytesRead = resp.GetResponseStream().Read(buffer, 0, buffer.Length);

                    // Write the data to the local file
                    response.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);

                response.Seek(0, SeekOrigin.Begin);

                if (response.Length > 0)
                {
                    StreamReader sr = new StreamReader(response);
                    return(JsonConvert.DeserializeObject <File>(sr.ReadToEnd()));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Error getting file: {0} : {1} ({2})", f.fullPath, f.version, ex.Message));

                return(null);
            }
        }
예제 #3
0
        /// <summary>
        /// Not implemented yet
        /// </summary>
        /// <param name="f"></param>
        /// <returns></returns>
        internal static File DeleteFile(File f)
        {
            string postData = "content=&fullpath=" + f.fullPath +
                              "&checksum=" + f.checksum +
                              "&base_version=" + f.version;

            WebRequest req = OrangeCloudServer.NewRequest("add", "POST", null);

            req.ContentLength = postData.Length;
            Stream       dataStream = req.GetRequestStream();
            StreamWriter sw         = new StreamWriter(dataStream);

            sw.Write(postData);
            sw.Close();
            dataStream.Close();

            WebResponse  resp;
            MemoryStream response = new MemoryStream();

            try
            {
                resp = req.GetResponse();

                // Allocate a 1k buffer
                byte[] buffer = new byte[1024];
                int    bytesRead;

                // Simple do/while loop to read from stream until
                // no bytes are returned
                do
                {
                    // Read data (up to 1k) from the stream
                    bytesRead = resp.GetResponseStream().Read(buffer, 0, buffer.Length);

                    // Write the data to the local file
                    response.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);

                response.Seek(0, SeekOrigin.Begin);
            }
            catch (Exception)
            {
                Console.WriteLine(string.Format("Error deleting file: {0} : {1}", f.fullPath, f.version));
            }


            if (response.Length > 0)
            {
                StreamReader sr = new StreamReader(response);
                return(JsonConvert.DeserializeObject <File>(sr.ReadToEnd()));
            }
            else
            {
                return(null);
            }
        }
예제 #4
0
        /// <summary>
        /// Get the current file listing from the server
        /// </summary>
        /// <returns>Hashset of File objects</returns>
        internal static HashSet <File> GetFiles()
        {
            WebRequest req = OrangeCloudServer.NewRequest("files", "GET", null);



            WebResponse  resp;
            MemoryStream response = new MemoryStream();

            try
            {
                resp = req.GetResponse();

                // Allocate a 1k buffer
                byte[] buffer = new byte[1024];
                int    bytesRead;

                // Simple do/while loop to read from stream until
                // no bytes are returned
                do
                {
                    // Read data (up to 1k) from the stream
                    bytesRead = resp.GetResponseStream().Read(buffer, 0, buffer.Length);

                    // Write the data to the local file
                    response.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);

                response.Seek(0, SeekOrigin.Begin);

                if (response.Length > 0)
                {
                    StreamReader sr = new StreamReader(response);

                    return(Newtonsoft.Json.JsonConvert.DeserializeObject <HashSet <File> >(sr.ReadToEnd()));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception)
            {
                Console.WriteLine(string.Format("Error getting file listing"));
            }

            return(null);
        }
        private void DownloadNewFiles(HashSet <File> newFiles)
        {
            // un gzip

            foreach (File f in newFiles)
            {
                File download = OrangeCloudServer.GetFile(f);

                if (download != null)
                {
                    //if ((download.content != null) && (download.content.Length > 0))
                    //{
                    //    System.IO.MemoryStream mOut = new System.IO.MemoryStream();

                    //    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(download.content))
                    //    {
                    //        using (System.IO.Compression.GZipStream compressed = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
                    //        {
                    //            compressed.CopyTo(mOut);
                    //        }

                    //        download.content = mOut.ToArray();
                    //    }
                    //}



                    if (download.content == null)
                    {
                        System.IO.File.Delete(repoPath + download.fullPath);
                        // update meta data
                        ShareMetaData.Remove(download);
                    }
                    else
                    {
                        System.IO.File.WriteAllBytes(repoPath + download.fullPath, download.content);
                        // update meta data
                        ShareMetaData.Add(download);
                    }
                }
            }
        }
        /// <summary>
        /// Updating the repo:
        /// 1. load saved share meta data (meta data will be as current as last time online user was online)
        /// 2. load current state of share folder
        /// 3. compare the two sets for adds/mods/deletes
        /// 4. push changes to the server, updating meta data on success
        ///    a. if there is a conflicted file, copy it locally using name server gives
        /// 5. ask server for current file list
        /// 6. compare this list against local share
        /// 7. download out-of-date and new files, updating meta data as we go
        /// 8. write out meta data
        /// </summary>
        public void SyncWithTheCloud()
        {
            // 1. load saved share meta data (meta data will be as current as last time online user was online)
            ShareMetaData = this.loadMetaData();

            // 2. load current state of share folder
            LocalShare = this.GetFiles();

            // 3. compare the two sets for adds/mods/deletes
            HashSet <File> changeSet = this.GetChangeSet(this.LocalShare, this.ShareMetaData, new StartupComparer());

            // 4. push changes to the server, updating meta data on success
            foreach (File f in changeSet)
            {
                // differentiate deletes from adds
                if (f.content == null) // delete
                {
                    File deleteResult = OrangeCloudServer.DeleteFile(f);

                    // if the file is null, the server didnt respond
                    // we'll do nothing now and try to push again later
                    if (deleteResult == null)
                    {
                        continue;
                    }
                    // if server returns -1, we have a conflict
                    else if (deleteResult.version == -1)
                    {
                        // do something
                    }
                    // operation was successful with server
                    else
                    {
                        // update file meta data
                        File metaFile = ShareMetaData.Where(file => file.fullPath == f.fullPath).First();

                        if (metaFile != null)
                        {
                            ShareMetaData.Remove(metaFile);
                        }
                    }
                }
                else // add
                {
                    File addResult = OrangeCloudServer.Add(f);

                    // if the file is null, the server didnt respond
                    // we'll do nothing now and try to push again later
                    if (addResult == null)
                    {
                        continue;
                    }
                    // if server returns -1, we have a conflict
                    else if (addResult.version == -1)
                    {
                        // do something
                    }
                    // operation was successful with server
                    else
                    {
                        // update file meta data

                        // if the file is in the meta data already
                        if (ShareMetaData.Contains(f))
                        {
                            // update the version
                            ShareMetaData.SingleOrDefault(file => file.fullPath == f.fullPath).version = addResult.version;
                        }
                        // add it to the meta data
                        else
                        {
                            f.version = addResult.version;
                            ShareMetaData.Add(f);
                        }
                    }
                }
            }

            changeSet.RemoveWhere(files => files.content == null);

            // 5. ask server for current file list
            HashSet <File> ServerList = OrangeCloudServer.GetFiles();

            // 6. compare this list against local share
            HashSet <File> newFiles = this.GetChangeSet(ServerList, LocalShare, new DefaultComparer());

            // 7. download out-of-date and new files, updating meta data as we go
            this.DownloadNewFiles(newFiles);

            // 8. write out meta data
            System.IO.File.WriteAllText(Environment.CurrentDirectory + "\\metadata.json", JsonConvert.SerializeObject(ShareMetaData));
        }