protected override void GetChildItems(string path, bool recurse) { // Files = me/skydrive:me/skydrive/files // Folders = me/skydrive:me/skydrive/files?filter=folders // Photos = me/skydrive:me/skydrive/files?filter=albums OneDriveInfo oneDrive = PSDriveInfo as OneDriveInfo; LiveOperationCompletedEventArgs eventArgs = default(LiveOperationCompletedEventArgs); using (ManualResetEvent signal = new ManualResetEvent(false)) { oneDrive.Client.GetCompleted += (s, e) => { eventArgs = e; signal.Set(); }; oneDrive.Client.GetAsync(hiddenRoot + path, signal); signal.WaitOne(); } var items = ((object[])eventArgs.Result["data"]).Cast <IDictionary <string, object> >(); foreach (var item in items) { OneDriveItem onedriveItem = new OneDriveItem(item); WriteItemObject(onedriveItem, hiddenRoot + onedriveItem.Name, onedriveItem.IsFolder || onedriveItem.IsAlbum ? true : false); } }
protected override bool IsValidPath(string path) { if (PathIsDrive(path)) { return(true); } try { OneDriveInfo oneDrive = PSDriveInfo as OneDriveInfo; LiveOperationCompletedEventArgs eventArgs = default(LiveOperationCompletedEventArgs); using (ManualResetEvent signal = new ManualResetEvent(false)) { oneDrive.Client.GetCompleted += (s, e) => { eventArgs = e; signal.Set(); }; oneDrive.Client.GetAsync(hiddenRoot + path, signal); signal.WaitOne(); } OneDriveItem onedriveItem = new OneDriveItem((((object[])eventArgs.Result["data"]).Cast <IDictionary <string, object> >()).First()); return(onedriveItem.IsFolder || onedriveItem.IsAlbum ? true : false); } catch (Exception) { return(false); } }
protected override PSDriveInfo NewDrive(PSDriveInfo drive) { try { using (var dialog = new SignInDialog()) { dialog.Scopes = new[] { "wl.basic", "wl.signin", "wl.offline_access", "wl.skydrive_update", "wl.contacts_skydrive", "wl.emails" }; dialog.ShowInTaskbar = true; dialog.Locale = "en"; dialog.ClientId = "000000004412E411"; if (dialog.ShowDialog() == DialogResult.OK) { OneDriveInfo oneDrive = new OneDriveInfo(drive); oneDrive.Client = new LiveConnectClient(dialog.Session); LiveOperationCompletedEventArgs eventArgs = default(LiveOperationCompletedEventArgs); using (ManualResetEvent signal = new ManualResetEvent(false)) { oneDrive.Client.GetCompleted += (s, e) => { eventArgs = e; signal.Set(); }; oneDrive.Client.GetAsync("me", signal); signal.WaitOne(); } if (eventArgs.Error == null) { foreach (var key in eventArgs.Result.Keys) { WriteVerbose(string.Format("{0}={1}", key, eventArgs.Result[key])); } return(oneDrive); } if (eventArgs.Cancelled) { WriteError(new ErrorRecord(new Exception("Operation cancelled by user."), "503", ErrorCategory.InvalidOperation, null)); return(null); } WriteError(new ErrorRecord(new Exception(eventArgs.Error.Message), "200", ErrorCategory.AuthenticationError, null)); return(null); } else { WriteError(new ErrorRecord(new Exception("Operation cancelled by user."), "502", ErrorCategory.InvalidOperation, null)); return(null); } } } catch (Exception ex) { WriteError(new ErrorRecord(ex.InnerException, "100", ErrorCategory.NotSpecified, null)); return(null); } }
public DeployAction TryParseDeploymentInfo(HttpRequest request, JObject payload, string targetBranch, out DeploymentInfoBase deploymentInfo) { deploymentInfo = null; string url = payload.Value <string>("RepositoryUrl"); if (string.IsNullOrWhiteSpace(url) || !url.ToLowerInvariant().Contains("api.onedrive.com")) { return(DeployAction.UnknownPayload); } /* * Expecting payload to be: * { * "RepositoryUrl": "xxx", * "AccessToken": "xxx" * } */ string accessToken = payload.Value <string>("AccessToken"); // keep email and name, so that can be re-used in later commit OneDriveInfo oneDriveInfo = new OneDriveInfo(_repositoryFactory) { Deployer = "OneDrive", RepositoryUrl = url, AccessToken = accessToken, AuthorName = _settings.GetValue("authorName"), AuthorEmail = _settings.GetValue("authorEmail") }; deploymentInfo = oneDriveInfo; deploymentInfo.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet( authorName: oneDriveInfo.AuthorName, authorEmail: oneDriveInfo.AuthorEmail, message: String.Format(CultureInfo.CurrentUICulture, Resources.OneDrive_Synchronizing) ); return(DeployAction.ProcessDeployment); }
public async Task SyncBasicTests() { var mockTracer = new Mock <ITracer>(); mockTracer .Setup(m => m.Trace(It.IsAny <string>(), It.IsAny <IDictionary <string, string> >())); var repository = Mock.Of <IRepository>(); var fileSystem = new Mock <IFileSystem>(); var fileBase = new Mock <FileBase>(); var fileInfoFactory = new Mock <IFileInfoFactory>(); var fileInfo = new Mock <FileInfoBase>(); var dirBase = new Mock <DirectoryBase>(); var dirInfoFactory = new Mock <IDirectoryInfoFactory>(); // mock dirInfo to make FileSystemHelpers.DeleteDirectorySafe not throw exception var dirInfoBase = new Mock <DirectoryInfoBase>(); fileSystem.Setup(f => f.File).Returns(fileBase.Object); fileSystem.Setup(f => f.FileInfo).Returns(fileInfoFactory.Object); fileInfoFactory.Setup(f => f.FromFileName(It.IsAny <string>())) .Returns(() => fileInfo.Object); fileSystem.Setup(f => f.Directory).Returns(dirBase.Object); fileSystem.Setup(f => f.DirectoryInfo).Returns(dirInfoFactory.Object); dirInfoFactory.Setup(d => d.FromDirectoryName(It.IsAny <string>())).Returns(dirInfoBase.Object); fileBase.Setup(fb => fb.Exists(It.IsAny <string>())).Returns((string path) => { return(path != null && (path.EndsWith("f-delete") || path.EndsWith("bar.txt"))); }); fileBase.Setup(fb => fb.SetLastWriteTimeUtc(It.IsAny <string>(), It.IsAny <DateTime>())); dirBase.Setup(d => d.Exists(It.IsAny <string>())).Returns((string path) => { return(path != null && (path.EndsWith("f-delete-dir") || path.EndsWith("f2"))); }); FileSystemHelpers.Instance = fileSystem.Object; // prepare change from OneDrive OneDriveModel.OneDriveChange change = new OneDriveModel.OneDriveChange(); change.IsDeleted = false; // prepare OneDriveInfo var info = new OneDriveInfo(); info.AccessToken = "fake-token"; info.RepositoryUrl = "https://api.onedrive.com/v1.0/drive/special/approot:/fake-folder"; info.TargetChangeset = new ChangeSet("id", "authorName", "authorEmail", "message", DateTime.UtcNow); // prepare http handler var handler = new TestMessageHandler((HttpRequestMessage message) => { StringContent content = null; if (message != null && message.RequestUri != null) { if (message.RequestUri.AbsoluteUri.Equals(info.RepositoryUrl)) { content = new StringContent(@"{ 'id': 'fake-id'}", Encoding.UTF8, "application/json"); return(new HttpResponseMessage { Content = content }); } else if (message.RequestUri.AbsoluteUri.Equals("https://api.onedrive.com/v1.0/drive/items/fake-id/view.changes")) { content = new StringContent(ViewChangePayload, Encoding.UTF8, "application/json"); return(new HttpResponseMessage { Content = content }); } else if (message.RequestUri.AbsoluteUri.EndsWith("items/A6034FFBC93398FD!331") || message.RequestUri.AbsoluteUri.EndsWith("items/A6034FFBC93398FD!330")) { content = new StringContent(@"{ '@content.downloadUrl': 'http://site-does-not-exist.microsoft.com'}", Encoding.UTF8, "application/json"); return(new HttpResponseMessage { Content = content }); } } content = new StringContent("test file content", Encoding.UTF8, "application/json"); return(new HttpResponseMessage { Content = content }); }); // perform action OneDriveHelper helper = CreateMockOneDriveHelper(handler: handler, tracer: mockTracer.Object); await helper.Sync(info, repository); // verification /* * Sycing f2 to wwwroot: * * There are 6 changes * 2 deletion * f2\f-delete (existed as file) * f2\f-delete-dir (existed as folder) * * 2 file changes * f2\foo.txt (not existed) * f2\f22\bar.txt (existed) * * 2 folder chagnes * f2 (existed) * f2\f22 (not existed) */ // deletion mockTracer.Verify(t => t.Trace(@"Deleted file D:\home\site\wwwroot\f-delete", It.Is <IDictionary <string, string> >(d => d.Count == 0))); mockTracer.Verify(t => t.Trace(@"Deleted directory D:\home\site\wwwroot\f-delete-dir", It.Is <IDictionary <string, string> >(d => d.Count == 0))); // file changes mockTracer.Verify(t => t.Trace(@"Creating file D:\home\site\wwwroot\foo.txt ...", It.Is <IDictionary <string, string> >(d => d.Count == 0))); mockTracer.Verify(t => t.Trace(@"Updating file D:\home\site\wwwroot\f22\bar.txt ...", It.Is <IDictionary <string, string> >(d => d.Count == 0))); mockTracer.Verify(t => t.Trace(@"Deleted file D:\home\site\wwwroot\f-delete", It.Is <IDictionary <string, string> >(d => d.Count == 0))); mockTracer.Verify(t => t.Trace(@"Deleted directory D:\home\site\wwwroot\f-delete-dir", It.Is <IDictionary <string, string> >(d => d.Count == 0))); // directory changes mockTracer.Verify(t => t.Trace(@"Ignore folder f2", It.Is <IDictionary <string, string> >(d => d.Count == 0))); mockTracer.Verify(t => t.Trace(@"Creating directory D:\home\site\wwwroot\f22 ...", It.Is <IDictionary <string, string> >(d => d.Count == 0))); }
protected override PSDriveInfo RemoveDrive(PSDriveInfo drive) { OneDriveInfo oneDrive = drive as OneDriveInfo; return(oneDrive); }