focalboard/webapp/src/archiver.ts

131 lines
4.5 KiB
TypeScript
Raw Normal View History

2020-10-20 21:50:53 +02:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {IAppWindow} from './types'
Migrate webapp global state to redux (#737) * Migrating workspace tree to redux * More changes for use the redux store for boads and views * Taking into account the templates on websocket event updates * Fixing bug on boardTree maintenance * Websocket client now connects once and subscribe/desubscribe on the fly * Including usage of the new websocket client * More work around migrating to redux * WIP * WIP * WIP * WIP * WIP * WIP * Fixing some things * WIP * WIP * Another small fix * Restoring filtering, sorting and grouping * Fixing some other bugs * Add search text reducer * Fixing another drag and drop problem * Improve store names and api * Fixing small bgus * Some small fixes * fixing login * Fixing register page * Some other improvements * Removing unneeded old files * Removing the need of userCache * Fixing comments and fixing content ordering * Fixing sort * Fixing some TODOs * Fixing tests * Fixing snapshot * Fixing cypress tests * Fix eslint * Fixing server tests * Updating the add cards actions * Fixing some tiny navigation problems * Mocking the api calls to pass the tests * Migrating a new test to redux * Adding the card right after the insert of the block (not wait for ws event) * Showing the ws disconnect banner only after 5 seconds of disconnection * Fixing share view * Fix eslint * Fixing problem with sort/groupby modifications * Fixing some details on redirections and templates creation * Fixing small bugs around undo * Fix update properties on click outside the dialog * Improving the column resize look and feel * Removing the class based objects from the store (now they are all plain objects * Fix eslint * Fixing tests * Removing unneeded code
2021-08-02 17:46:00 +02:00
import {ArchiveUtils, ArchiveHeader, ArchiveLine, BlockArchiveLine} from './blocks/archive'
import {Block} from './blocks/block'
import {Board} from './blocks/board'
2021-03-03 00:14:37 +01:00
import {LineReader} from './lineReader'
2020-10-20 21:52:56 +02:00
import mutator from './mutator'
import {Utils} from './utils'
2020-10-08 18:21:27 +02:00
declare let window: IAppWindow
2020-10-08 18:21:27 +02:00
class Archiver {
Migrate webapp global state to redux (#737) * Migrating workspace tree to redux * More changes for use the redux store for boads and views * Taking into account the templates on websocket event updates * Fixing bug on boardTree maintenance * Websocket client now connects once and subscribe/desubscribe on the fly * Including usage of the new websocket client * More work around migrating to redux * WIP * WIP * WIP * WIP * WIP * WIP * Fixing some things * WIP * WIP * Another small fix * Restoring filtering, sorting and grouping * Fixing some other bugs * Add search text reducer * Fixing another drag and drop problem * Improve store names and api * Fixing small bgus * Some small fixes * fixing login * Fixing register page * Some other improvements * Removing unneeded old files * Removing the need of userCache * Fixing comments and fixing content ordering * Fixing sort * Fixing some TODOs * Fixing tests * Fixing snapshot * Fixing cypress tests * Fix eslint * Fixing server tests * Updating the add cards actions * Fixing some tiny navigation problems * Mocking the api calls to pass the tests * Migrating a new test to redux * Adding the card right after the insert of the block (not wait for ws event) * Showing the ws disconnect banner only after 5 seconds of disconnection * Fixing share view * Fix eslint * Fixing problem with sort/groupby modifications * Fixing some details on redirections and templates creation * Fixing small bugs around undo * Fix update properties on click outside the dialog * Improving the column resize look and feel * Removing the class based objects from the store (now they are all plain objects * Fix eslint * Fixing tests * Removing unneeded code
2021-08-02 17:46:00 +02:00
static async exportBoardArchive(board: Board): Promise<void> {
const blocks = await mutator.exportArchive(board.id)
2021-03-02 22:21:55 +01:00
this.exportArchive(blocks)
2020-10-20 21:50:53 +02:00
}
2020-10-20 22:36:54 +02:00
static async exportFullArchive(): Promise<void> {
const blocks = await mutator.exportArchive()
2021-03-02 22:21:55 +01:00
this.exportArchive(blocks)
2020-10-20 21:50:53 +02:00
}
Migrate webapp global state to redux (#737) * Migrating workspace tree to redux * More changes for use the redux store for boads and views * Taking into account the templates on websocket event updates * Fixing bug on boardTree maintenance * Websocket client now connects once and subscribe/desubscribe on the fly * Including usage of the new websocket client * More work around migrating to redux * WIP * WIP * WIP * WIP * WIP * WIP * Fixing some things * WIP * WIP * Another small fix * Restoring filtering, sorting and grouping * Fixing some other bugs * Add search text reducer * Fixing another drag and drop problem * Improve store names and api * Fixing small bgus * Some small fixes * fixing login * Fixing register page * Some other improvements * Removing unneeded old files * Removing the need of userCache * Fixing comments and fixing content ordering * Fixing sort * Fixing some TODOs * Fixing tests * Fixing snapshot * Fixing cypress tests * Fix eslint * Fixing server tests * Updating the add cards actions * Fixing some tiny navigation problems * Mocking the api calls to pass the tests * Migrating a new test to redux * Adding the card right after the insert of the block (not wait for ws event) * Showing the ws disconnect banner only after 5 seconds of disconnection * Fixing share view * Fix eslint * Fixing problem with sort/groupby modifications * Fixing some details on redirections and templates creation * Fixing small bugs around undo * Fix update properties on click outside the dialog * Improving the column resize look and feel * Removing the class based objects from the store (now they are all plain objects * Fix eslint * Fixing tests * Removing unneeded code
2021-08-02 17:46:00 +02:00
private static exportArchive(blocks: readonly Block[]): void {
2021-03-02 22:21:55 +01:00
const content = ArchiveUtils.buildBlockArchive(blocks)
2020-10-20 21:50:53 +02:00
const date = new Date()
2021-01-26 20:23:20 +01:00
const filename = `archive-${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}.focalboard`
2020-10-20 21:50:53 +02:00
const link = document.createElement('a')
2020-10-20 21:52:56 +02:00
link.style.display = 'none'
2020-10-20 21:50:53 +02:00
// const file = new Blob([content], { type: "text/json" })
// link.href = URL.createObjectURL(file)
link.href = 'data:text/json,' + encodeURIComponent(content)
link.download = filename
document.body.appendChild(link) // FireFox support
link.click()
// TODO: Review if this is needed in the future, this is to fix the problem with linux webview links
if (window.openInNewBrowser) {
window.openInNewBrowser(link.href)
}
2020-10-20 21:50:53 +02:00
// TODO: Remove or reuse link
}
2021-03-03 01:13:26 +01:00
private static async importBlocksFromFile(file: File): Promise<void> {
let blockCount = 0
2021-03-03 03:09:13 +01:00
const maxBlocksPerImport = 1000
Migrate webapp global state to redux (#737) * Migrating workspace tree to redux * More changes for use the redux store for boads and views * Taking into account the templates on websocket event updates * Fixing bug on boardTree maintenance * Websocket client now connects once and subscribe/desubscribe on the fly * Including usage of the new websocket client * More work around migrating to redux * WIP * WIP * WIP * WIP * WIP * WIP * Fixing some things * WIP * WIP * Another small fix * Restoring filtering, sorting and grouping * Fixing some other bugs * Add search text reducer * Fixing another drag and drop problem * Improve store names and api * Fixing small bgus * Some small fixes * fixing login * Fixing register page * Some other improvements * Removing unneeded old files * Removing the need of userCache * Fixing comments and fixing content ordering * Fixing sort * Fixing some TODOs * Fixing tests * Fixing snapshot * Fixing cypress tests * Fix eslint * Fixing server tests * Updating the add cards actions * Fixing some tiny navigation problems * Mocking the api calls to pass the tests * Migrating a new test to redux * Adding the card right after the insert of the block (not wait for ws event) * Showing the ws disconnect banner only after 5 seconds of disconnection * Fixing share view * Fix eslint * Fixing problem with sort/groupby modifications * Fixing some details on redirections and templates creation * Fixing small bugs around undo * Fix update properties on click outside the dialog * Improving the column resize look and feel * Removing the class based objects from the store (now they are all plain objects * Fix eslint * Fixing tests * Removing unneeded code
2021-08-02 17:46:00 +02:00
let blocks: Block[] = []
2021-03-02 22:21:55 +01:00
2021-03-03 00:14:37 +01:00
let isFirstLine = true
2021-03-03 01:13:26 +01:00
return new Promise<void>((resolve) => {
LineReader.readFile(file, async (line, completed) => {
2021-03-03 00:14:37 +01:00
if (completed) {
2021-03-03 01:13:26 +01:00
if (blocks.length > 0) {
await mutator.importFullArchive(blocks)
blockCount += blocks.length
}
Utils.log(`Imported ${blockCount} blocks.`)
resolve()
2021-03-03 00:14:37 +01:00
return
}
2021-03-02 22:21:55 +01:00
2021-03-03 00:14:37 +01:00
if (isFirstLine) {
isFirstLine = false
Migrate webapp global state to redux (#737) * Migrating workspace tree to redux * More changes for use the redux store for boads and views * Taking into account the templates on websocket event updates * Fixing bug on boardTree maintenance * Websocket client now connects once and subscribe/desubscribe on the fly * Including usage of the new websocket client * More work around migrating to redux * WIP * WIP * WIP * WIP * WIP * WIP * Fixing some things * WIP * WIP * Another small fix * Restoring filtering, sorting and grouping * Fixing some other bugs * Add search text reducer * Fixing another drag and drop problem * Improve store names and api * Fixing small bgus * Some small fixes * fixing login * Fixing register page * Some other improvements * Removing unneeded old files * Removing the need of userCache * Fixing comments and fixing content ordering * Fixing sort * Fixing some TODOs * Fixing tests * Fixing snapshot * Fixing cypress tests * Fix eslint * Fixing server tests * Updating the add cards actions * Fixing some tiny navigation problems * Mocking the api calls to pass the tests * Migrating a new test to redux * Adding the card right after the insert of the block (not wait for ws event) * Showing the ws disconnect banner only after 5 seconds of disconnection * Fixing share view * Fix eslint * Fixing problem with sort/groupby modifications * Fixing some details on redirections and templates creation * Fixing small bugs around undo * Fix update properties on click outside the dialog * Improving the column resize look and feel * Removing the class based objects from the store (now they are all plain objects * Fix eslint * Fixing tests * Removing unneeded code
2021-08-02 17:46:00 +02:00
const header = JSON.parse(line) as ArchiveHeader
2021-03-03 00:14:37 +01:00
if (header.date && header.version >= 1) {
const date = new Date(header.date)
Utils.log(`Import archive, version: ${header.version}, date/time: ${date.toLocaleString()}.`)
}
} else {
Migrate webapp global state to redux (#737) * Migrating workspace tree to redux * More changes for use the redux store for boads and views * Taking into account the templates on websocket event updates * Fixing bug on boardTree maintenance * Websocket client now connects once and subscribe/desubscribe on the fly * Including usage of the new websocket client * More work around migrating to redux * WIP * WIP * WIP * WIP * WIP * WIP * Fixing some things * WIP * WIP * Another small fix * Restoring filtering, sorting and grouping * Fixing some other bugs * Add search text reducer * Fixing another drag and drop problem * Improve store names and api * Fixing small bgus * Some small fixes * fixing login * Fixing register page * Some other improvements * Removing unneeded old files * Removing the need of userCache * Fixing comments and fixing content ordering * Fixing sort * Fixing some TODOs * Fixing tests * Fixing snapshot * Fixing cypress tests * Fix eslint * Fixing server tests * Updating the add cards actions * Fixing some tiny navigation problems * Mocking the api calls to pass the tests * Migrating a new test to redux * Adding the card right after the insert of the block (not wait for ws event) * Showing the ws disconnect banner only after 5 seconds of disconnection * Fixing share view * Fix eslint * Fixing problem with sort/groupby modifications * Fixing some details on redirections and templates creation * Fixing small bugs around undo * Fix update properties on click outside the dialog * Improving the column resize look and feel * Removing the class based objects from the store (now they are all plain objects * Fix eslint * Fixing tests * Removing unneeded code
2021-08-02 17:46:00 +02:00
const row = JSON.parse(line) as ArchiveLine
2021-03-03 00:14:37 +01:00
if (!row || !row.type || !row.data) {
2021-03-02 22:21:55 +01:00
Utils.logError('importFullArchive ERROR parsing line')
2021-03-03 00:14:37 +01:00
return
2021-03-02 22:21:55 +01:00
}
2021-03-03 00:14:37 +01:00
switch (row.type) {
2021-03-02 22:21:55 +01:00
case 'block': {
Migrate webapp global state to redux (#737) * Migrating workspace tree to redux * More changes for use the redux store for boads and views * Taking into account the templates on websocket event updates * Fixing bug on boardTree maintenance * Websocket client now connects once and subscribe/desubscribe on the fly * Including usage of the new websocket client * More work around migrating to redux * WIP * WIP * WIP * WIP * WIP * WIP * Fixing some things * WIP * WIP * Another small fix * Restoring filtering, sorting and grouping * Fixing some other bugs * Add search text reducer * Fixing another drag and drop problem * Improve store names and api * Fixing small bgus * Some small fixes * fixing login * Fixing register page * Some other improvements * Removing unneeded old files * Removing the need of userCache * Fixing comments and fixing content ordering * Fixing sort * Fixing some TODOs * Fixing tests * Fixing snapshot * Fixing cypress tests * Fix eslint * Fixing server tests * Updating the add cards actions * Fixing some tiny navigation problems * Mocking the api calls to pass the tests * Migrating a new test to redux * Adding the card right after the insert of the block (not wait for ws event) * Showing the ws disconnect banner only after 5 seconds of disconnection * Fixing share view * Fix eslint * Fixing problem with sort/groupby modifications * Fixing some details on redirections and templates creation * Fixing small bugs around undo * Fix update properties on click outside the dialog * Improving the column resize look and feel * Removing the class based objects from the store (now they are all plain objects * Fix eslint * Fixing tests * Removing unneeded code
2021-08-02 17:46:00 +02:00
const blockLine = row as BlockArchiveLine
2021-03-02 22:21:55 +01:00
const block = blockLine.data
2021-03-03 00:35:44 +01:00
if (Archiver.isValidBlock(block)) {
blocks.push(block)
2021-03-03 01:13:26 +01:00
if (blocks.length >= maxBlocksPerImport) {
2021-03-03 03:09:13 +01:00
const blocksToSend = blocks
blocks = []
await mutator.importFullArchive(blocksToSend)
blockCount += blocksToSend.length
2021-03-03 01:13:26 +01:00
}
2021-03-03 00:35:44 +01:00
}
2021-03-02 22:21:55 +01:00
break
}
}
}
2021-03-03 00:14:37 +01:00
})
})
2021-03-02 22:21:55 +01:00
}
Migrate webapp global state to redux (#737) * Migrating workspace tree to redux * More changes for use the redux store for boads and views * Taking into account the templates on websocket event updates * Fixing bug on boardTree maintenance * Websocket client now connects once and subscribe/desubscribe on the fly * Including usage of the new websocket client * More work around migrating to redux * WIP * WIP * WIP * WIP * WIP * WIP * Fixing some things * WIP * WIP * Another small fix * Restoring filtering, sorting and grouping * Fixing some other bugs * Add search text reducer * Fixing another drag and drop problem * Improve store names and api * Fixing small bgus * Some small fixes * fixing login * Fixing register page * Some other improvements * Removing unneeded old files * Removing the need of userCache * Fixing comments and fixing content ordering * Fixing sort * Fixing some TODOs * Fixing tests * Fixing snapshot * Fixing cypress tests * Fix eslint * Fixing server tests * Updating the add cards actions * Fixing some tiny navigation problems * Mocking the api calls to pass the tests * Migrating a new test to redux * Adding the card right after the insert of the block (not wait for ws event) * Showing the ws disconnect banner only after 5 seconds of disconnection * Fixing share view * Fix eslint * Fixing problem with sort/groupby modifications * Fixing some details on redirections and templates creation * Fixing small bugs around undo * Fix update properties on click outside the dialog * Improving the column resize look and feel * Removing the class based objects from the store (now they are all plain objects * Fix eslint * Fixing tests * Removing unneeded code
2021-08-02 17:46:00 +02:00
static isValidBlock(block: Block): boolean {
2021-03-03 00:35:44 +01:00
if (!block.id || !block.rootId) {
return false
}
return true
}
2020-10-20 21:50:53 +02:00
static importFullArchive(onComplete?: () => void): void {
const input = document.createElement('input')
2020-10-20 21:52:56 +02:00
input.type = 'file'
2021-01-26 20:23:20 +01:00
input.accept = '.focalboard'
2020-10-20 21:50:53 +02:00
input.onchange = async () => {
2020-11-13 02:24:24 +01:00
const file = input.files && input.files[0]
2021-03-02 22:21:55 +01:00
if (file) {
2021-03-03 01:13:26 +01:00
await Archiver.importBlocksFromFile(file)
2021-03-02 22:21:55 +01:00
}
2020-10-20 21:50:53 +02:00
onComplete?.()
2020-10-20 21:52:56 +02:00
}
2020-10-20 21:50:53 +02:00
2020-10-20 21:52:56 +02:00
input.style.display = 'none'
2020-10-20 21:50:53 +02:00
document.body.appendChild(input)
input.click()
// TODO: Remove or reuse input
}
2020-10-08 18:21:27 +02:00
}
2020-10-20 21:50:53 +02:00
export {Archiver}