async protected override void OnExecute(NameValueMap context) { FileUploadForm fUp = new FileUploadForm(); fUp.UserName = Util.GetUser(); fUp.EMail = Util.GetEmail(); var syncContext = SynchronizationContext.Current; var dialogResult = fUp.ShowDialog(); SynchronizationContext.SetSynchronizationContext( syncContext); if (dialogResult != System.Windows.Forms.DialogResult.OK) { Terminate(); return; } if (fUp.StoreDetails) { Util.StoreUserInfo(fUp.UserName, fUp.EMail); } // the gallery bucket var bucketKey = "adn-viewer-gallery"; // Generates unique file key string objectKey = Guid.NewGuid().ToString() + ".dwf"; // Generate temp filename string filename = System.IO.Path.GetTempFileName() + ".dwf"; if (!Util.ExportDwf( AddInSite.Application, AddInSite.Application.ActiveDocument, filename)) { Terminate(); return; } AdnViewDataClient viewDataClient = new AdnViewDataClient( UserSettings.BASE_URL, UserSettings.CONSUMER_KEY, UserSettings.CONSUMER_SECRET); var tokenResult = await viewDataClient.AuthenticateAsync(); if (!tokenResult.IsOk()) { Util.LogError("Authentication failed: " + tokenResult.Error.Reason); System.IO.File.Delete(filename); Terminate(); return; } var fi = FileUploadInfo.CreateFromFile(objectKey, filename); var bucketData = new BucketCreationData( bucketKey, BucketPolicyEnum.kPersistent); var response = await viewDataClient.UploadAndRegisterAsync( bucketData, fi); if (!response.IsOk()) { Util.LogError("Error: " + response.Error.Reason); System.IO.File.Delete(filename); Terminate(); return; } if (response is RegisterResponse) { RegisterResponse registerResponse = response as RegisterResponse; if (registerResponse.Result.ToLower() != "success") { Util.LogError("Registration failed: " + registerResponse.Result); System.IO.File.Delete(filename); Terminate(); return; } var name = AddInSite.Application.ActiveDocument.DisplayName; var modelName = name.Substring(0, name.Length - 4); var fileId = viewDataClient.GetFileId( bucketKey, objectKey); var dbModel = new DBModel( new Author(fUp.UserName, fUp.EMail), modelName, fileId, fileId.ToBase64()); AdnGalleryClient galleryClient = new AdnGalleryClient( Util.GetGalleryUrl()); var modelResponse = await galleryClient.AddModelAsync( dbModel); if (!modelResponse.IsOk()) { Util.LogError("Error: " + modelResponse.Error.ToString()); System.IO.File.Delete(filename); Terminate(); return; } var url = Util.GetGalleryUrl() + "/#/viewer?id=" + modelResponse.Model.Id; if (fUp.ShowProgress) { var notifier = new TranslationNotifier( viewDataClient, fileId, 2000); var fProgress = new ProgressForm( modelName, url, notifier); fProgress.Show(); notifier.Activate(); } System.IO.File.Delete(filename); } Terminate(); }
async static public void UploadToGallery( string filename, string modelname) { System.Windows.Forms.IWin32Window revit_window = new JtWindowHandle( Autodesk.Windows.ComponentManager.ApplicationWindow); var syncContext = SynchronizationContext.Current; FileUploadForm fUp = new FileUploadForm(); fUp.UserName = Util.GetUser(); fUp.EMail = Util.GetEmail(); var dialogResult = fUp.ShowDialog(revit_window); if (dialogResult != System.Windows.Forms.DialogResult.OK) { Util.LogError("Upload cancelled."); return; } if (fUp.StoreDetails) { Util.StoreUserInfo(fUp.UserName, fUp.EMail); } SynchronizationContext.SetSynchronizationContext( syncContext); // The gallery bucket var bucketKey = "adn-viewer-gallery"; string consumer_key, consumer_secret; if (!Util.GetConsumerCredentials( "C:/credentials.txt", out consumer_key, out consumer_secret)) { Util.LogError("Consumer credentials retrieval failed."); return; } AdnViewDataClient viewDataClient = new AdnViewDataClient( UserSettings.BASE_URL, consumer_key, consumer_secret); var tokenResult = await viewDataClient.AuthenticateAsync(); if (!tokenResult.IsOk()) { Util.LogError("Authentication failed: " + tokenResult.Error.Reason); return; } // Generate unique file key string objectKey = Guid.NewGuid().ToString() + ".rvt"; var fi = FileUploadInfo.CreateFromFile( objectKey, filename); var bucketData = new BucketCreationData( bucketKey, BucketPolicyEnum.kPersistent); var response = await viewDataClient.UploadAndRegisterAsync( bucketData, fi); if (!response.IsOk()) { Util.LogError("Error: " + response.Error.Reason); return; } if (response is RegisterResponse) { RegisterResponse registerResponse = response as RegisterResponse; if (registerResponse.Result.ToLower() != "success") { Util.LogError("Registration failed: " + registerResponse.Result); return; } var fileId = viewDataClient.GetFileId( bucketKey, objectKey); var dbModel = new DBModel( new Author(fUp.UserName, fUp.EMail), modelname, fileId, fileId.ToBase64()); string url = Util.GalleryUrl; AdnGalleryClient galleryClient = new AdnGalleryClient( url); DBModelResponse modelResponse = await Util.AddModelToGalleryAsync( dbModel); if (!modelResponse.IsOk()) { Util.LogError(string.Format("Error: '{0}' {1}", modelResponse.Error.ToString(), null == modelResponse.Model ? "model is null" : "")); return; } url = url + "/#/viewer?id=" + modelResponse.Model.Id; if (fUp.ShowProgress) { var notifier = new TranslationNotifier( viewDataClient, fileId, 2000); var fProgress = new ProgressForm( modelname, url, notifier); fProgress.Show(revit_window); notifier.OnTranslationCompleted += OnTranslationCompleted; notifier.Activate(); } string msg = "You successfully " + " uploaded a new model to the gallery.\r\n" + "Your model is viewable at the following url:\r\n" + url; Util.LogError(msg); TaskDialog dlg = new TaskDialog( "Gallery Upload"); dlg.MainInstruction = "Upload succeeded"; dlg.MainContent = msg; dlg.Show(); } }