public async Task <string> GetReadWriteSasTokenForUpdatedDatasetContent(Guid id, IPrincipal user, CancellationToken token) { var dataset = await GetDatasetEditById(id, user, token); var containerToken = string.Empty; if (dataset.EditStatus == DatasetEditStatus.ContentsModified) { containerToken = (await SasTokens.GenerateSasTokenForUpdatingDatasetContainer(dataset.ContentEditAccount, dataset.ContentEditContainer)).ToString(); } return(containerToken); }
public async Task <bool> CleanUpDatasetEditAfterImport(Guid datasetId) { // Update Azure Search with the latest document await DatasetStorage.UpdateDatasetDetailsInSearchIndex(datasetId, default); // Retreive the dataset edit record (if present) var dataset = (await Client.CreateDocumentQuery <DatasetEditStorageItem>( UserDataDocumentCollectionUri, new FeedOptions { PartitionKey = new PartitionKey(WellKnownIds.DatasetEditDatasetId.ToString()) }) .Where(d => d.Id == datasetId) .AsDocumentQuery() .GetQueryResultsAsync()) .SingleOrDefault(); if (dataset == null) { return(false); } // Delete the original dataset container (if applicable) if ( dataset.EditStatus == DatasetEditStatus.Importing && !string.IsNullOrWhiteSpace(dataset.OriginalStorageAccount) && !string.IsNullOrWhiteSpace(dataset.OriginalStorageContainer) ) { await SasTokens.DeleteDatasetContainer(new DatasetStorage { Id = dataset.Id, DatasetName = dataset.Name, AccountName = dataset.OriginalStorageAccount, ContainerName = dataset.OriginalStorageContainer, }); } // Delete the edit record await Client.DeleteDocumentAsync( UserDataDocumentUriById(datasetId.ToString()), new RequestOptions { PartitionKey = new PartitionKey(WellKnownIds.DatasetEditDatasetId.ToString()) }, default); return(true); }
public async Task <string> GetReadOnlySasTokenForOriginalDatasetContent(Guid id, IPrincipal user, CancellationToken token) { var dataset = await GetDatasetEditById(id, user, token); var containerToken = string.Empty; if (dataset.EditStatus == DatasetEditStatus.DetailsModified || dataset.EditStatus == DatasetEditStatus.ContentsModified) { var details = await DatasetStorage.GetDatasetStorageDetails(id, token); if (details is DatasetBlobStorageDetails blobDetails) { containerToken = SasTokens.CreateContainerSasToken(blobDetails.Account, blobDetails.Container).ToString(); } } return(containerToken); }
/// <summary> /// Creates the Azure storage container for the dataset and adds the attachment records /// to the nomination document. /// </summary> /// <param name="storage"></param> /// <param name="user"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <NominationStatus?> CreateDatasetStorageAsync(DatasetStorage storage, ClaimsPrincipal user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var docUri = CreateUserDataDocumentUri(storage.Id); var options = new RequestOptions { PartitionKey = new PartitionKey(WellKnownIds.DatasetNominationDatasetId.ToString()) }; Document document = await Client.ReadDocumentAsync(docUri, options); if (document == null) { return(null); } var containerUri = await SasTokens.CreateDatasetContainer(storage); var name = GetUserName(user); var email = GetUserEmail(user); var status = NominationStatus.Uploading; document.SetPropertyValue("modified", DateTime.UtcNow); document.SetPropertyValue("modifiedByUserName", name); document.SetPropertyValue("modifiedByUserEmail", email); document.SetPropertyValue("nominationStatus", status.ToString()); await Client.ReplaceDocumentAsync(document.SelfLink, document); var datasetRecordLink = new Attachment { Id = "Content", // "Slug" is ID with hard-attach ContentType = "x-azure-blockstorage", MediaLink = containerUri, }; datasetRecordLink.SetPropertyValue("storageType", "blob"); datasetRecordLink.SetPropertyValue("container", storage.ContainerName); datasetRecordLink.SetPropertyValue("account", storage.AccountName); await Client.UpsertAttachmentAsync(document.SelfLink, datasetRecordLink, options); return(status); }
public async Task <bool> CancelDatasetChanges(Guid id, IPrincipal user, CancellationToken token) { var(original, modified) = await VerifyDatasetOwnership(id, user, token); if (original == null) { throw new InvalidOperationException("Invalid dataset id."); } var status = modified?.EditStatus ?? DatasetEditStatus.Unmodified; if (!(status == DatasetEditStatus.DetailsModified || status == DatasetEditStatus.ContentsModified)) { return(false); } await Client.DeleteDocumentAsync( UserDataDocumentUriById(id.ToString()), new RequestOptions { PartitionKey = new PartitionKey(WellKnownIds.DatasetEditDatasetId.ToString()) }, token); if (modified.EditStatus == DatasetEditStatus.ContentsModified) { var datasetStorage = new DatasetStorage { Id = id, DatasetName = original.Name, AccountName = modified.ContentEditAccount, ContainerName = modified.ContentEditContainer, }; await SasTokens.DeleteDatasetContainer(datasetStorage); } return(true); }
public async Task <DatasetImportProperties> GetDatasetImportPropertiesForNomination(Guid id, CancellationToken cancellationToken) { var nomination = await GetByIdAsync(id, cancellationToken); if (nomination == null) { return(null); } var contentLink = CreateUserDataDocumentAttachmentUri(id, "Content"); var options = new RequestOptions { PartitionKey = new PartitionKey(WellKnownIds.DatasetNominationDatasetId.ToString()) }; var response = await Client.ReadAttachmentAsync(contentLink.ToString(), options).ConfigureAwait(false); var resource = response?.Resource; if (resource == null) { return(null); } var accountName = resource.GetPropertyValue <string>("account"); var containerName = resource.GetPropertyValue <string>("container"); var accessToken = await SasTokens.GenerateSasTokenForUpdatingDatasetContainer(accountName, containerName); return(new DatasetImportProperties { Id = nomination.Id, DatasetName = nomination.Name, AccessToken = accessToken.ToString(), ContainerName = containerName, AccountName = accountName, }); }
public async Task <DatasetEditStorageItem> InitiateDatasetContentEdit(Guid id, IPrincipal user, CancellationToken token) { var dataset = await GetDatasetEditById(id, user, token); if (dataset.EditStatus == DatasetEditStatus.ContentsModified) { // Already in content edit mode return(dataset); } var details = await DatasetStorage.GetDatasetStorageDetails(id, token); var blobDetails = details as DatasetBlobStorageDetails; if (blobDetails == null) { throw new InvalidOperationException("Dataset storage must be blob storage."); } var datasetStorage = new DatasetStorage { Id = dataset.Id, DatasetName = dataset.Name, AccountName = blobDetails.Account }; await SasTokens.FindUniqueDatasetUpdateContainerName(datasetStorage); await SasTokens.CreateDatasetContainer(datasetStorage); dataset.EditStatus = DatasetEditStatus.ContentsModified; dataset.ContentEditAccount = datasetStorage.AccountName; dataset.ContentEditContainer = datasetStorage.ContainerName; dataset.OriginalStorageAccount = blobDetails.Account; dataset.OriginalStorageContainer = blobDetails.Container; return(await UpdateDatasetEditItemDocument(user, dataset, token)); }
private async Task CreateNominationDocFromEditDoc(DatasetEditStorageItem dataset, CancellationToken token) { // Disallow changes to the edited container await SasTokens.DisableSasTokenForUpdatingDatasetContainer(dataset.ContentEditAccount, dataset.ContentEditContainer); // Update the status of edit document. dataset.EditStatus = DatasetEditStatus.Importing; await Client.ReplaceDocumentAsync( UserDataDocumentUriById(dataset.Id.ToString()), dataset, new RequestOptions { PartitionKey = new PartitionKey(WellKnownIds.DatasetEditDatasetId.ToString()) }, token); // Create nomination document var nomination = new DatasetNominationStorageItem { Id = dataset.Id, DatasetId = WellKnownIds.DatasetNominationDatasetId, Name = dataset.Name, Description = dataset.Description, Domain = dataset.Domain, DomainId = dataset.DomainId, SourceUri = dataset.SourceUri, ProjectUri = dataset.ProjectUri, Version = dataset.Version, Published = dataset.Published, Created = dataset.Created, Modified = dataset.Modified, License = dataset.License, LicenseId = dataset.LicenseId, Tags = (dataset.Tags ?? Enumerable.Empty <string>()).ToList(), ContactName = dataset.ContactName, ContactInfo = dataset.ContactInfo, CreatedByUserId = dataset.CreatedByUserId, CreatedByUserName = dataset.CreatedByUserName, CreatedByUserEmail = dataset.CreatedByUserEmail, ModifiedByUserName = dataset.ModifiedByUserName, ModifiedByUserEmail = dataset.ModifiedByUserEmail, IsDownloadAllowed = dataset.IsDownloadAllowed, NominationStatus = NominationStatus.Importing, NominationLicenseType = dataset.NominationLicenseType, DigitalObjectIdentifier = dataset.DigitalObjectIdentifier, OtherLicenseContentHtml = dataset.OtherLicenseContentHtml, OtherLicenseFileContent = dataset.OtherLicenseFileContent, OtherLicenseFileContentType = dataset.OtherLicenseFileContentType, OtherLicenseAdditionalInfoUrl = dataset.OtherLicenseAdditionalInfoUrl, OtherLicenseName = dataset.OtherLicenseName, OtherLicenseFileName = dataset.OtherLicenseFileName }; await Client.UpsertDocumentAsync( UserDataDocumentCollectionUri, nomination, new RequestOptions { PartitionKey = new PartitionKey(WellKnownIds.DatasetNominationDatasetId.ToString()) }, false, token); // Point nomination document to updated container var datasetRecordLink = new Attachment { Id = "Content", ContentType = "x-azure-blockstorage", MediaLink = SasTokens.GetContainerMediaLink( dataset.ContentEditAccount, dataset.ContentEditContainer) }; datasetRecordLink.SetPropertyValue("storageType", "blob"); datasetRecordLink.SetPropertyValue("container", dataset.ContentEditContainer); datasetRecordLink.SetPropertyValue("account", dataset.ContentEditAccount); await Client.UpsertAttachmentAsync( UserDataDocumentUriById(dataset.Id.ToString()), datasetRecordLink, new RequestOptions { PartitionKey = new PartitionKey(WellKnownIds.DatasetNominationDatasetId.ToString()) }, token); }