public ActionResult Publish(TemplatePublisherModel templatePublisher, HttpPostedFileBase file, string tenantId) { CloudBlockBlob originalFileBlob = null; PublishModel publishJob = new PublishModel(); try { if (file == null) { ModelState.AddModelError(string.Empty, "Please provide file path"); } //create an instance of templateModel from inputs IEnumerable<TemplateModel> templatesFromStorage = TemplateModel.GetFromStorage(tenantId); templatePublisher.Template = templatesFromStorage.Single<TemplateModel>(x => string.Compare(x.TemplateId, templatePublisher.Template.TemplateId, StringComparison.OrdinalIgnoreCase) == 0); publishJob.TenantId = tenantId; publishJob.OriginalFileName = file.FileName; publishJob.TemplateId = templatePublisher.Template.TemplateId; publishJob.OriginalFileSizeInBytes = file.ContentLength; publishJob.SaveToStorage(); originalFileBlob = DataModel.StorageFactory.Instance.IpcAzureAppFileBlobContainer.GetBlockBlobReference(publishJob.OriginalFileBlobRef); // Create the blob by uploading the original file. using (var fileStream = file.InputStream) { originalFileBlob.UploadFromStream(fileStream); } //Create a command message for the worker role and send it by queue RmsCommand rmsCommand = new RmsCommand(RmsCommand.Command.PublishTemplate, tenantId, publishJob.OriginalFileBlobRef); CloudQueueMessage cloudQueueMessage = new CloudQueueMessage(rmsCommand.ToString()); DataModel.StorageFactory.Instance.IpcAzureAppWorkerJobQueue.AddMessage(cloudQueueMessage); //Poll for completion of job by worker role. Don't poll for more than a minute DateTime startTime = DateTime.Now; PublishModel pJob = publishJob; while (startTime.AddMinutes(1) > DateTime.Now && string.Compare(pJob.JState.ToString(), DataModel.Models.PublishModel.JobState.Completed.ToString(), true) != 0) { System.Threading.Thread.Sleep(1 * 100); pJob = DataModel.Models.PublishModel.GetFromStorage(publishJob.TenantId, publishJob.OriginalFileBlobRef); } //send the published file to the user CloudBlockBlob publishedFileblob = DataModel.StorageFactory.Instance.IpcAzureAppFileBlobContainer.GetBlockBlobReference(pJob.PublishedFileBlobRef); return File(publishedFileblob.OpenRead(), "application/force-download", pJob.PublishedFileName); } catch (Exception ex) { Trace.TraceError(ex.Message); return View("Error"); } }
/// <summary> /// Delete a specific model instance from storage /// </summary> /// <param name="toDelete">model instance to delete</param> public static void DeleteFromStorage(PublishModel toDelete) { try { PublishModel existingEntity = GetFromStorage(toDelete.PartitionKey, toDelete.RowKey); var deleteOperation = TableOperation.Delete(existingEntity); StorageFactory.Instance.IpcAzureAppTenantStateTable.Execute(deleteOperation, tableReqOptions); } catch (StorageException ex) { Trace.TraceError(ex.Message); } }