public IMegaRequest DownloadFile(MegaNode node, string filename, Action<DownloadHandle> OnHandleReady, Action<int> OnError) { var req = new MRequestGetDownloadUrl<MResponseGetDownloadUrl>(User, node.Id); DownloadHandle handle = null; req.Success += (s, a) => { handle = MegaDownloader.GetHandle(filename, a.FileSize, a.Url, node); if (OnHandleReady != null) { Util.StartThread(() => OnHandleReady(handle), "transfer_handle_ready_handler"); } downloader.StartTransfer(handle); }; req.Error += (s, e) => { if (OnError != null) { OnError(e.Error); } }; transport.EnqueueRequest(req); return req; }
public IMegaRequest UpdateNodeAttr(MegaNode node, Action OnSuccess, Action <int> OnError) { if (node == null || node.Attributes == null) { if (OnError != null) { OnError(MegaApiError.EWRONG); } return(new EmptyRequest()); } var req = new MRequestUpdateAttributes <MResponseUpdateAttributes>(User, node); req.Success += (s, e) => { if (OnSuccess != null) { OnSuccess(); } }; req.Error += (s, e) => { if (OnError != null) { OnError(e.Error); } }; transport.EnqueueRequest(req); return(req); }
public void UpdateNodeAttrSync(MegaNode node) { int?error = null; UpdateNodeAttr(node, null, (e) => error = e).ResetEvent.WaitOne(); if (error != null) { throw new MegaApiException((int)error, "Could not update the node"); } }
public MegaNode CreateFolderSync(string targetNode, string folderName) { MegaNode folder = null; int? error = null; CreateFolder(targetNode, folderName, (n) => folder = n, (e) => error = e).ResetEvent.WaitOne(); if (folder == null || error != null) { throw new MegaApiException((int)error, "Could not create the folder"); } return(folder); }
public MegaNode CreateFolderSync(MegaNode target, List <MegaNode> nodes, string folder, char separator) { MegaNode result = null; int? errno = null; CreateFolder(target, nodes, folder, separator, (n) => result = n, (e) => errno = e).ResetEvent.WaitOne(); if (result == null || errno != null) { throw new MegaApiException((int)errno, String.Format("Could not create the folder {0}", folder)); } return(result); }
void CreateFolders( MegaNode targetNode, List <MegaNode> existingNodes, string[] folders, Action <MegaNode> OnSuccess, Action <int> OnError) { if (folders.Length == 0) { OnSuccess(targetNode); return; } MegaNode existing; lock (existingNodes) { existing = existingNodes.Where(n => n.ParentId == targetNode.Id && n.Attributes.Name == folders[0]).FirstOrDefault(); } if (existing != null) { CreateFolders( existing, existingNodes, folders.Skip(1).ToArray(), OnSuccess, OnError); } else { CreateFolder(targetNode.Id, folders[0], (n) => { lock (existingNodes) { existingNodes.Add(n); } CreateFolders( n, existingNodes, folders.Skip(1).ToArray(), OnSuccess, OnError); }, (i) => OnError(i)); } }
public void DownloadFileSync(MegaNode node, string filename) { int? error = null; TransferHandle handle = null; var waitFile = new ManualResetEvent(false); DownloadFile(node, filename, (h) => { handle = h; h.TransferEnded += (s, e) => waitFile.Set(); }, (e) => error = e) .ResetEvent.WaitOne(); waitFile.WaitOne(); if (handle == null || error != null || handle.Node == null || handle.Error != null) { throw new MegaApiException(handle.Error == null ? (int)error : (int)handle.Error, "Could not upload the file"); } }
public IMegaRequest CreateFolder(MegaNode targetNode, List <MegaNode> existingNodes, string folderPath, char separator, Action <MegaNode> OnSuccess, Action <int> OnError) { var result = new EmptyRequest(); if (string.IsNullOrEmpty(folderPath)) { OnSuccess(targetNode); return(result); } result.ResetEvent.Reset(); var folders = folderPath.Split(new char[] { separator }, StringSplitOptions.RemoveEmptyEntries); CreateFolders( targetNode, existingNodes, folders, (node) => { OnSuccess(node); result.ResetEvent.Set(); }, (i) => { OnError(i); result.ResetEvent.Set(); }); return(result); }
public IMegaRequest DownloadFile(MegaNode node, string filename, Action <DownloadHandle> OnHandleReady, Action <int> OnError) { var req = new MRequestGetDownloadUrl <MResponseGetDownloadUrl>(User, node.Id); DownloadHandle handle = null; req.Success += (s, a) => { handle = MegaDownloader.GetHandle(filename, a.FileSize, a.Url, node); if (OnHandleReady != null) { Util.StartThread(() => OnHandleReady(handle), "transfer_handle_ready_handler"); } downloader.StartTransfer(handle); }; req.Error += (s, e) => { if (OnError != null) { OnError(e.Error); } }; transport.EnqueueRequest(req); return(req); }
public void UpdateNodeAttrSync(MegaNode node) { int? error = null; UpdateNodeAttr(node, null, (e) => error = e).ResetEvent.WaitOne(); if (error != null) { throw new MegaApiException((int)error, "Could not update the node"); } }
public IMegaRequest UpdateNodeAttr(MegaNode node, Action OnSuccess, Action<int> OnError) { if (node==null || node.Attributes == null) { if (OnError != null) { OnError(MegaApiError.EWRONG); } return new EmptyRequest(); } var req = new MRequestUpdateAttributes<MResponseUpdateAttributes>(User, node); req.Success += (s, e) => { if (OnSuccess != null) { OnSuccess(); } }; req.Error += (s, e) => { if (OnError != null) { OnError(e.Error); } }; transport.EnqueueRequest(req); return req; }
public MegaNode CreateFolderSync(MegaNode target, List<MegaNode> nodes, string folder, char separator) { MegaNode result = null; int? errno = null; CreateFolder(target, nodes, folder, separator, (n) => result = n, (e) => errno = e).ResetEvent.WaitOne(); if (result == null || errno != null) { throw new MegaApiException((int)errno, String.Format("Could not create the folder {0}",folder)); } return result; }
public IMegaRequest CreateFolder(MegaNode targetNode, List<MegaNode> existingNodes, string folderPath, char separator, Action<MegaNode> OnSuccess, Action<int> OnError) { var result = new EmptyRequest(); if (string.IsNullOrEmpty(folderPath)) { OnSuccess(targetNode); return result; } result.ResetEvent.Reset(); var folders = folderPath.Split(new char[]{separator}, StringSplitOptions.RemoveEmptyEntries); CreateFolders( targetNode, existingNodes, folders, (node)=>{OnSuccess(node); result.ResetEvent.Set();}, (i) => { OnError(i); result.ResetEvent.Set(); }); return result; }
void CreateFolders( MegaNode targetNode, List<MegaNode> existingNodes, string[] folders, Action<MegaNode> OnSuccess, Action<int> OnError) { if(folders.Length == 0) { OnSuccess(targetNode); return; } MegaNode existing; lock (existingNodes) { existing = existingNodes.Where(n => n.ParentId == targetNode.Id && n.Attributes.Name == folders[0]).FirstOrDefault(); } if (existing != null) { CreateFolders( existing, existingNodes, folders.Skip(1).ToArray(), OnSuccess, OnError); } else { CreateFolder(targetNode.Id, folders[0], (n) => { lock (existingNodes) { existingNodes.Add(n); } CreateFolders( n, existingNodes, folders.Skip(1).ToArray(), OnSuccess, OnError); }, (i) => OnError(i)); } }
public void DownloadFileSync(MegaNode node, string filename) { int? error = null; TransferHandle handle = null; var waitFile = new ManualResetEvent(false); DownloadFile(node, filename, (h) => { handle = h; h.TransferEnded += (s, e) => waitFile.Set(); }, (e) => error = e) .ResetEvent.WaitOne(); waitFile.WaitOne(); if (handle == null || error != null || handle.Node == null || handle.Error != null) { throw new MegaApiException(handle.Error == null ? (int)error : (int)handle.Error, "Could not upload the file"); } }
private void DownloadFile(MegaNode clickedNode) { var d = new SaveFileDialog(); d.FileName = clickedNode.Attributes.Name; if (d.ShowDialog() == true) { SetStatus("Starting download..."); api.DownloadFile(clickedNode, d.FileName, AddDownloadHandle, SetStatusError); } }
private void InitialLoadNodes() { Invoke(() => listBoxDownloads.ItemsSource = transfers); SetStatus("Retrieving the list of files..."); api.GetNodes((list) => { Invoke(() => { buttonUpload.IsEnabled = true; buttonRefresh.IsEnabled = true; buttonLogout.IsEnabled = true; buttonLogin.IsEnabled = true; }); SetStatusDone(); nodes = list; currentNode = list.Where(n => n.Type == MegaNodeType.RootFolder).FirstOrDefault(); ShowFiles(); }, e => SetStatusError(e)); }
private void ShowFiles(MegaNode parent = null, bool refresh = false) { if (parent == null) { parent = currentNode; } if (refresh) { SetStatus("Refreshing file info..."); api.GetNodes((l) => { SetStatusDone(); nodes = l; ShowFiles(parent); }, e => SetStatusError(e)); return; } var list = nodes.Where(n => n.ParentId == parent.Id).ToList<MegaNode>(); currentNode = parent.Type == MegaNodeType.Dummy ? nodes.Where(n => n.Id == parent.Id).FirstOrDefault() : parent; if (currentNode.Type != MegaNodeType.RootFolder) { var p = nodes.Where(n => n.Id == currentNode.ParentId).FirstOrDefault(); list.Insert(0, new MegaNode { Id = p.Id, Attributes = new NodeAttributes { Name = ".." }, Type = MegaNodeType.Dummy }); } Invoke(() => listBoxNodes.ItemsSource = list); }
MegaNodeHelper HelperFromNode(MegaNode node) { return new MegaNodeHelper { Node = node, Parent = serverNodes.Where(n => n.Node.Id == node.ParentId).FirstOrDefault() }; }
private void ScheduleUpload(string[] files, MegaNode target) { SetStatus("Adding files and folders..."); var list = new List<Tuple<string, string>>(); foreach (var file in files) { var root = Path.GetDirectoryName(file); list.Add(new Tuple<string, string>(file, root)); if ((new FileInfo(file).Attributes & FileAttributes.Directory) == FileAttributes.Directory) { AddDirectoryContent(file, list, root); } } SetStatus("Preparing MEGA folders..."); foreach (var file in list) { var filename = file.Item1.Replace(file.Item2, "").TrimStart(Path.DirectorySeparatorChar); var folder = Path.GetDirectoryName(filename); try { var d = api.CreateFolderSync(target, nodes, folder, Path.DirectorySeparatorChar); var fi = new FileInfo(file.Item1); if ((fi.Attributes & FileAttributes.Directory) == FileAttributes.Directory) { try { nodes.Add(api.CreateFolderSync(d.Id, Path.GetFileName(filename))); } catch (MegaApiException e) { SetStatusError(e.ErrorNumber); } } else { if (fi.Length > 0) { api.UploadFile(d.Id, file.Item1, AddUploadHandle, SetStatusError); } } } catch (MegaApiException e) { SetStatusError(e.ErrorNumber); } } SetStatusDone(); }
bool IsInsideWorkingRoot(MegaNode node) { lock(serverNodes) { var parent = serverNodes.Where(n => n.Node.Id == node.ParentId).FirstOrDefault(); if (parent == null && node.ParentId != rootFolder.Node.Id) { return false; } return true; } }