private async Task ApplyEdit(PackageEdit edit) { string originalPath = null; try { TempDirectory = Path.Combine(Path.GetTempPath(), "NuGetService", "HandlePackageEdits"); var directory = Path.Combine(TempDirectory, edit.Id, edit.Version); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } originalPath = Path.Combine(directory, "original.nupkg"); var sourceItem = FileStorage.GetFile(edit.Id, edit.Version); Trace.TraceInformation($"Name is {sourceItem.Name}, storage uri is {sourceItem.Uri}"); // Download the original file Trace.TraceInformation($"Downloading original copy of {edit.Id} {edit.Version}"); await FileStorage.DownloadAsync(sourceItem, originalPath); Trace.TraceInformation($"Downloaded original copy of {edit.Id} {edit.Version}"); // Check that a backup exists Trace.TraceInformation($"Backing up original copy of {edit.Id} {edit.Version}"); await FileStorage.BackupAsync(originalPath, edit.Id, edit.Version, edit.Hash); Trace.TraceInformation($"Backed up original copy of {edit.Id} {edit.Version}"); // Update the nupkg manifest with the new metadata using (var originalStream = File.Open(originalPath, FileMode.Open, FileAccess.ReadWrite)) { Trace.TraceInformation($"Rewriting package file for {edit.Id} {edit.Version}"); var editActionList = edit.GetEditsAsActionList(); NupkgRewriter.RewriteNupkgManifest(originalStream, editActionList); Trace.TraceInformation($"Rewrote package file for {edit.Id} {edit.Version}"); } // Snapshot the original blob Trace.TraceInformation($"Snapshotting original blob for {edit.Id} {edit.Version} ({sourceItem.Uri.AbsoluteUri})."); var snapshot = await FileStorage.SnapshotService.CreateSnapshot(sourceItem); Trace.TraceInformation($"Snapshotted original blob for {edit.Id} {edit.Version} ({sourceItem.Uri.AbsoluteUri})."); // Upload the updated file Trace.TraceInformation($"Uploading modified package file for {edit.Id} {edit.Version} to {sourceItem.Uri.AbsoluteUri}"); await FileStorage.UploadAsync(sourceItem, originalPath); Trace.TraceInformation($"Uploaded modified package file for {edit.Id} {edit.Version} to {sourceItem.Uri.AbsoluteUri}"); // Calculate new size and hash string hash; long size; using (var originalStream = File.OpenRead(originalPath)) { size = originalStream.Length; var hashAlgorithm = HashAlgorithm.Create(HashAlgorithmName); hash = Convert.ToBase64String( hashAlgorithm.ComputeHash(originalStream)); } // Update the database try { Trace.TraceInformation($"Updating package record for {edit.Id} {edit.Version}"); await UpdateDatabaseWithEdit(edit, hash, size); Trace.TraceInformation($"Updated package record for {edit.Id} {edit.Version}"); } catch (Exception exception) { // Error occurred while updaing database, roll back the blob to the snapshot // Can't do "await" in a catch block, but this should be pretty quick since it just starts the copy Trace.TraceError($"Failed to update database! {exception}"); Trace.TraceWarning( $"Rolling back updated blob for {edit.Id} {edit.Version}. Copying snapshot {snapshot.Uri.AbsoluteUri} to {sourceItem.Uri.AbsoluteUri}"); FileStorage.SnapshotService.RestoreSnapshot(sourceItem, snapshot); Trace.TraceWarning( $"Rolled back updated blob for {edit.Id} {edit.Version}. Copying snapshot {snapshot.Uri.AbsoluteUri} to {sourceItem.Uri.AbsoluteUri}"); throw; } Trace.TraceInformation("Deleting snapshot blob {2} for {0} {1}.", edit.Id, edit.Version, snapshot.Uri.AbsoluteUri); await FileStorage.SnapshotService.DeleteSnapshotAsync(snapshot); Trace.TraceInformation("Deleted snapshot blob {2} for {0} {1}.", edit.Id, edit.Version, snapshot.Uri.AbsoluteUri); } finally { if (!string.IsNullOrEmpty(originalPath) && File.Exists(originalPath)) { File.Delete(originalPath); } } }
private async Task ApplyEdit(PackageEdit edit) { string originalPath = null; string originalReadMeMDPath = null; try { TempDirectory = Path.Combine(Path.GetTempPath(), "NuGetService", "HandlePackageEdits"); var directory = Path.Combine(TempDirectory, edit.Id, edit.Version); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } originalPath = Path.Combine(directory, "original.nupkg"); Logger.LogInformation("Downloading nupkg to {OriginalPath}", originalPath); var sourceBlob = SourceContainer.GetBlockBlobReference( StorageHelpers.GetPackageBlobName(edit.Id, edit.Version)); Logger.LogInformation("Name is {SourceBlobName}, storage uri is {StorageUri}", sourceBlob.Name, sourceBlob.StorageUri); // Download the original file Logger.LogInformation("Downloading original copy of {EditId} {EditVersion}", edit.Id, edit.Version); await sourceBlob.DownloadToFileAsync(originalPath, FileMode.Create); Logger.LogInformation("Downloaded original copy of {EditId} {EditVersion}", edit.Id, edit.Version); // Check that a backup exists var backupBlob = BackupsContainer.GetBlockBlobReference( StorageHelpers.GetPackageBackupBlobName(edit.Id, edit.Version, edit.Hash)); if (!await backupBlob.ExistsAsync()) { Logger.LogInformation("Backing up original copy of {EditId} {EditVersion}", edit.Id, edit.Version); await backupBlob.UploadFromFileAsync(originalPath); Logger.LogInformation("Backed up original copy of {EditId} {EditVersion}", edit.Id, edit.Version); } // Update the nupkg manifest with the new metadata using (var originalStream = File.Open(originalPath, FileMode.Open, FileAccess.ReadWrite)) { Logger.LogInformation("Rewriting package file for {EditId} {EditVersion}", edit.Id, edit.Version); var editActionList = edit.GetEditsAsActionList(); NupkgRewriter.RewriteNupkgManifest(originalStream, editActionList); Logger.LogInformation("Rewrote package file for {EditId} {EditVersion}", edit.Id, edit.Version); } // Snapshot the original blob Logger.LogInformation("Snapshotting original blob for {EditId} {EditVersion} ({BlobUri}).", edit.Id, edit.Version, sourceBlob.Uri.AbsoluteUri); var sourceSnapshot = await sourceBlob.CreateSnapshotAsync(); Logger.LogInformation("Snapshotted original blob for {EditId} {EditVersion} ({BlobUri}).", edit.Id, edit.Version, sourceBlob.Uri.AbsoluteUri); // Upload the updated file Logger.LogInformation("Uploading modified package file for {EditId} {EditVersion} to {BlobUri}", edit.Id, edit.Version, sourceBlob.Uri.AbsoluteUri); await sourceBlob.UploadFromFileAsync(originalPath); Logger.LogInformation("Uploaded modified package file for {EditId} {EditVersion} to {BlobUri}", edit.Id, edit.Version, sourceBlob.Uri.AbsoluteUri); // Calculate new size and hash string hash; long size; using (var originalStream = File.OpenRead(originalPath)) { size = originalStream.Length; var hashAlgorithm = HashAlgorithm.Create(HashAlgorithmName); hash = Convert.ToBase64String( hashAlgorithm.ComputeHash(originalStream)); } // ReadMe update var readMeMDBlob = await UpdateReadMeAsync(edit, directory, originalReadMeMDPath, MarkdownExtension); try { Logger.LogInformation("Updating package record for {EditId} {EditVersion}", edit.Id, edit.Version); await UpdateDatabaseWithEdit(edit, hash, size); Logger.LogInformation("Updated package record for {EditId} {EditVersion}", edit.Id, edit.Version); } catch (Exception exception) { // Error occurred while updaing database, roll back the blob to the snapshot Logger.LogError("Failed to update database! {Exception}", exception); Logger.LogWarning( "Rolling back updated blob for {EditId} {EditVersion}. Copying snapshot {SnapshotUri} to {BlobUri}", edit.Id, edit.Version, sourceSnapshot.Uri.AbsoluteUri, sourceBlob.Uri.AbsoluteUri); await sourceBlob.StartCopyAsync(sourceSnapshot); while (sourceBlob.CopyState.Status != CopyStatus.Success) { await Task.Delay(1000); } Logger.LogWarning( "Rolled back updated blob for {EditId} {EditVersion}. Copying snapshot {SnapshotUri} to {BlobUri}", sourceSnapshot.Uri.AbsoluteUri, sourceBlob.Uri.AbsoluteUri); await RollBackReadMeAsync(edit, directory, originalReadMeMDPath, readMeMDBlob.activeSnapshot, readMeMDBlob.activeBlob); throw; } if (edit.ReadMeState == ReadMeChanged) { // Delete pending ReadMes Logger.LogInformation("Deleting pending ReadMe for {EditId} {EditVersion} from {PendingReadMeMdUri}", edit.Id, edit.Version, readMeMDBlob.pendingBlob.Uri.AbsoluteUri); await readMeMDBlob.pendingBlob.DeleteIfExistsAsync(); Logger.LogInformation($"Deleted pending ReadMe for {edit.Id} {edit.Version} from {readMeMDBlob.pendingBlob.Uri.AbsoluteUri}"); } Logger.LogInformation("Deleting snapshot blob {SnapshotUri} for {EditId} {EditVersion}.", sourceSnapshot.Uri.AbsoluteUri, edit.Id, edit.Version); await sourceSnapshot.DeleteAsync(); Logger.LogInformation("Deleted snapshot blob {SnapshotUri} for {EditId} {EditVersion}.", sourceSnapshot.Uri.AbsoluteUri, edit.Id, edit.Version); if (readMeMDBlob.activeSnapshot != null) { Logger.LogInformation("Deleting snapshot ReadMe Md {ActiveReadMeMdUri} for {EditId} {EditVersion}.", readMeMDBlob.activeSnapshot.Uri.AbsoluteUri, edit.Id, edit.Version); await readMeMDBlob.activeSnapshot.DeleteAsync(); Logger.LogInformation("Deleted snapshot ReadMe Md {ActiveReadMeMdUri} for {EditId} {EditVersion}.", readMeMDBlob.activeSnapshot.Uri.AbsoluteUri, edit.Id, edit.Version); } } finally { if (!string.IsNullOrEmpty(originalPath) && File.Exists(originalPath)) { File.Delete(originalPath); } } }