Ejemplo n.º 1
0
        // METADATA


        private void GetMetadata <T>(string dropboxPath, Action <DropboxRequestResult <T> > onResult) where T : DBXItem
        {
            var prms = new DropboxGetMetadataRequestParams(dropboxPath);

            Log("GetMetadata for " + dropboxPath);
            MakeDropboxRequest(METADATA_ENDPOINT, prms,
                               onResponse: (jsonStr) => {
                Log("GetMetadata onResponse");
                var dict = JSON.FromJson <Dictionary <string, object> >(jsonStr);

                if (typeof(T) == typeof(DBXFolder))
                {
                    var folderMetadata = DBXFolder.FromDropboxDictionary(dict);
                    onResult(new DropboxRequestResult <T>(folderMetadata as T));
                }
                else if (typeof(T) == typeof(DBXFile))
                {
                    var fileMetadata = DBXFile.FromDropboxDictionary(dict);
                    onResult(new DropboxRequestResult <T>(fileMetadata as T));
                }
            },
                               onProgress: null,
                               onWebError: (error) => {
                Log("GetMetadata:onWebError");
                onResult(DropboxRequestResult <T> .Error(error));
            });
        }
        /// <summary>
        /// Creates folder using path specified
        /// </summary>
        /// <param name="dropboxFolderPath">Path of folder to create</param>
        /// <param name="onResult">Result callback that contains metadata of the created folder</param>
        public void CreateFolder(string dropboxFolderPath, Action <DropboxRequestResult <DBXFolder> > onResult)
        {
            var path = DropboxSyncUtils.NormalizePath(dropboxFolderPath);

            var prms = new DropboxCreateFolderRequestParams();

            prms.path = path;

            MakeDropboxRequest(CREATE_FOLDER_ENDPOINT, prms, (jsonStr) => {
                DBXFolder folderMetadata = null;

                try {
                    var root       = JSON.FromJson <Dictionary <string, object> >(jsonStr);
                    folderMetadata = DBXFolder.FromDropboxDictionary(root["metadata"] as Dictionary <string, object>);
                }catch (Exception ex) {
                    _mainThreadQueueRunner.QueueOnMainThread(() => {
                        onResult(DropboxRequestResult <DBXFolder> .Error(new DBXError(ex.Message, DBXErrorType.ParsingError)));
                    });
                    return;
                }

                _mainThreadQueueRunner.QueueOnMainThread(() => {
                    onResult(new DropboxRequestResult <DBXFolder>(folderMetadata));
                });
            }, onProgress: (progress) => {}, onWebError: (error) => {
                if (error.ErrorDescription.Contains("path/conflict/folder"))
                {
                    error.ErrorType = DBXErrorType.RemotePathAlreadyExists;
                }
                _mainThreadQueueRunner.QueueOnMainThread(() => {
                    onResult(DropboxRequestResult <DBXFolder> .Error(error));
                });
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Moves file or folder from dropboxFromPath to dropboxToPath
        /// </summary>
        /// <param name="dropboxFromPath">From path</param>
        /// <param name="dropboxToPath">To path</param>
        /// <param name="onResult">Result callback containing metadata of moved object</param>
        public void Move(string dropboxFromPath, string dropboxToPath,
                         Action <DropboxRequestResult <DBXItem> > onResult)
        {
            var prms = new DropboxMoveFileRequestParams();

            prms.from_path = dropboxFromPath;
            prms.to_path   = dropboxToPath;

            MakeDropboxRequest(MOVE_ENDPOINT, prms, (jsonStr) => {
                DBXItem metadata = null;

                try {
                    var root          = JSON.FromJson <Dictionary <string, object> >(jsonStr);
                    var metadata_dict = root["metadata"] as Dictionary <string, object>;

                    if (metadata_dict[".tag"].ToString() == "file")
                    {
                        metadata = DBXFile.FromDropboxDictionary(metadata_dict);
                    }
                    else if (metadata_dict[".tag"].ToString() == "folder")
                    {
                        metadata = DBXFolder.FromDropboxDictionary(metadata_dict);
                    }
                }catch (Exception ex) {
                    _mainThreadQueueRunner.QueueOnMainThread(() => {
                        onResult(DropboxRequestResult <DBXItem> .Error(new DBXError(ex.Message, DBXErrorType.ParsingError)));
                    });
                    return;
                }

                _mainThreadQueueRunner.QueueOnMainThread(() => {
                    onResult(new DropboxRequestResult <DBXItem>(metadata));
                });
            }, onProgress: (progress) => {}, onWebError: (error) => {
                _mainThreadQueueRunner.QueueOnMainThread(() => {
                    if (error.ErrorType == DBXErrorType.RemotePathAlreadyExists)
                    {
                        error.ErrorDescription = "Can't move file: " + dropboxToPath + " already exists";
                    }
                    onResult(DropboxRequestResult <DBXItem> .Error(error));
                });
            });
        }
        /// <summary>
        /// Retrieves structure of dropbox folders and files inside specified folder.
        /// </summary>
        /// <param name="dropboxFolderPath">Dropbox folder path</param>
        /// <param name="onResult">Callback function that receives result containing DBXFolder with all child nodes inside.</param>
        /// <param name="onProgress">Callback fnction that receives float from 0 to 1 intdicating the progress.</param>
        public void GetFolderStructure(string dropboxFolderPath, Action <DropboxRequestResult <DBXFolder> > onResult,
                                       Action <float> onProgress = null)
        {
            var path = DropboxSyncUtils.NormalizePath(dropboxFolderPath);

            _GetFolderItemsFlat(path, onResult: (items) => {
                DBXFolder rootFolder = null;

                // get root folder
                if (path == "/")
                {
                    rootFolder = new DBXFolder {
                        id = "", path = "/", name = "", items = new List <DBXItem>()
                    };
                }
                else
                {
                    rootFolder = items.Where(x => x.path == path).First() as DBXFolder;
                }
                // squash flat results
                rootFolder = BuildStructureFromFlat(rootFolder, items);

                _mainThreadQueueRunner.QueueOnMainThread(() => {
                    onResult(new DropboxRequestResult <DBXFolder>(rootFolder));
                });
            },
                                onProgress: (progress) => {
                if (onProgress != null)
                {
                    _mainThreadQueueRunner.QueueOnMainThread(() => {
                        onProgress(progress);
                    });
                }
            },
                                onError: (errorStr) => {
                _mainThreadQueueRunner.QueueOnMainThread(() => {
                    onResult(DropboxRequestResult <DBXFolder> .Error(errorStr));
                });
            }, recursive: true);
        }
        DBXFolder BuildStructureFromFlat(DBXFolder rootFolder, List <DBXItem> pool)
        {
            foreach (var poolItem in pool)
            {
                // if item is immediate child of rootFolder
                if (DropboxSyncUtils.IsPathImmediateChildOfFolder(rootFolder.path, poolItem.path))
                {
                    // add poolItem to folder children
                    if (poolItem.type == DBXItemType.Folder)
                    {
                        //Debug.Log("Build structure recursive");
                        rootFolder.items.Add(BuildStructureFromFlat(poolItem as DBXFolder, pool));
                    }
                    else
                    {
                        rootFolder.items.Add(poolItem);
                    }
                    //Debug.Log("Added child "+poolItem.path);
                }
            }

            return(rootFolder);
        }
Ejemplo n.º 6
0
        // FILE OPERATIONS

        /// <summary>
        /// Deletes file or folder on Dropbox
        /// </summary>
        /// <param name="dropboxPath">Path to file or folder on Dropbox or inside of Dropbox App folder (depending on accessToken type). Should start with "/". Example:/DropboxSyncExampleFolder/image.jpg</param>
        /// <param name="onResult">Callback function that receives DropboxRequestResult with DBXItem metadata of deleted file or folder</param>
        public void Delete(string dropboxPath, Action <DropboxRequestResult <DBXItem> > onResult)
        {
            var prms = new DropboxDeletePathRequestParams();

            prms.path = dropboxPath;

            MakeDropboxRequest(DELETE_ENDPOINT, prms, (jsonStr) => {
                DBXItem metadata = null;

                try {
                    var root          = JSON.FromJson <Dictionary <string, object> >(jsonStr);
                    var metadata_dict = root["metadata"] as Dictionary <string, object>;

                    if (metadata_dict[".tag"].ToString() == "file")
                    {
                        metadata = DBXFile.FromDropboxDictionary(metadata_dict);
                    }
                    else if (metadata_dict[".tag"].ToString() == "folder")
                    {
                        metadata = DBXFolder.FromDropboxDictionary(metadata_dict);
                    }
                }catch (Exception ex) {
                    _mainThreadQueueRunner.QueueOnMainThread(() => {
                        onResult(DropboxRequestResult <DBXItem> .Error(new DBXError(ex.Message, DBXErrorType.ParsingError)));
                    });
                    return;
                }

                _mainThreadQueueRunner.QueueOnMainThread(() => {
                    onResult(new DropboxRequestResult <DBXItem>(metadata));
                });
            }, onProgress: (progress) => {}, onWebError: (error) => {
                _mainThreadQueueRunner.QueueOnMainThread(() => {
                    onResult(DropboxRequestResult <DBXItem> .Error(error));
                });
            });
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Subscribes to file changes on Dropbox in specified folder (and recursively to all subfolders and their files).
        /// Callback fires once, when change is being registered and changed file checksum is cached in local metadata.
        /// If change was made not during app runtime, callback fires as soon as app is running and checking for updates.
        /// Update interval can be changed using `DBXChangeForChangesIntervalSeconds` (default values if 5 seconds).
        /// </summary>
        /// <param name="dropboxFolderPath">
        /// Path to folder on Dropbox or inside Dropbox App (depending on accessToken type).
        /// Should start with "/". Example: /DropboxSyncExampleFolder
        /// </param>
        /// <param name="onChange">
        /// Callback function that receives list consisting of file changes.
        /// Each file change contains `changeType` and `DBXFile` (updated file metadata).
        /// </param>
        public void SubscribeToFolderChanges(string dropboxFolderPath, Action <List <DBXFileChange> > onChange)
        {
            var item = new DBXFolder(dropboxFolderPath);

            SubscribeToChanges(item, onChange);
        }
        void _GetFolderItemsFlat(string folderPath, Action <List <DBXItem> > onResult, Action <float> onProgress,
                                 Action <DBXError> onError, bool recursive = false, string requestCursor = null, List <DBXItem> currentResults = null)
        {
            folderPath = DropboxSyncUtils.NormalizePath(folderPath);

            if (folderPath == "/")
            {
                folderPath = "";                 // dropbox error fix
            }

            string url;
            DropboxRequestParams prms;

            if (requestCursor == null)
            {
                // first request
                currentResults = new List <DBXItem>();
                url            = LIST_FOLDER_ENDPOINT;
                prms           = new DropboxListFolderRequestParams {
                    path = folderPath, recursive = recursive
                };
            }
            else
            {
                // have cursor to continue list
                url  = LIST_FOLDER_CONTINUE_ENDPOINT;
                prms = new DropboxContinueWithCursorRequestParams(requestCursor);
            }

            MakeDropboxRequest(url, prms, onResponse: (jsonStr) => {
                //Log("Got reponse: "+jsonStr);

                Dictionary <string, object> root = null;
                try {
                    root = JSON.FromJson <Dictionary <string, object> >(jsonStr);
                }catch (Exception ex) {
                    onError(new DBXError(ex.Message, DBXErrorType.ParsingError));
                    return;
                }

                var entries = root["entries"] as List <object>;
                foreach (Dictionary <string, object> entry in entries)
                {
                    if (entry[".tag"].ToString() == "file")
                    {
                        currentResults.Add(DBXFile.FromDropboxDictionary(entry));
                    }
                    else if (entry[".tag"].ToString() == "folder")
                    {
                        currentResults.Add(DBXFolder.FromDropboxDictionary(entry));
                    }
                    else
                    {
                        onError(new DBXError("Unknown entry tag " + entry[".tag".ToString()], DBXErrorType.Unknown));
                        return;
                    }
                }

                if ((bool)root["has_more"])
                {
                    // recursion
                    _GetFolderItemsFlat(folderPath, onResult, onProgress, onError, recursive: recursive,
                                        requestCursor: root["cursor"].ToString(),
                                        currentResults: currentResults);
                }
                else
                {
                    // done
                    onResult(currentResults);
                }
            }, onProgress: onProgress,
                               onWebError: (webErrorStr) => {
                //LogError("Got web err: "+webErrorStr);
                onError(webErrorStr);
            });
        }