|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +using System; |
| 15 | +using System.IO; |
| 16 | +using System.ServiceModel.Syndication; |
| 17 | +using System.Threading.Tasks; |
| 18 | +using System.Xml; |
| 19 | +using Amathus.Common.Reader; |
| 20 | +using Google.Cloud.Storage.V1; |
| 21 | +using Microsoft.Extensions.Logging; |
| 22 | + |
| 23 | +namespace Amathus.Common.FeedStore |
| 24 | +{ |
| 25 | + public class CloudStorageSyndFeedStore : ISyndFeedStore |
| 26 | + { |
| 27 | + private readonly string _bucketId; |
| 28 | + private readonly string _projectId; |
| 29 | + private readonly ILogger _logger; |
| 30 | + |
| 31 | + public CloudStorageSyndFeedStore(string projectId, string bucketId, ILogger logger = null) |
| 32 | + { |
| 33 | + _projectId = projectId; |
| 34 | + _bucketId = bucketId; |
| 35 | + _logger = logger; |
| 36 | + } |
| 37 | + |
| 38 | + public async Task InsertAsync(SyndicationFeed feed) |
| 39 | + { |
| 40 | + var client = StorageClient.Create(); |
| 41 | + |
| 42 | + await CreateBucketIfNeeded(client); |
| 43 | + |
| 44 | + var objectName = feed.Id.ToLowerInvariant() + ".xml"; |
| 45 | + _logger?.LogInformation($"Uploading {objectName} to bucket {_bucketId}"); |
| 46 | + |
| 47 | + var stream = GetStream(feed); |
| 48 | + await client.UploadObjectAsync(_bucketId, objectName, "application/xml", stream); |
| 49 | + |
| 50 | + _logger?.LogInformation($"Uploaded {objectName}"); |
| 51 | + } |
| 52 | + |
| 53 | + public async Task<SyndicationFeed> ReadAsync(string feedId) |
| 54 | + { |
| 55 | + if (string.IsNullOrEmpty(feedId)) |
| 56 | + { |
| 57 | + throw new ArgumentNullException(); |
| 58 | + } |
| 59 | + |
| 60 | + var bucketExists = await BucketExists(); |
| 61 | + if (!bucketExists) |
| 62 | + { |
| 63 | + throw new ArgumentException("Bucket does not exist yet"); |
| 64 | + } |
| 65 | + |
| 66 | + var client = StorageClient.Create(); |
| 67 | + |
| 68 | + var objectName = feedId.ToLowerInvariant() + "2.xml"; |
| 69 | + _logger?.LogInformation($"Reading {objectName} from bucket {_bucketId}"); |
| 70 | + |
| 71 | + var stream = new MemoryStream(); |
| 72 | + try |
| 73 | + { |
| 74 | + await client.DownloadObjectAsync(_bucketId, objectName, stream); |
| 75 | + _logger?.LogInformation($"Read {objectName}"); |
| 76 | + } |
| 77 | + catch (Exception e) |
| 78 | + { |
| 79 | + _logger.LogError($"Error reading {objectName}: " + e.Message); |
| 80 | + throw e; |
| 81 | + } |
| 82 | + |
| 83 | + _logger?.LogInformation($"Converting to SyndicationFeed"); |
| 84 | + |
| 85 | + try |
| 86 | + { |
| 87 | + stream.Position = 0; // Reset to read |
| 88 | + var reader = new DateInvariantXmlReader(stream); |
| 89 | + |
| 90 | + var feed = SyndicationFeed.Load(reader); |
| 91 | + reader.Close(); |
| 92 | + _logger?.LogInformation($"Converted to SyndicationFeed"); |
| 93 | + return feed; |
| 94 | + } |
| 95 | + catch (Exception e) |
| 96 | + { |
| 97 | + _logger?.LogError($"Error converting {e.Message}"); |
| 98 | + throw e; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + private async Task CreateBucketIfNeeded(StorageClient client) |
| 104 | + { |
| 105 | + var exists = await BucketExists(); |
| 106 | + if (exists) |
| 107 | + { |
| 108 | + _logger?.LogInformation($"Bucket exists: {_bucketId}"); |
| 109 | + |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + _logger?.LogInformation($"Bucked does not exist, creating bucket: {_bucketId}"); |
| 114 | + |
| 115 | + await client.CreateBucketAsync(_projectId, _bucketId); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private async Task<bool> BucketExists() |
| 120 | + { |
| 121 | + if (string.IsNullOrEmpty(_bucketId)) |
| 122 | + { |
| 123 | + throw new ArgumentNullException(); |
| 124 | + } |
| 125 | + |
| 126 | + try |
| 127 | + { |
| 128 | + var client = StorageClient.Create(); |
| 129 | + |
| 130 | + await client.GetBucketAsync(_bucketId); |
| 131 | + |
| 132 | + return true; |
| 133 | + |
| 134 | + } |
| 135 | + catch (Exception) |
| 136 | + { |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static MemoryStream GetStream(SyndicationFeed feed) |
| 142 | + { |
| 143 | + var stream = new MemoryStream(); |
| 144 | + var writer = XmlWriter.Create(stream); |
| 145 | + var atomFormatter = feed.GetAtom10Formatter(); |
| 146 | + atomFormatter.WriteTo(writer); |
| 147 | + writer.Close(); |
| 148 | + return stream; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments