main

mattermost/focalboard

Last updated at: 28/12/2023 01:41

archiver.ts

TLDR

The archiver.ts file contains a class Archiver that provides methods for exporting and importing board archives. It exports two main methods: exportBoardArchive and exportFullArchive which export a board archive in the form of a file and importFullArchive which imports a board archive from a file. Additionally, there is a method isValidBlock which checks if a given block is valid.

Methods

exportBoardArchive

This method takes a Board object as a parameter and exports the board archive associated with the board. The exported archive is saved as a file.

exportFullArchive

This method takes a teamID as a parameter and exports the complete archive for the given team. The exported archive is saved as a file.

importFullArchive

This method allows the user to import a board archive from a file. It opens a file selection dialog where the user can choose the board archive file to import. After the import is complete, an optional callback function can be provided to execute some code.

isValidBlock

This method takes a Block object as a parameter and checks if it is a valid block. It returns true if the block has an id and a boardId property, otherwise returns false.

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {IAppWindow} from './types'
import {Block} from './blocks/block'
import {Board} from './blocks/board'
import mutator from './mutator'
import {Utils} from './utils'

declare let window: IAppWindow

class Archiver {
    static async exportBoardArchive(board: Board): Promise<void> {
        this.exportArchive(mutator.exportBoardArchive(board.id))
    }

    static async exportFullArchive(teamID: string): Promise<void> {
        this.exportArchive(mutator.exportFullArchive(teamID))
    }

    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'

                    const date = new Date()
                    const filename = `archive-${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}.boardarchive`

                    const file = new Blob([blob], {type: 'application/octet-stream'})
                    link.href = URL.createObjectURL(file)
                    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)
                    }

                    // TODO: Remove or reuse link and revolkObjectURL to avoid memory leak
                })
        })
    }

    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())
        }
    }

    static isValidBlock(block: Block): boolean {
        if (!block.id || !block.boardId) {
            return false
        }

        return true
    }

    static importFullArchive(onComplete?: () => void): void {
        const input = document.createElement('input')
        input.type = 'file'
        input.accept = '.boardarchive'
        input.onchange = async () => {
            const file = input.files && input.files[0]
            if (file) {
                await Archiver.importArchiveFromFile(file)
            }

            onComplete?.()
        }

        input.style.display = 'none'
        document.body.appendChild(input)
        input.click()

        // TODO: Remove or reuse input
    }
}

export {Archiver}