예제 #1
0
파일: NodeViewModel.cs 프로젝트: rrsc/uwp
        private void CheckAndUpdateOffline(MNode megaNode)
        {
            var offlineNodePath = OfflineService.GetOfflineNodePath(megaNode);

            if ((megaNode.getType() == MNodeType.TYPE_FILE && FileService.FileExists(offlineNodePath)) ||
                (megaNode.getType() == MNodeType.TYPE_FOLDER && FolderService.FolderExists(offlineNodePath)) ||
                TransferService.ExistPendingNodeOfflineTransfer(this))
            {
                if (SavedForOfflineDB.ExistsNodeByLocalPath(offlineNodePath))
                {
                    SavedForOfflineDB.UpdateNode(megaNode);
                }
                else
                {
                    SavedForOfflineDB.InsertNode(megaNode);
                }

                this.IsSavedForOffline = true;
                return;
            }

            if (SavedForOfflineDB.ExistsNodeByLocalPath(offlineNodePath))
            {
                SavedForOfflineDB.DeleteNodeByLocalPath(offlineNodePath);
            }

            this.IsSavedForOffline = false;
        }
예제 #2
0
        public async Task <bool> SaveForOffline()
        {
            // User must be online to perform this operation
            if (!IsUserOnline())
            {
                return(false);
            }

            MNode parentNode = SdkService.MegaSdk.getParentNode(this.OriginalMNode);

            String sfoRootPath = Path.Combine(ApplicationData.Current.LocalFolder.Path,
                                              AppResources.DownloadsDirectory.Replace("\\", ""));

            String parentNodePath;

            if (ParentContainerType != ContainerType.PublicLink)
            {
                parentNodePath = Path.Combine(ApplicationData.Current.LocalFolder.Path,
                                              AppResources.DownloadsDirectory.Replace("\\", ""),
                                              (SdkService.MegaSdk.getNodePath(parentNode)).Remove(0, 1).Replace("/", "\\"));
            }
            else
            {
                // If is a public node (link) the destination folder is the SFO root
                parentNodePath = sfoRootPath;
            }

            if (!FolderService.FolderExists(parentNodePath))
            {
                FolderService.CreateFolder(parentNodePath);
            }

            if (this.IsFolder)
            {
                await RecursiveSaveForOffline(parentNodePath, this);
            }
            else
            {
                await SaveFileForOffline(parentNodePath, this);
            }

            this.IsAvailableOffline = this.IsSelectedForOffline = true;

            // Check and add to the DB if necessary the previous folders of the path
            while (String.Compare(parentNodePath, sfoRootPath) != 0)
            {
                var folderPathToAdd = parentNodePath;
                parentNodePath = ((new DirectoryInfo(parentNodePath)).Parent).FullName;

                if (!SavedForOffline.ExistsNodeByLocalPath(folderPathToAdd))
                {
                    SavedForOffline.Insert(parentNode);
                }

                parentNode = SdkService.MegaSdk.getParentNode(parentNode);
            }

            return(true);
        }
예제 #3
0
        private async Task RecursiveSaveForOffline(String sfoPath, NodeViewModel node)
        {
            if (!FolderService.FolderExists(sfoPath))
            {
                FolderService.CreateFolder(sfoPath);
            }

            String newSfoPath = Path.Combine(sfoPath, node.Name);

            if (!FolderService.FolderExists(newSfoPath))
            {
                FolderService.CreateFolder(newSfoPath);
            }

            if (!SavedForOffline.ExistsNodeByLocalPath(newSfoPath))
            {
                SavedForOffline.Insert(node.OriginalMNode, true);
            }
            else
            {
                SavedForOffline.UpdateNode(node.OriginalMNode, true);
            }

            MNodeList childList = MegaSdk.getChildren(node.OriginalMNode);

            for (int i = 0; i < childList.size(); i++)
            {
                // To avoid pass null values to CreateNew
                if (childList.get(i) == null)
                {
                    continue;
                }

                var childNode = NodeService.CreateNew(this.MegaSdk, this.AppInformation, childList.get(i), this.ParentContainerType);

                // If node creation failed for some reason, continue with the rest and leave this one
                if (childNode == null)
                {
                    continue;
                }

                if (childNode.IsFolder)
                {
                    await RecursiveSaveForOffline(newSfoPath, childNode);
                }
                else
                {
                    await SaveFileForOffline(newSfoPath, childNode);
                }
            }
        }
예제 #4
0
        private void CheckAndUpdateSFO(MNode megaNode)
        {
            this.IsAvailableOffline   = false;
            this.IsSelectedForOffline = false;

            var nodePath = SdkService.MegaSdk.getNodePath(megaNode);

            if (String.IsNullOrWhiteSpace(nodePath))
            {
                return;
            }

            var nodeOfflineLocalPath = Path.Combine(ApplicationData.Current.LocalFolder.Path,
                                                    AppResources.DownloadsDirectory, nodePath.Remove(0, 1).Replace("/", "\\"));

            if (SavedForOffline.ExistsNodeByLocalPath(nodeOfflineLocalPath))
            {
                var existingNode = SavedForOffline.ReadNodeByLocalPath(nodeOfflineLocalPath);
                if ((megaNode.getType() == MNodeType.TYPE_FILE && FileService.FileExists(nodeOfflineLocalPath)) ||
                    (megaNode.getType() == MNodeType.TYPE_FOLDER && FolderService.FolderExists(nodeOfflineLocalPath)))
                {
                    this.IsAvailableOffline   = true;
                    this.IsSelectedForOffline = existingNode.IsSelectedForOffline;
                }
                else
                {
                    SavedForOffline.DeleteNodeByLocalPath(nodeOfflineLocalPath);
                }
            }
            else
            {
                if (megaNode.getType() == MNodeType.TYPE_FILE && FileService.FileExists(nodeOfflineLocalPath))
                {
                    SavedForOffline.Insert(megaNode, true);
                    this.IsAvailableOffline = this.IsSelectedForOffline = true;
                }
                else if (megaNode.getType() == MNodeType.TYPE_FOLDER && FolderService.FolderExists(nodeOfflineLocalPath))
                {
                    SavedForOffline.Insert(megaNode);
                    this.IsAvailableOffline   = true;
                    this.IsSelectedForOffline = false;
                }
            }
        }
예제 #5
0
파일: NodeViewModel.cs 프로젝트: rrsc/uwp
        public async void SaveForOffline()
        {
            // User must be online to perform this operation
            if (!await IsUserOnlineAsync())
            {
                return;
            }

            var offlineParentNodePath = OfflineService.GetOfflineParentNodePath(this.OriginalMNode);

            if (!FolderService.FolderExists(offlineParentNodePath))
            {
                FolderService.CreateFolder(offlineParentNodePath);
            }

            var existingNode = SavedForOfflineDB.SelectNodeByFingerprint(MegaSdk.getNodeFingerprint(this.OriginalMNode));

            if (existingNode != null)
            {
                bool result = this.IsFolder ?
                              await FolderService.CopyFolderAsync(existingNode.LocalPath, offlineParentNodePath) :
                              await FileService.CopyFileAsync(existingNode.LocalPath, offlineParentNodePath);

                if (result)
                {
                    SavedForOfflineDB.InsertNode(this.OriginalMNode);
                }
            }
            else
            {
                TransferService.MegaTransfers.Add(this.OfflineTransfer);
                this.OfflineTransfer.StartTransfer(true);
            }

            this.IsSavedForOffline = true;

            OfflineService.CheckOfflineNodePath(this.OriginalMNode);
        }
예제 #6
0
        private async Task RecursiveRemoveForOffline(String sfoPath, String nodeName)
        {
            String newSfoPath = Path.Combine(sfoPath, nodeName);

            if (FolderService.FolderExists(newSfoPath))
            {
                // Search if the folder has a pending transfer for offline and cancel it on this case
                TransfersService.CancelPendingNodeOfflineTransfers(String.Concat(newSfoPath, "\\"), this.IsFolder);

                IEnumerable <string> childFolders = Directory.GetDirectories(newSfoPath);
                if (childFolders != null)
                {
                    foreach (var folder in childFolders)
                    {
                        if (folder != null)
                        {
                            await RecursiveRemoveForOffline(newSfoPath, folder);

                            SavedForOffline.DeleteNodeByLocalPath(Path.Combine(newSfoPath, folder));
                        }
                    }
                }

                IEnumerable <string> childFiles = Directory.GetFiles(newSfoPath);
                if (childFiles != null)
                {
                    foreach (var file in childFiles)
                    {
                        if (file != null)
                        {
                            SavedForOffline.DeleteNodeByLocalPath(Path.Combine(newSfoPath, file));
                        }
                    }
                }
            }
        }