Ejemplo n.º 1
0
        private static void UpDownAlignRemoteDirTree(IEnumerable<string> dirtree, StreamWriter sw, StreamReader sr)
        {
            HashSet<string> done = new HashSet<string>();
            // Garantiamo sempre che il dirTree sia ordinato per path name, così le cartelle saranno create in ordine gerarchico :)
            foreach (string s in dirtree)
            {
                string localPath = (new DirectoryInfo(Path.Combine(GetRootPath(), "." + s))).FullName;
                DirectoryInfo d = new DirectoryInfo(localPath);
                if (!d.Exists)
                    d.Create();
                done.Add(d.FullName);
            }

            string[] allDirectories = Directory.GetDirectories(GetRootPath().TrimEnd(Path.DirectorySeparatorChar), "*", SearchOption.AllDirectories);
            Array.Sort<string>(allDirectories, new Comparison<string>((string x, string y) => { return x.CompareTo(y); }));
            UserCredentials cred = UserCredentials.Instance;
            foreach (string directory in allDirectories)
            {
                string dir = directory.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
                if (!done.Contains(dir))
                {
                    DirectoryInfo di = new DirectoryInfo(dir);
                    string name = di.Name;
                    string absParent = di.Parent.FullName;
                    string relativeParent = UserUtils.FromAbsoluteDirToRelativeDir(absParent);
                    string relativePath = UserUtils.FromAbsoluteDirToRelativeDir(di.FullName);

                    // Pushare directory
                    CreateDirectoryRequest req = new CreateDirectoryRequest(relativePath, name, relativeParent, SyncToken.Instance.VFSVersion, cred.Username, cred.AccessToken);
                    UserUtils.NetworkWriteLine(sw, req.ToJSON());
                    string rec = UserUtils.NetworkReadLine(sr);
                    Response r = Response.fromJson(rec);
                    if (r.Result != 200)
                    {
                        throw new Exception("Bad response del file in seguito ad un push di una cartella.");
                    }
                }

            }
        }
        /// <summary>
        /// La funzione deve gestire la creazione di una nuova directory.
        /// </summary>
        public override bool Handle()
        {
            if(this.Stopped)
                return false;

            if (String.IsNullOrEmpty(base.FullPath)){
                throw new Exception("Path della directory invalido.");
            }
            using(TcpClient connection = new TcpClient()){
                connection.Connect(UserCredentials.Instance.Host, UserCredentials.Instance.Port);
                using (StreamReader sr = new StreamReader(connection.GetStream()))
                using (StreamWriter sw = new StreamWriter(connection.GetStream()))
                {
                    //sr.BaseStream.ReadTimeout = 10000;
                    //sw.BaseStream.WriteTimeout = 10000;

                    /************************************ Inizio della funzione ***************************************/

                    string username = UserCredentials.Instance.Username;
                    string accesstoken = UserCredentials.Instance.AccessToken;
                    string allPath = this.FullPath;
                    long client_vfsversion  = SyncToken.Instance.VFSVersion;

                    try
                    {
                        string syncroPath = UserUtils.GetRootPath();
                        if (!allPath.StartsWith(syncroPath))
                            throw new Exception("File o root non validi.");

                        //controlli
                        if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(accesstoken))
                        {
                            throw new Exception("Lo username o l'accesstoken non può essere nullo.");
                        }
                        if (!Directory.Exists(allPath))
                        {
                            throw new Exception("Il path assoluto "+allPath + " non costituisce una directory valida.");
                        }
                        DirectoryInfo di = new DirectoryInfo(allPath);

                        string name = di.Name;
                        string parent = (di.Parent.FullName.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar).Replace(UserUtils.GetRootPath(),""+Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar)+Path.DirectorySeparatorChar;
                        string relativePath = di.FullName.Replace(UserUtils.GetRootPath(), ""+Path.DirectorySeparatorChar)+Path.DirectorySeparatorChar;

                        CreateDirectoryRequest req = new CreateDirectoryRequest(relativePath, name, parent, client_vfsversion, username, accesstoken);
                        UserUtils.NetworkWriteLine(sw,req.ToJSON());
                        string recv = UserUtils.NetworkReadLine(sr);
                        Response r = Response.fromJson(recv);
                        if (r.Result == 401)
                        {
                            //problemi con la login T (token)
                            throw new UserLoginException(BadLoginResponse.fromJson(recv).Msg);
                        }
                        else if (r.Result != 200)
                        {
                            throw new Exception(BadCreateDirectoryResponse.fromJson(recv).Msg);
                        }
                    }
                    catch (UserLoginException e)
                    {
                        throw e;
                    }
                    catch (Exception e)
                    {
                        throw new CreateDirectoryExceptionU(e.Message);
                    }

                    /**************************************** Fine della funzione ***************************************/
                }

            }
            return true;
        }