Beispiel #1
0
        public async static Task <List <OneDriveStorageFile> > getCloudDataFiles(OneDriveStorageFolder rootFolder)
        {
            var cloudGoalData = await rootFolder.GetFileAsync("golaso.json");

            var cloudCompletedGoalData = await rootFolder.GetFileAsync("noGolaso.json");

            var cloudHistoryData = await rootFolder.GetFileAsync("history.json");

            return(new List <OneDriveStorageFile> {
                cloudGoalData, cloudCompletedGoalData, cloudHistoryData
            });
        }
Beispiel #2
0
        /// <summary>
        /// 确认OneDrive中是否有备份数据存在
        /// </summary>
        /// <returns></returns>
        public async Task <bool> EnsureCloudMetaExist()
        {
            if (_appFolder == null)
            {
                throw new UnauthorizedAccessException("You need to complete OneDrive authorization before you can get this file");
            }
            try
            {
                var file = await _appFolder.GetFileAsync("ImgMeta.db");

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public static async Task <OneDriveStorageFile> GetOneDriveFilesAsync(string fileName)
        {
            if (rootFolder is null)
            {
                if (!await LogintoOnedriveAsync())
                {
                    throw new InvalidOperationException("Failed to log in");
                }
                if (rootFolder is null)
                {
                    var root = await OneDriveService.Instance.AppRootFolderAsync();

                    Interlocked.CompareExchange(ref rootFolder, root, null);
                }
            }
            fileName = fileName.Split(@"\OneDrive\", 2).Last();
            var folderName = SysPath.GetDirectoryName(fileName);
            var sFileName  = SysPath.GetFileName(fileName);

            if (folderName.IsNullorEmpty())
            {
                return(await rootFolder.GetFileAsync(fileName));
            }

            var file = default(OneDriveStorageFile);

            // copy to local to avoid changes by other threads
            var currentCache = cache;

            if (currentCache != null && folderName == currentCache.FolderName)
            {
                file = currentCache.Files.Find(f => f.Name == sFileName);
            }
            if (file != null)
            {
                return(file);
            }

            // cache miss
            var folder = await rootFolder.GetFolderAsync(folderName);

            var files = await folder.GetFilesAsync(10000);

            file  = files.Find(f => f.Name == sFileName);
            cache = new CacheData
            {
                Files      = files,
                Folder     = folder,
                FolderName = folderName,
            };
            if (file != null)
            {
                return(file);
            }

            // slow route: first 10000 files missed
            return(await folder.GetFileAsync(sFileName));
        }
Beispiel #4
0
        public async static Task <bool> checkForStatusFile(OneDriveStorageFolder rootFolder)
        {
            bool statusFileExists = false;

            try
            {
                await rootFolder.GetFileAsync("status.txt");

                statusFileExists = true;
            }

            catch
            {
            }
            return(statusFileExists);
        }
Beispiel #5
0
        public async static Task <string> getStatusFileContents(OneDriveStorageFolder rootFolder)
        {
            var fileToReadTextFrom = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("statusContent.txt", CreationCollisionOption.ReplaceExisting);

            var statusFile = await rootFolder.GetFileAsync("status.txt");

            using (var fileStream = await statusFile.OpenAsync())
            {
                byte[] buffer      = new byte[fileStream.Size];
                var    localBuffer = await fileStream.ReadAsync(buffer.AsBuffer(), (uint)fileStream.Size, InputStreamOptions.ReadAhead);

                using (var localStream = await fileToReadTextFrom.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await localStream.WriteAsync(localBuffer);

                    await localStream.FlushAsync();
                }
            }

            string statusFileContent = System.IO.File.ReadAllText(fileToReadTextFrom.Path);
            await fileToReadTextFrom.DeleteAsync();

            return(statusFileContent);
        }
Beispiel #6
0
        /// <summary>
        /// 获取OneDrive中存储的Rss列表数据
        /// </summary>
        /// <returns></returns>
        public async Task <List <Category> > GetCategoryList()
        {
            if (_appFolder == null)
            {
                throw new UnauthorizedAccessException("You need to complete OneDrive authorization before you can get this file");
            }
            try
            {
                var file = await _appFolder.GetFileAsync("RssList.json");

                using (var stream = (await file.StorageFilePlatformService.OpenAsync()) as IRandomAccessStream)
                {
                    Stream st = WindowsRuntimeStreamExtensions.AsStreamForRead(stream);
                    st.Position = 0;
                    StreamReader sr     = new StreamReader(st, Encoding.UTF8);
                    string       result = sr.ReadToEnd();
                    result = result.Replace("\0", "");
                    if (string.IsNullOrEmpty(result))
                    {
                        result = "[]";
                    }
                    var list = JsonConvert.DeserializeObject <List <Category> >(result);
                    return(list);
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToLower().Contains("itemnotfound"))
                {
                    await _appFolder.StorageFolderPlatformService.CreateFileAsync("RssList.json", CreationCollisionOption.ReplaceExisting);

                    return(new List <Category>());
                }
                throw;
            }
        }
        public async Task <string> downloadFileAsync(string filename, IStorageFile file)
        {
            try
            {
                string tmp = await connectAsync();

                if (tmp == "OK")
                {
                    var sourceItem = await skyDriveFolder.GetFileAsync(filename);

                    if (sourceItem != null)
                    {
                        using (var remoteStream = (await sourceItem.StorageFilePlatformService.OpenAsync()) as IRandomAccessStream)
                        {
                            byte[] buffer      = new byte[remoteStream.Size];
                            var    localBuffer = await remoteStream.ReadAsync(buffer.AsBuffer(), (uint)remoteStream.Size, InputStreamOptions.ReadAhead);

                            using (var localStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                            {
                                //var res = await RandomAccessStream.CopyAsync(remoteStream, localStream);
                                await localStream.WriteAsync(localBuffer);

                                if (await localStream.FlushAsync())
                                {
                                }
                                //if (res == 1)
                                {
                                }
                            }
                        }

                        if (downloadFinished != null)
                        {
                            downloadFinished(this, file);
                        }

                        return("OK");
                    }
                    else
                    {
                        LiveStatusMessage notif = notifMessage;
                        if (notif != null)
                        {
                            notif(this, FOLDER_NOT_FOUND, null);
                        }
                    }
                }
            }
            catch (TaskCanceledException)
            {
                LiveStatusMessage notif = notifMessage;
                if (notif != null)
                {
                    notif(this, TASK_CANCELLED, null);
                }
            }
            catch (Exception ex)
            {
                LiveStatusMessage notif = notifMessage;
                if (notif != null)
                {
                    notif(this, ERROR, ex);
                }
            }
            //finally
            //{
            //    resetTransfers();
            //}
            return("KO");
        }