Example #1
0
        public MetaData CreateFolder(MetaData parent, string folderName)
        {
            Http http = new Http("get");
            http.Url = new Uri("https://api.dropbox.com/0/fileops/create_folder");

            OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret);

            http.Parameters.Add(new HttpParameter { Name = "path", Value = string.Format("{0}/{1}", parent.Path, folderName) });
            http.Parameters.Add(new HttpParameter { Name = "root", Value = "dropbox" });

            oAuth.Authenticate(http);

            try
            {
                http.Get();

                BytesRecieved += http.Response.Content.Length;

                JsonDeserializer deserializer = new JsonDeserializer();

                var returnMeta = deserializer.Deserialize<MetaData>(http.Response.Content);

                if (string.IsNullOrEmpty(returnMeta.error))
                {
                    if (returnMeta.Contents == null) returnMeta.Contents = new List<MetaData>();
                    return returnMeta;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
Example #2
0
        private void Sync(SyncFolder syncFolder, string dropPath)
        {
            //Try create the folder...
            var parentMeta = new MetaData { Path = dropPath };

            var tempMeta = new MetaData { Path = string.Format("{0}/{1}", dropPath, syncFolder.Name) };

            var folderMeta = Syncer.Instance.DropBox.GetItems(tempMeta);

            if (folderMeta == null)
            {
                //Doesnt exist on Dropbox
                //if its the Sync Root Create it anyway...
                if (syncFolder.Parent == null)
                {
                    folderMeta = Syncer.Instance.DropBox.CreateFolder(parentMeta, syncFolder.Name);
                }
                else
                {
                    //Not the root SyncFolder
                    if (syncFolder.LastSync == DateTime.MinValue)
                    {
                        //not been synced before (is new)
                        folderMeta = Syncer.Instance.DropBox.CreateFolder(parentMeta, syncFolder.Name);
                    }
                    else
                    {
                        //has been synced before
                        if (syncFolder.Created > syncFolder.LastSync)
                        {
                            //Created after last sync, new
                            folderMeta = Syncer.Instance.DropBox.CreateFolder(parentMeta, syncFolder.Name);
                        }
                        else
                        {
                            //Folder deleted from dropbox after last sync
                            try
                            {
                                Directory.Delete(syncFolder.Path);
                            }
                            catch { }
                            return; //End current loop
                        }
                    }
                }

                //if folderMeta is still null, data fail
                if (folderMeta == null)
                {
                    Form1.Instance.DoAction(() =>
                    {
                        MessageDialog.Show("Syncing failed" + Environment.NewLine + "Check Data Connection", "OK", null);
                    });
                    return;
                }
            }

            if (folderMeta.Contents == null) folderMeta.Contents = new List<MetaData>();

            var folders = syncFolder.GetSubFolders();

            foreach (SyncFolder subFolder in folders)
            {
                subFolder.Parent = syncFolder;
                Sync(subFolder, string.Format("{0}/{1}", dropPath, syncFolder.Name));
            }

            Syncer.Instance.CurrentFolderPath = syncFolder.Path;

            var files = syncFolder.GetFiles();

            foreach (SyncFile file in files)
            {
                try
                {
                    UpdateSyncStatuses(file.Name, SyncStatus.Checking);
                    //Check for extension and size exclusions
                    if (Settings.Instance.MaxSizeMB > 0)
                    {
                        if (Settings.Instance.MaxSizeMB < (file.Size / 1024.00 / 1024.00))
                        {
                            //File too big...
                            //Add to the Syncer thing
                            //Syncer.Instance.FilesSkipped.Add(file);
                            continue;
                        }
                    }
                    //Check file against the dropbox 1
                    var fileMeta = folderMeta.Contents.SingleOrDefault(f => f.Name == file.Name);
                    if (fileMeta != null)
                    {
                        //file exists on dropbox check if modified after last sync
                        if (syncFolder.LastSync == DateTime.MinValue)
                        {
                            //First Time syncing said folder and file exists on both...
                            if (fileMeta.Modified < TimeZone.CurrentTimeZone.ToUniversalTime(file.LastMod))
                            {
                                //phone file is newer (upload)
                                UpdateSyncStatuses(SyncStatus.Uploading);
                                Syncer.Instance.DropBox.UploadFile(new FileInfo(file.Path), folderMeta);
                                //Add to the Syncer thing
                                //Syncer.Instance.FilesUp.Add(file);
                            }
                            else
                            {
                                //update phone files (download)   new FileInfo(file.Path), folderMeta
                                UpdateSyncStatuses(SyncStatus.Downloading);
                                var downloadFile = Syncer.Instance.DropBox.GetFile(fileMeta);

                                if (downloadFile != null)
                                {
                                    downloadFile.SaveFile(file.Path);
                                    //Add to the Syncer thing
                                    //Syncer.Instance.FilesDown.Add(file);
                                }
                            }
                        }
                        else
                        {
                            //Not first time syncing folder
                            if (fileMeta.Modified > TimeZone.CurrentTimeZone.ToUniversalTime(file.LastMod))
                            {
                                if (fileMeta.Modified > TimeZone.CurrentTimeZone.ToUniversalTime(syncFolder.LastSync))
                                {
                                    //update phone files (download)   new FileInfo(file.Path), folderMeta
                                    UpdateSyncStatuses(SyncStatus.Downloading);
                                    var downloadFile = Syncer.Instance.DropBox.GetFile(fileMeta);

                                    if (downloadFile != null)
                                    {
                                        downloadFile.SaveFile(file.Path);
                                        //Add to the Syncer thing
                                        //Syncer.Instance.FilesDown.Add(file);
                                    }
                                }
                            }
                            else
                            {
                                if (file.LastMod > syncFolder.LastSync) //Both Local
                                {
                                    //phone file is newer (upload)
                                    UpdateSyncStatuses(SyncStatus.Uploading);
                                    Syncer.Instance.DropBox.UploadFile(new FileInfo(file.Path), folderMeta);
                                    //Add to the Syncer thing
                                    //Syncer.Instance.FilesUp.Add(file);
                                }
                            }

                        }
                    }
                    else
                    {
                        //File doesnt exist on Dropbox
                        //check if it was deleted
                        if (file.LastMod > syncFolder.LastSync) //Both Local
                        {
                            //file added after last sync (upload)
                            UpdateSyncStatuses(SyncStatus.Uploading);
                            Syncer.Instance.DropBox.UploadFile(new FileInfo(file.Path), folderMeta);
                            //Add to the Syncer thing
                            //Syncer.Instance.FilesUp.Add(file);
                        }
                        else
                        {
                            //Check for moved file...
                            if (file.LastAccess > syncFolder.LastSync)
                            {
                                //TODO - TESTING!

                                //File was moved? upload it to dropbox...
                                UpdateSyncStatuses(SyncStatus.Uploading);
                                Syncer.Instance.DropBox.UploadFile(new FileInfo(file.Path), folderMeta);
                            }
                            else
                            {
                                //Delete Local File
                                try
                                {
                                    File.Delete(file.Path);
                                }
                                catch
                                {
                                    //Failed to delete.
                                }
                            }
                        }
                    }
                }
                catch
                {
                    //Pokemon Exception handling...
                }
            }

            //reload files list for deletes
            files = syncFolder.GetFiles();

            //Now check for files the are on dropbox but not phone
            var syncFiles = new List<SyncFile>();
            foreach (SyncFile file in files)
            {
                syncFiles.Add(file);
            }

            var newFiles = from df in folderMeta.Contents
                join lf in syncFiles on df.Name equals lf.Name into nf
                from f in nf.DefaultIfEmpty()
                where f == null && (!df.Is_Dir)
                select df;

            foreach (MetaData dropMeta in newFiles)
            {
                UpdateSyncStatuses(dropMeta.Name, SyncStatus.Checking);
                //Check for extension and size exclusions
                if (Settings.Instance.MaxSizeMB > 0)
                {
                    if (Settings.Instance.MaxSizeMB < (dropMeta.Bytes / 1024.00 / 1024.00))
                    {
                        //File too big...
                        //Add to the Syncer thing
                        //Syncer.Instance.FilesSkippedD.Add(dropFile);
                        continue;
                    }
                }
                //check if dropbox file was added after last sync
                if (dropMeta.Modified > TimeZone.CurrentTimeZone.ToUniversalTime(syncFolder.LastSync))
                {
                    //new file (download)
                    UpdateSyncStatuses(SyncStatus.Downloading);
                    var downloadFile = Syncer.Instance.DropBox.GetFile(dropMeta);

                    if (downloadFile != null)
                    {
                        downloadFile.SaveFile(Path.Combine(syncFolder.Path, downloadFile.Name));
                        //Add to the Syncer thing
                        //Syncer.Instance.FilesNew.Add(dropFile);
                    }
                }
                else
                {
                    //file deleted (delete)
                    Syncer.Instance.DropBox.Delete(dropMeta);
                }
            }

            //now check for folders on dropbox but not the phone
            var syncFolders = new List<SyncFolder>();
            foreach (SyncFolder folder in folders)
            {
                syncFolders.Add(folder);
            }

            var newFolders = from df in folderMeta.Contents
                             join lf in syncFolders on df.Name equals lf.Name into nf
                             from f in nf.DefaultIfEmpty()
                             where f == null && df.Is_Dir
                             select df;

            foreach (MetaData dropMeta in newFolders)
            {
                //ITS A FOLDER! (exists on dropbox but not phone)

                //If folder was added to dropbox after last sync, download it
                if (dropMeta.Modified > TimeZone.CurrentTimeZone.ToUniversalTime(syncFolder.LastSync))
                {
                    //create local folder and download files in said folder
                    var newFolder = new DirectoryInfo(Path.Combine(syncFolder.Path, dropMeta.Name));
                    if (!newFolder.Exists) newFolder.Create();

                    var subFolder = new SyncFolder(newFolder);
                    subFolder.Parent = syncFolder;
                    Sync(subFolder, string.Format("{0}/{1}", dropPath, syncFolder.Name));
                }
                else
                {
                    //folder deleted (delete)
                    Syncer.Instance.DropBox.Delete(dropMeta);
                }
                continue;
            }
        }
Example #3
0
        public bool UploadFile(FileInfo localFile, MetaData remoteDir)
        {
            var path = remoteDir.Path;
            if (!path.StartsWith("/")) path = "/" + path;

            byte[] buff = null;
            FileStream fs = new FileStream(localFile.FullName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = localFile.Length;
            buff = br.ReadBytes((int)numBytes);

            Http http = new Http("post");
            http.Url = new Uri("https://api-content.dropbox.com/0/files/dropbox" + path);

            http.Parameters.Add(new HttpParameter { Name = "file", Value = localFile.Name });

            http.Files.Add(new HttpFile { Parameter = "file", FileName = localFile.Name, ContentType = localFile.Extension, Data = buff });

            OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret);

            oAuth.Authenticate(http);

            try
            {
                http.Post();

                BytesRecieved += http.Response.Content.Length;
                BytesSent += localFile.Length;

                return http.Response.StatusCode == HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
Example #4
0
        public DropboxFile GetFile(MetaData remoteFile)
        {
            Http http = new Http("get");
            http.Url = new Uri("https://api-content.dropbox.com/0/files/dropbox" + remoteFile.Path);

            OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret);

            oAuth.Authenticate(http);

            try
            {
                var localFileInfo = new FileInfo(Settings.Instance.TempDirectory + remoteFile.Name);

                if (!localFileInfo.Directory.Exists) localFileInfo.Directory.Create();

                http.GetAndSaveFile(localFileInfo.FullName);

                BytesRecieved += localFileInfo.Length;
                
                //Check for OK status ;)
                if (http.Response.StatusCode != System.Net.HttpStatusCode.OK) return null;

                return new DropboxFile { Name = remoteFile.Name, Path = remoteFile.Path, LocalFileInfo = localFileInfo };
            }
            catch (Exception ex)
            {
                return null;
            }
        }
Example #5
0
        public bool MoveFile(MetaData fromFile, MetaData toDir)
        {
            Http http = new Http("get");
            http.Url = new Uri("https://api.dropbox.com/0/fileops/move");

            OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret);

            http.Parameters.Add(new HttpParameter { Name = "root", Value = "dropbox" });
            http.Parameters.Add(new HttpParameter { Name = "from_path", Value = fromFile.Path });
            http.Parameters.Add(new HttpParameter { Name = "to_path", Value = string.Format("{0}/{1}", toDir.Path, fromFile.Name) });

            oAuth.Authenticate(http);

            try
            {
                http.Get();

                BytesRecieved += http.Response.Content.Length;

                return http.Response.StatusCode == HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
Example #6
0
        public bool Delete(MetaData remoteFile)
        {
            Http http = new Http("get");
            http.Url = new Uri("https://api.dropbox.com/0/fileops/delete");

            OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret);

            http.Parameters.Add(new HttpParameter { Name = "path", Value = remoteFile.Path });
            http.Parameters.Add(new HttpParameter { Name = "root", Value = "dropbox" });

            oAuth.Authenticate(http);

            try
            {
                http.Get();

                BytesRecieved += http.Response.Content.Length;

                return http.Response.StatusCode == HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
Example #7
0
        public MetaData GetItems(MetaData remoteDir)
        {
            Http http = new Http("get");
            http.Url = new Uri("https://api.dropbox.com/0/metadata/dropbox" + remoteDir.Path);

            OAuthAuthenticator oAuth = new OAuthAuthenticator(http.Url.ToString(), _apiKey, _appsecret, UserLogin.Token, UserLogin.Secret);

            oAuth.Authenticate(http);

            try
            {
                http.Get();

                BytesRecieved += http.Response.Content.Length;

                JsonDeserializer deserializer = new JsonDeserializer();

                var returnMeta = deserializer.Deserialize<MetaData>(http.Response.Content);

                if (string.IsNullOrEmpty(returnMeta.error))
                {
                    if (returnMeta.Contents == null) returnMeta.Contents = new List<MetaData>();
                    return returnMeta;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }