public async Task <IActionResult> CreateBlobAsync([FromForm] FileUploadForm upload) { var file = upload.File; if (file == null) { var response = new Response <string>(); foreach (var error in this.ModelState.Values.SelectMany(modelState => modelState.Errors)) { response.AddError(error.ErrorMessage); } return(this.UnprocessableEntity(response)); } this.m_logger.LogDebug($"Creating file/blob: {file.Name}"); var blob = new Blob { SensorID = upload.SensorId, FileName = upload.Name, Timestamp = DateTime.UtcNow, FileSize = Convert.ToInt32(upload.File.Length) }; var auth = await this.AuthenticateUserForSensor(upload.SensorId, true).ConfigureAwait(false); if (!auth) { return(this.Forbidden()); } await using var mstream = new MemoryStream(); await upload.File.CopyToAsync(mstream); await this.m_blobService.StoreAsync(blob, mstream.ToArray()).ConfigureAwait(false); blob = await this.m_blobs.CreateAsync(blob).ConfigureAwait(false); return(this.CreatedAtRoute("GetBlobById", new { Controller = "blobs", blobId = blob.ID }, blob)); }
public async Task <IActionResult> FileUploadForm(FileUploadForm FileUpload) { using (var memoryStream = new MemoryStream()) { await FileUpload.FormFile.CopyToAsync(memoryStream); // Upload the file if less than 2 MB if (memoryStream.Length < 2097152) { await S3Upload.UploadFileAsync(memoryStream, "intex-fagelgamous", "AKIAVY5M47Y5S26WCNPL"); } else { ModelState.AddModelError("File", "The file is too large."); } } return(View()); }
public IActionResult Upload(FileUploadForm form) { var img = form.FileToUpload; string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath + "/images"); string datedFilename = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + img.FileName; string filePath = Path.Combine(uploadsFolder, datedFilename); bool successfulUpload = false; try { FileStream fs = new FileStream(filePath, FileMode.Create); img.CopyTo(fs); fs.Close(); successfulUpload = true; } catch (Exception e) { datedFilename = "File not uploaded"; _logger.LogError($"{DateTime.Now} [api/files] - Failed to upload file. {e.ToString()}"); } FilenameResponse response = new FilenameResponse() { filename = datedFilename }; if (successfulUpload) { _logger.LogInformation($"{DateTime.Now} [api/files] - successfully uploaded {datedFilename} to wwwroot/images ."); return(Ok(response)); } else { _logger.LogError($"{DateTime.Now} [api/files] - ERROR: {datedFilename} not uploaded."); return(BadRequest(response)); } }
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() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // validates if file has name (ie saved at least once) FileInfo info = new FileInfo(db.Filename); if (info.Extension == ".dwt") { Util.LogError("Please save the drawing before uploading to the gallery, aborting..."); return; } var syncContext = SynchronizationContext.Current; FileUploadForm fUp = new FileUploadForm(); fUp.UserName = Util.GetUser(); fUp.EMail = Util.GetEmail(); var dialogResult = Application.ShowModalDialog(fUp); if (dialogResult != System.Windows.Forms.DialogResult.OK) { return; } if (fUp.StoreDetails) { Util.StoreUserInfo(fUp.UserName, fUp.EMail); } SynchronizationContext.SetSynchronizationContext( syncContext); // the gallery bucket var bucketKey = "adn-viewer-gallery"; // Generates unique file key string objectKey = Guid.NewGuid().ToString() + ".dwg"; // Generate temp filename string filename = Path.GetTempFileName() + ".dwg"; db.SaveAs(filename, DwgVersion.Current); 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); 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); 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); return; } var modelName = info.Name.Substring(0, info.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); 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); Application.ShowModelessDialog(fProgress); notifier.OnTranslationCompleted += OnTranslationCompleted; notifier.Activate(); } ed.WriteMessage("\nYou successfully uploaded a new model to the gallery!"); ed.WriteMessage("\nYour model is viewable at the following url:"); ed.WriteMessage("\n" + url + "\n"); System.IO.File.Delete(filename); } }
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(); } }