private async Task MoveBlob(DocLibDocument doc, Container doclibContainer) { var query = new QueryDefinition("SELECT * FROM doclib dl WHERE dl.id = @id") .WithParameter("@id", doc.Id); var unsortedEntry1 = await CosmosQueryHelper.ExecuteQueryAsync <DocLibDocument>(query, doclibContainer); var unsortedEntry = unsortedEntry1.First(); if (unsortedEntry == null) { return; } await doclibContainer.DeleteItemAsync <DocLibDocument>(unsortedEntry.Id, new PartitionKey(unsortedEntry.Id)); var newBlobLocation = $"{doc.FolderName}/{doc.RegisterName}/{doc.PhysicalName}"; var source = _bcc.GetBlobClient(doc.BlobLocation); if (await source.ExistsAsync()) { var destBlob = _bcc.GetBlobClient(newBlobLocation); await destBlob.StartCopyFromUriAsync(source.Uri); } await source.DeleteAsync(); doc.BlobLocation = newBlobLocation; }
private async Task CreateCategoryAsync(DocLibDocument doc, Container doclibContainer) { var id = $"Category.{doc.Category}"; var query = new QueryDefinition("SELECT * FROM doclib dl WHERE dl.id = @id").WithParameter("@id", id); var entity = (await CosmosQueryHelper.ExecuteQueryAsync <DocLibCategory>(query, doclibContainer)).SingleOrDefault(); if (entity == null) { var cat = new DocLibCategory { Id = id, Name = doc.Category, Description = "" }; await doclibContainer.CreateItemAsync(cat); } }
private async Task CreateTagsAsync(DocLibDocument doc, Container doclibContainer) { foreach (var docTag in doc.Tags) { var id = $"Tag.{docTag}"; var query = new QueryDefinition("SELECT * FROM doclib dl WHERE dl.id = @id").WithParameter("@id", id); var entity = (await CosmosQueryHelper.ExecuteQueryAsync <DocLibTag>(query, doclibContainer)).SingleOrDefault(); if (entity == null) { var lowercased = docTag.ToLower(); var tag = new DocLibTag { Id = id, Name = lowercased, }; await doclibContainer.CreateItemAsync(tag); } } }
public async Task IndexSingleDocumentAsync(ICloudBlob blob) { var db = _cosmosClient.GetDatabase(TableNames.Doclib); var container = db.GetContainer(TableNames.Doclib); var name = blob.Name.Split("/").Last(); var id = $"UnsortedDocument.{name}"; var doc = new DocLibDocument { Id = id, Name = name, PhysicalName = blob.Name, BlobLocation = blob.Name, UploadDate = DateTimeOffset.Now, Unsorted = true, Tags = new string[0] }; await container.CreateItemAsync(doc, new PartitionKey(id)); }
public async Task <DocLibDocument> CreateDocLibDocumentAsync(DocLibDocument doc) { doc.Validate(); var db = _cosmosClient.GetDatabase(TableNames.Doclib); var docLibContainer = db.GetContainer(TableNames.Doclib); await CreateCategoryAsync(doc, docLibContainer); await CreateTagsAsync(doc, docLibContainer); var folder = await GetCurrentFolderAsync(docLibContainer) ?? await CreateFolderAsync(docLibContainer); var register = GetRegister(folder); if (string.IsNullOrEmpty(doc.FolderName) && string.IsNullOrEmpty(doc.RegisterName)) { doc.FolderName = folder.Name; doc.RegisterName = register; folder.TotalDocuments += 1; if (folder.TotalDocuments >= folder.DocumentsPerFolder) { folder.IsFull = true; } await MoveBlob(doc, docLibContainer); await docLibContainer.UpsertItemAsync(folder); } var guid = Guid.NewGuid(); var id = $"Document.{doc.Name}.{guid}"; doc.Id = id; doc.Name = $"{doc.Name}_{guid}"; doc.Unsorted = false; await docLibContainer.CreateItemAsync(doc, new PartitionKey(id)); return(doc); }