From ef5b9d081681d86e37bfb5e444779b82f5f8e059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20R=C3=A4tzel?= Date: Sun, 19 Apr 2020 19:10:32 +0000 Subject: [PATCH] Fix migrating process states from older versions Add branch for compatibility with the older encoding of reduction records into files. See https://github.com/elm-fullstack/elm-fullstack/commit/ce013157dfb523a3b1ace601eec604b883908212#diff-d7e54a6500d82439620a96092749a70e --- .../PersistentProcess.Common/ProcessStore.cs | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/implement/PersistentProcess/PersistentProcess.Common/ProcessStore.cs b/implement/PersistentProcess/PersistentProcess.Common/ProcessStore.cs index 32a99a51..ecd64dd6 100644 --- a/implement/PersistentProcess/PersistentProcess.Common/ProcessStore.cs +++ b/implement/PersistentProcess/PersistentProcess.Common/ProcessStore.cs @@ -134,17 +134,36 @@ public ReductionRecord GetReduction(byte[] reducedCompositionHash) if (fileContent == null) return null; - var reductionRecordFromFile = - JsonConvert.DeserializeObject(Encoding.UTF8.GetString(fileContent)); - - if (reducedCompositionHashBase16 != reductionRecordFromFile.ReducedCompositionHashBase16) - throw new Exception("Unexpected content in file " + string.Join("/", filePath) + ", composition hash does not match."); - - return new ReductionRecord + try { - ReducedCompositionHash = reducedCompositionHash, - ReducedValueLiteralString = reductionRecordFromFile.ReducedValue?.LiteralString, - }; + var payloadStartIndex = + /* + Previous implementation used `File.WriteAllText`: + https://github.com/elm-fullstack/elm-fullstack/blob/1cd3f00bdf5a05e9bda479c534b0458b2496393c/implement/PersistentProcess/PersistentProcess.Common/ProcessStore.cs#L183 + Looking at the files from stores in production, it seems like that caused addition of BOM. + */ + fileContent.Take(3).SequenceEqual(new byte[] { 0xEF, 0xBB, 0xBF }) + ? + 3 + : + 0; + + var reductionRecordFromFile = + JsonConvert.DeserializeObject(Encoding.UTF8.GetString(fileContent.AsSpan(payloadStartIndex))); + + if (reducedCompositionHashBase16 != reductionRecordFromFile.ReducedCompositionHashBase16) + throw new Exception("Unexpected content in file " + string.Join("/", filePath) + ", composition hash does not match."); + + return new ReductionRecord + { + ReducedCompositionHash = reducedCompositionHash, + ReducedValueLiteralString = reductionRecordFromFile.ReducedValue?.LiteralString, + }; + } + catch (Exception e) + { + throw new Exception("Failed to read reduction from file '" + string.Join("/", filePath) + "'.", e); + } } public IEnumerable ReductionsFilesNames() =>