2020-10-20 21:50:53 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
2021-10-16 18:36:03 +02:00
|
|
|
|
|
|
|
import {IAppWindow} from './types'
|
2021-08-02 17:46:00 +02:00
|
|
|
import {Block} from './blocks/block'
|
|
|
|
import {Board} from './blocks/board'
|
2020-10-20 21:52:56 +02:00
|
|
|
import mutator from './mutator'
|
|
|
|
import {Utils} from './utils'
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2021-10-16 18:36:03 +02:00
|
|
|
declare let window: IAppWindow
|
|
|
|
|
2020-10-08 18:21:27 +02:00
|
|
|
class Archiver {
|
2021-08-02 17:46:00 +02:00
|
|
|
static async exportBoardArchive(board: Board): Promise<void> {
|
2022-02-02 01:01:29 +01:00
|
|
|
this.exportArchive(mutator.exportArchive(board.id))
|
2020-10-20 21:50:53 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 22:36:54 +02:00
|
|
|
static async exportFullArchive(): Promise<void> {
|
2022-02-02 01:01:29 +01:00
|
|
|
this.exportArchive(mutator.exportArchive())
|
2020-10-20 21:50:53 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 01:01:29 +01:00
|
|
|
private static exportArchive(prom: Promise<Response>): void {
|
|
|
|
// TODO: don't download whole archive before presenting SaveAs dialog.
|
|
|
|
prom.then((response) => {
|
|
|
|
response.blob().
|
|
|
|
then((blob) => {
|
|
|
|
const link = document.createElement('a')
|
|
|
|
link.style.display = 'none'
|
2020-10-20 21:50:53 +02:00
|
|
|
|
2022-02-02 01:01:29 +01:00
|
|
|
const date = new Date()
|
|
|
|
const filename = `archive-${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}.focalboard`
|
2020-10-20 21:50:53 +02:00
|
|
|
|
2022-02-02 01:01:29 +01:00
|
|
|
const file = new Blob([blob], {type: 'application/octet-stream'})
|
|
|
|
link.href = URL.createObjectURL(file)
|
|
|
|
link.download = filename
|
|
|
|
document.body.appendChild(link) // FireFox support
|
2020-10-20 21:50:53 +02:00
|
|
|
|
2022-02-02 01:01:29 +01:00
|
|
|
link.click()
|
2020-10-20 21:50:53 +02:00
|
|
|
|
2022-02-02 01:01:29 +01:00
|
|
|
// 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)
|
|
|
|
}
|
2021-04-29 15:31:17 +02:00
|
|
|
|
2022-02-02 01:01:29 +01:00
|
|
|
// TODO: Remove or reuse link and revolkObjectURL to avoid memory leak
|
|
|
|
})
|
|
|
|
})
|
2020-10-20 21:50:53 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 01:01:29 +01:00
|
|
|
private static async importArchiveFromFile(file: File): Promise<void> {
|
|
|
|
const response = await mutator.importFullArchive(file)
|
|
|
|
if (response.status !== 200) {
|
|
|
|
Utils.log('ERROR importing archive: ' + response.text())
|
|
|
|
}
|
2021-03-02 22:21:55 +01:00
|
|
|
}
|
|
|
|
|
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'
|
2022-02-02 19:25:06 +01:00
|
|
|
input.accept = '.boardarchive'
|
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) {
|
2022-02-02 01:01:29 +01:00
|
|
|
await Archiver.importArchiveFromFile(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}
|