-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restore ArchiveUtils for import scripts (#2314)
- Loading branch information
Showing
6 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
import {Block} from '../../webapp/src/blocks/block' | ||
|
||
interface ArchiveHeader { | ||
version: number | ||
date: number | ||
} | ||
|
||
interface ArchiveLine { | ||
type: string, | ||
data: unknown, | ||
} | ||
|
||
// This schema allows the expansion of additional line types in the future | ||
interface BlockArchiveLine extends ArchiveLine { | ||
type: 'block', | ||
data: Block | ||
} | ||
|
||
class ArchiveUtils { | ||
static buildBlockArchive(blocks: readonly Block[]): string { | ||
const header: ArchiveHeader = { | ||
version: 1, | ||
date: Date.now(), | ||
} | ||
|
||
const headerString = JSON.stringify(header) | ||
let content = headerString + '\n' | ||
for (const block of blocks) { | ||
const line: BlockArchiveLine = { | ||
type: 'block', | ||
data: block, | ||
} | ||
const lineString = JSON.stringify(line) | ||
content += lineString | ||
content += '\n' | ||
} | ||
|
||
return content | ||
} | ||
|
||
static parseBlockArchive(contents: string): Block[] { | ||
const blocks: Block[] = [] | ||
const allLineStrings = contents.split('\n') | ||
if (allLineStrings.length >= 2) { | ||
const headerString = allLineStrings[0] | ||
const header = JSON.parse(headerString) as ArchiveHeader | ||
if (header.date && header.version >= 1) { | ||
const lineStrings = allLineStrings.slice(1) | ||
let lineNum = 2 | ||
for (const lineString of lineStrings) { | ||
if (!lineString) { | ||
// Ignore empty lines, e.g. last line | ||
continue | ||
} | ||
const line = JSON.parse(lineString) as ArchiveLine | ||
if (!line || !line.type || !line.data) { | ||
throw new Error(`ERROR parsing line ${lineNum}`) | ||
} | ||
switch (line.type) { | ||
case 'block': { | ||
const blockLine = line as BlockArchiveLine | ||
const block = blockLine.data | ||
blocks.push(block) | ||
break | ||
} | ||
} | ||
|
||
lineNum += 1 | ||
} | ||
} else { | ||
throw new Error('ERROR parsing header') | ||
} | ||
} | ||
|
||
return blocks | ||
} | ||
} | ||
|
||
export {ArchiveHeader, ArchiveLine, BlockArchiveLine, ArchiveUtils} |