Exemplo n.º 1
0
        public async Task Restore(string filename)
        {
            if (await SignInSkydrive())
            {
                await LoadData();

                try
                {
                    StorageFile restoreFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

                    LiveConnectClient   client        = new LiveConnectClient(_session);
                    LiveOperationResult liveOpResult2 = await client.GetAsync(_skyDriveFolderId + "/files");

                    dynamic result       = liveOpResult2.Result;
                    string  backupFileID = null;
                    foreach (var item in result.data)
                    {
                        if (item.name == filename)
                        {
                            backupFileID = item.id;
                            break;
                        }
                    }
                    if (backupFileID != null)
                    {
                        LiveDownloadOperationResult operationResult = await client.BackgroundDownloadAsync(backupFileID + "/content", restoreFile);
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception("Error during restore: " + exception.Message);
                }
            }
        }
Exemplo n.º 2
0
        public async Task <LiveDownloadOperationResult> StartBackgroundDownloadAsync(String skyDriveItemId, StorageFile file, CancellationToken cancellationToken, IProgress <LiveOperationProgress> progressHandler)
        {
            // requires wl.skydrive scope
            var client = new LiveConnectClient(_session);
            var path   = String.Format("{0}/content", skyDriveItemId);
            var result = await client.BackgroundDownloadAsync(path, file, cancellationToken, progressHandler);

            return(result);
        }
        public void TestDownloadWhiteSpaceStringPath()
        {
            var connectClient = new LiveConnectClient(new LiveConnectSession());

            try
            {
                connectClient.BackgroundDownloadAsync("\t\n ");
                Assert.Fail("Expected ArguementException to be thrown.");
            }
            catch (ArgumentException)
            {
            }
        }
        public void TestDownloadNullPath()
        {
            var connectClient = new LiveConnectClient(new LiveConnectSession());

            try
            {
                connectClient.BackgroundDownloadAsync(null);
                Assert.Fail("Expected ArguementNullException to be thrown.");
            }
            catch (ArgumentNullException)
            {
            }
        }
Exemplo n.º 5
0
        public async Task <int> DownloadFileFromOneDrive()
        {
            try
            {
                string fileID = string.Empty;

                //  get folder ID
                string folderID = await GetFolderID("folderone");

                if (string.IsNullOrEmpty(folderID))
                {
                    return(0); // doesnt exists
                }

                //  get list of files in this folder
                LiveOperationResult loResults = await liveClient.GetAsync(folderID + "/files");

                List <object> folder = loResults.Result["data"] as List <object>;

                //  search for our file
                foreach (object fileDetails in folder)
                {
                    IDictionary <string, object> file = fileDetails as IDictionary <string, object>;
                    if (string.Compare(file["name"].ToString(), "filename", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        //  found our file
                        fileID = file["id"].ToString();
                        break;
                    }
                }

                if (string.IsNullOrEmpty(fileID))
                {
                    //  file doesnt exists
                    return(0);
                }

                //  create local file
                StorageFile localFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename_from_onedrive", CreationCollisionOption.ReplaceExisting);

                //  download file from OneDrive
                await liveClient.BackgroundDownloadAsync(fileID + "/content", localFile);

                return(1);
            }
            catch
            {
            }
            return(0);
        }
Exemplo n.º 6
0
        public async void TestBackgroundDownloadNullPath()
        {
            var connectClient = new LiveConnectClient(new LiveConnectSession());

            try
            {
                var downloadLocation = new Uri("/shared/transfers/downloadLocation.txt", UriKind.RelativeOrAbsolute);
                await connectClient.BackgroundDownloadAsync(null, downloadLocation);

                Assert.Fail("Expected ArguementException to be thrown.");
            }
            catch (ArgumentException)
            {
            }
        }
Exemplo n.º 7
0
        private void BeginDownloadFile(OneDriveFile file)
        {
            // Sync Step 3: We're scheduling some files to be downloaded.

            // Adds the file id to the list of currently downloading files.
            lock (_syncRoot)
            {
                _dlFiles.Add(file);
            }

            // Starts downloading the cartridge to the isostore.
            bool shouldDirectDownload = !IsBackgroundDownloadAllowed;

            Log(file.ToString() + ", directDownload = " + shouldDirectDownload);
            string fileAttribute = file.Id + "/content";

            if (shouldDirectDownload)
            {
                // Direct download.
                Log("Starts DownloadAsync");
                StartTaskAndContinueWith(
                    () => _liveClient.DownloadAsync(fileAttribute),
                    t => OnLiveClientDownloadCompleted(t, file));
            }
            else
            {
                try
                {
                    // Tries to perform a background download.
                    Log("Starts BackgroundDownloadAsync");
                    StartTaskAndContinueWith(
                        () => _liveClient.BackgroundDownloadAsync(
                            fileAttribute,
                            new Uri(GetTempIsoStorePath(file.Name), UriKind.RelativeOrAbsolute)
                            ),
                        t => OnLiveClientBackgroundDownloadCompleted(t, file));
                }
                catch (Exception)
                {
                    // Tries the direct download method.
                    Log("Starts DownloadAsync (fallback)");
                    StartTaskAndContinueWith(
                        () => _liveClient.DownloadAsync(fileAttribute),
                        t => OnLiveClientDownloadCompleted(t, file));
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Download a file from onedrive
        /// </summary>
        /// <returns></returns>
        public static async Task <int> DownloadAsync(IDictionary <string, object> File)
        {
            try
            {
                //  create local file
                StorageFile localFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(File["name"].ToString(), CreationCollisionOption.ReplaceExisting);

                //  download file from OneDrive
                await liveClient.BackgroundDownloadAsync(File["id"].ToString() + "/content", localFile);

                return(1);
            }
            catch (Exception e)
            {
                return(e.Message.Length);
            }
            return(0);
        }
Exemplo n.º 9
0
        public static async Task<string> SetupChmFileFromOneDrive(LiveConnectClient client, 
            IProgress<LiveOperationProgress> progressHandler,
            System.Threading.CancellationToken ctoken,
            string id, string name, string path)
        {
            ChmFile ret = new ChmFile();
            ret.Key = Guid.NewGuid().ToString("N");
            ret.HasThumbnail = false;
            Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

            StorageFile file = await localFolder.CreateFileAsync(ret.Key + ChmFileExtension, CreationCollisionOption.ReplaceExisting);
            LiveDownloadOperationResult result = await client.BackgroundDownloadAsync(id + "/content", file, ctoken, progressHandler);
            
            try
            {
                ret.Chm = await LoadChm(file.Path, false);
                MetaInfo meta = new MetaInfo();
                meta.SetOriginalPath(path);
                if (ret.Chm.Title != null)
                {
                    meta.SetDisplayName(ret.Chm.Title);
                }
                else
                {
                    meta.SetDisplayName(System.IO.Path.GetFileNameWithoutExtension(name));
                }
                ret.ChmMeta = meta;
                await ret.Save();
                FileHistory.AddToHistory(ret.Key);
            }
            catch
            {
                ret.Chm = null;
            }
            if (ret.Chm == null)
            {
                await MetaInfo.DeleteMetaFile(ret.Key);
                await DeleteFile(ret.Key);
                return null;
            }
            return ret.Key;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Downloads all files from BBLyrics folder in OneDrive cloud to Music folder
        /// </summary>
        /// <returns></returns>
        public static async Task DownloadFilesAsync(Progress <LiveOperationProgress> progress)
        {
            await CheckIfExists();

            StorageFolder       folder = Windows.Storage.KnownFolders.MusicLibrary;
            StorageFile         newFile;
            List <string>       fileNames       = new List <string>();
            string              query           = _folderId + "/files";
            LiveOperationResult operationResult = await _client.GetAsync(query);

            OneDriveManager.CancelToken = new System.Threading.CancellationTokenSource();
            dynamic result = operationResult.Result;

            foreach (dynamic file in result.data)
            {
                fileNames.Add(file.name); //lame hack not to get runtimebinder exception caused by no getawaiter in object
                newFile = await folder.CreateFileAsync(fileNames[fileNames.Count - 1], CreationCollisionOption.ReplaceExisting);

                await _client.BackgroundDownloadAsync(file.id + "/Content", newFile, OneDriveManager.CancelToken.Token, progress);
            }
            OneDriveManager.CancelToken = null;
        }
        /// <summary>
        ///     Restore a database backup from OneDrive
        /// </summary>
        /// <returns>TaskCompletionType wether the task was successful or not.</returns>
        public async Task <TaskCompletionType> Restore()
        {
            if (_liveClient == null)
            {
                await Login();
            }

            try {
                await GetBackupId();

                var localFolder = ApplicationData.Current.LocalFolder;
                var storageFile =
                    await localFolder.CreateFileAsync(DB_NAME, CreationCollisionOption.ReplaceExisting);

                await _liveClient.BackgroundDownloadAsync(_backupId + "/content", storageFile);

                return(TaskCompletionType.Successful);
            }
            catch (Exception ex) {
                InsightHelper.Report(ex);
                return(TaskCompletionType.Unsuccessful);
            }
        }
 public async void TestBackgroundDownloadNullPath()
 {
     var connectClient = new LiveConnectClient(new LiveConnectSession());
     try
     {
         var downloadLocation = new Uri("/shared/transfers/downloadLocation.txt", UriKind.RelativeOrAbsolute);
         await connectClient.BackgroundDownloadAsync(null, downloadLocation);
         Assert.Fail("Expected ArguementException to be thrown.");
     }
     catch (ArgumentException)
     {
     }
 }
 public void TestDownloadWhiteSpaceStringPath()
 {
     var connectClient = new LiveConnectClient(new LiveConnectSession());
     try
     {
         connectClient.BackgroundDownloadAsync("\t\n ");
         Assert.Fail("Expected ArguementException to be thrown.");
     }
     catch (ArgumentException)
     {
     }
 }
 public void TestDownloadNullPath()
 {
     var connectClient = new LiveConnectClient(new LiveConnectSession());
     try
     {
         connectClient.BackgroundDownloadAsync(null);
         Assert.Fail("Expected ArguementNullException to be thrown.");
     }
     catch (ArgumentNullException)
     {
     }
 }