private async Task <FileDocument> DownloadItemsAsJobAsync(string ownerId, IFolder targetFolder, IFolder[] folders, FileDocument[] documents) { Ensure.NotNullOrEmpty(ownerId, $""); // Choose a guid for the zipped output file name // NOPE! The Guid should be the documentID for the zip file you just created. var result = await CreateZipDocumentAsync(targetFolder, folders, documents); await _documentService.CreateAsync(result); await _folderManager.AddDocumentAsync(result, targetFolder); await _log.LogEventCreateAsync(result, ownerId); // So, I need to create the DB document record (and GUID) immediately. When the JobZip finishes, *it* needs to push // that data onto the file system! // Documents have their own folder information and are thus self-referencing, which the JobsScheduler (JSON) cannot handle // This is more minimalist data, but as the job is async, the extra CPU time to perform lookups is OK here. var tf = targetFolder.ToFolder(); var kvs = GetFlatDocuments(folders).Concat(documents.Select(x => new DocumentFolderEntry(tf, x))) .ToDictionary(document => $"{_folderManager.GetPathAsync(document.Folder).Result}/{document.Document.FileName}", document => document.Document.DocumentId) .ToArray(); // NOTE: Do *not* await this, as the whole point is to run this action asynchronously _jobs.EnqueueAsync(() => new JobZip(this, _documentService, _uploadService, _libraryZipService, _folderManager) .Execute(result, ownerId, targetFolder.Id, kvs)); // Return the guid to the view return(result); }
public async Task CopyDocument(string documentId, string sourceFolderId, string destinationFolderId, string newName = null) { Ensure.Argument.NotNullOrEmpty(documentId); Ensure.Argument.NotNullOrEmpty(sourceFolderId); Ensure.Argument.NotNullOrEmpty(destinationFolderId); var documentToMove = await _documentService.GetAsync(documentId); var originFolder = await _folderManager.GetFolderAsync(sourceFolderId); var destinationFolder = await _folderManager.GetFolderAsync(destinationFolderId); var originalId = documentToMove.DocumentId; var originalOwnerId = originFolder.OwnerId; newName = newName?.Trim(); if (!string.IsNullOrEmpty(newName) && documentToMove.FileName != newName) { // Rename documentToMove.FileName = newName; } // Copy the document object await _folderManager.CopyDocumentAsync(documentToMove, destinationFolder); // Log it await _log.LogEventCreateAsync(documentToMove, destinationFolder.OwnerId); }