Example #1
0
        public static async Task PushToRoamingData(StorageFile file)
        {
            var roamingFolder = ApplicationData.Current.RoamingFolder;
            var folder        = ApplicationData.Current.LocalFolder;

            using (var releaser = await _RoamingDataSyncLock.LockAsync())
            {
                // fileの相対的なパスをLocalFolder基準で取得
                // LocalFolder外のアイテムは同期不可能
                var filePath = file.Path;
                if (filePath.StartsWith(folder.Path))
                {
                    filePath = filePath.Substring(folder.Path.Length + 1);
                }
                else
                {
                    throw new ArgumentException("file is not Local Folder item, cant sync to roaming folder.");
                }

                // ローミングから同期情報を取得、無ければ新規作成
                var syncInfoFileAccessor = new Util.FileAccessor <RoamingSyncInfo>(roamingFolder, "sync.json");
                var syncInfo             = await syncInfoFileAccessor.Load();

                if (syncInfo == null)
                {
                    syncInfo = new RoamingSyncInfo();

                    // 同期情報がなかった場合は、同期不要なファイルになるので全てお掃除
                    var items = await roamingFolder.GetItemsAsync();

                    foreach (var item in items)
                    {
                        await item.DeleteAsync();
                    }
                }

                // 同期情報にfileの情報を追加してローミングフォルダに保存
                var fileProp = await file.GetBasicPropertiesAsync();

                syncInfo.AddOrReplace(filePath, fileProp.DateModified.DateTime);
                await syncInfoFileAccessor.Save(syncInfo);


                // ローミングフォルダにLocalFolderからfileまでの相対的なフォルダ構造を再現
                var folderPathStack = filePath.Split('/', '\\').ToList();
                var parentFolder    = roamingFolder;
                foreach (var folderName in folderPathStack.Take(folderPathStack.Count - 1 /* without file name */))
                {
                    parentFolder = await parentFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);
                }

                // ローミングフォルダ側のファイルを作成
                var fileName    = Path.GetFileName(file.Path);
                var roamingFile = await parentFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);

                // ローミング側のファイルに元ファイルをコピー
                await file.CopyAndReplaceAsync(roamingFile);
            }
        }
Example #2
0
        public static async Task <IList <StorageFile> > GetSyncRoamingData(StorageFolder folder)
        {
            // 指定フォルダはアプリのローカルフォルダであるか
            if (!folder.Path.StartsWith(ApplicationData.Current.LocalFolder.Path))
            {
                return(new List <StorageFile>());
            }

            // 同期済みファイル
            var roamingFolder        = ApplicationData.Current.RoamingFolder;
            var syncInfoFileAccessor = new Util.FileAccessor <RoamingSyncInfo>(roamingFolder, "sync.json");
            var syncInfo             = await syncInfoFileAccessor.Load();

            if (syncInfo == null)
            {
                return(new List <StorageFile>());
            }

            // ローカルフォルダからの相対パスを中出
            var reletivePath = folder.Path.Substring(ApplicationData.Current.LocalFolder.Path.Length + 1);

            // 指定フォルダの削除されていない同期ファイルを抽出
            var list = new List <StorageFile>();

            foreach (var item in syncInfo.SyncInfoItems)
            {
                if (item.Mode == SyncMode.Remove)
                {
                    continue;
                }

                if (item.RelativeFilePath.StartsWith(reletivePath))
                {
                    var path = Path.Combine(ApplicationData.Current.LocalFolder.Path, item.RelativeFilePath);
                    try
                    {
                        if (File.Exists(path))
                        {
                            var file = await StorageFile.GetFileFromPathAsync(path);

                            list.Add(file);
                        }
                    }
                    catch (FileNotFoundException) { }
                }
            }

            return(list);
        }
Example #3
0
        public static async Task PullRoamingData()
        {
            var roamingFolder = ApplicationData.Current.RoamingFolder;
            var folder        = ApplicationData.Current.LocalFolder;

            using (var releaser = await _RoamingDataSyncLock.LockAsync())
            {
                var syncInfoFileAccessor = new Util.FileAccessor <RoamingSyncInfo>(roamingFolder, "sync.json");
                var syncInfo             = await syncInfoFileAccessor.Load();

                if (syncInfo == null)
                {
                    // 同期情報がなかった場合は、同期不要なファイルになるので全てお掃除
                    var items = await roamingFolder.GetItemsAsync();

                    foreach (var item in items)
                    {
                        await item.DeleteAsync();
                    }

                    return;
                }

                List <FileSyncInfo> removeItems = new List <FileSyncInfo>();
                foreach (var syncFileInfo in syncInfo.SyncInfoItems)
                {
                    if (false == await PullRoamingData(syncFileInfo))
                    {
                        removeItems.Add(syncFileInfo);
                    }
                }

                if (removeItems.Count > 0)
                {
                    foreach (var removeItem in removeItems)
                    {
                        syncInfo.SyncInfoItems.Remove(removeItem);
                    }

                    await syncInfoFileAccessor.Save(syncInfo);
                }
            }
        }
Example #4
0
        public static async Task RoamingDataRemoved(StorageFile file)
        {
            if (file == null)
            {
                return;
            }

            var romingFolder = ApplicationData.Current.RoamingFolder;
            var folder       = ApplicationData.Current.LocalFolder;

            using (var releaser = await _RoamingDataSyncLock.LockAsync())
            {
                // fileの相対的なパスをLocalFolder基準で取得
                // LocalFolder外のアイテムは同期不可能
                var filePath = file.Path;
                if (filePath.StartsWith(folder.Path))
                {
                    filePath = filePath.Substring(folder.Path.Length - 1);
                }
                else
                {
                    return;
                }

                // ローミングから同期情報を取得、無ければ新規作成
                var syncInfoFileAccessor = new Util.FileAccessor <RoamingSyncInfo>(romingFolder, "sync.json");
                var syncInfo             = await syncInfoFileAccessor.Load();

                if (syncInfo == null)
                {
                    return;
                }

                // 同期情報にfileの情報を削除して保存
                syncInfo.Remove(filePath);
                await syncInfoFileAccessor.Save(syncInfo);
            }
        }