diff --git a/app/components/DocumentViews.tsx b/app/components/DocumentViews.tsx index e77c205e13..454b2ebfb7 100644 --- a/app/components/DocumentViews.tsx +++ b/app/components/DocumentViews.tsx @@ -61,6 +61,7 @@ function DocumentViews({ document, isOpen }: Props) { subtitle={subtitle} image={} border={false} + compact small /> ); diff --git a/app/components/List/Item.tsx b/app/components/List/Item.tsx index f548fd7c2f..98ecc82d38 100644 --- a/app/components/List/Item.tsx +++ b/app/components/List/Item.tsx @@ -10,6 +10,7 @@ type Props = { title: React.ReactNode; subtitle?: React.ReactNode; actions?: React.ReactNode; + compact?: boolean; border?: boolean; small?: boolean; }; @@ -49,6 +50,7 @@ const ListItem = ( + {content(false)} ); }; -const Wrapper = styled.div<{ $border?: boolean }>` +const Wrapper = styled.div<{ $compact?: boolean; $border?: boolean }>` display: flex; - padding: ${(props) => (props.$border === false ? 0 : "8px 0")}; - margin: ${(props) => (props.$border === false ? "8px 0" : 0)}; + margin: ${(props) => (props.$compact === false ? 0 : "8px 0")}; + padding: ${(props) => (props.$compact === false ? "8px 0" : 0)}; border-bottom: 1px solid ${(props) => props.$border === false ? "transparent" : props.theme.divider}; diff --git a/app/components/Sidebar/Settings.tsx b/app/components/Sidebar/Settings.tsx index d3041274e1..6297c710fb 100644 --- a/app/components/Sidebar/Settings.tsx +++ b/app/components/Sidebar/Settings.tsx @@ -1,6 +1,6 @@ import { observer } from "mobx-react"; import { - DocumentIcon, + NewDocumentIcon, EmailIcon, ProfileIcon, PadlockIcon, @@ -11,6 +11,7 @@ import { TeamIcon, ExpandedIcon, BeakerIcon, + DownloadIcon, } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; @@ -118,11 +119,18 @@ function SettingsSidebar() { icon={} label={t("Share Links")} /> + {can.manage && ( + } + label={t("Import")} + /> + )} {can.export && ( } - label={`${t("Import")} / ${t("Export")}`} + to="/settings/export" + icon={} + label={t("Export")} /> )} diff --git a/app/components/SocketProvider.tsx b/app/components/SocketProvider.tsx index ae9d0fc990..3f6536049e 100644 --- a/app/components/SocketProvider.tsx +++ b/app/components/SocketProvider.tsx @@ -328,15 +328,17 @@ class SocketProvider extends React.Component { } }); + this.socket.on("fileOperations.create", async (event: any) => { + const user = auth.user; + if (user) { + fileOperations.add({ ...event, user }); + } + }); + this.socket.on("fileOperations.update", async (event: any) => { const user = auth.user; - let collection = null; - if (event.collectionId) { - collection = await collections.fetch(event.collectionId); - } - if (user) { - fileOperations.add({ ...event, user, collection }); + fileOperations.add({ ...event, user }); } }); diff --git a/app/components/Spinner.tsx b/app/components/Spinner.tsx new file mode 100644 index 0000000000..bc2809f2e6 --- /dev/null +++ b/app/components/Spinner.tsx @@ -0,0 +1,59 @@ +import * as React from "react"; +import styled from "styled-components"; + +export default function Spinner(props: React.HTMLAttributes) { + return ( + + + + ); +} + +const SVG = styled.svg` + @keyframes rotator { + 0% { + transform: rotate(0deg); + } + 100% { + transform: rotate(270deg); + } + } + + animation: rotator 1.4s linear infinite; + margin: 4px; +`; + +const Circle = styled.circle` + @keyframes dash { + 0% { + stroke-dashoffset: 47; + } + 50% { + stroke-dashoffset: 11; + transform: rotate(135deg); + } + 100% { + stroke-dashoffset: 47; + transform: rotate(450deg); + } + } + + stroke: ${(props) => props.theme.textSecondary}; + stroke-dasharray: 46; + stroke-dashoffset: 0; + transform-origin: center; + animation: dash 1.4s ease-in-out infinite; +`; diff --git a/app/models/FileOperation.ts b/app/models/FileOperation.ts index d7d5c25c47..725f656c72 100644 --- a/app/models/FileOperation.ts +++ b/app/models/FileOperation.ts @@ -1,6 +1,5 @@ import { computed } from "mobx"; import BaseModal from "./BaseModel"; -import Collection from "./Collection"; import User from "./User"; class FileOperation extends BaseModal { @@ -8,11 +7,15 @@ class FileOperation extends BaseModal { state: string; - collection: Collection | null | undefined; + name: string; + + error: string | null; + + collectionId: string | null; size: number; - type: string; + type: "import" | "export"; user: User; diff --git a/app/routes/settings.tsx b/app/routes/settings.tsx index 3ca3cbcb14..1114a0c3ea 100644 --- a/app/routes/settings.tsx +++ b/app/routes/settings.tsx @@ -1,9 +1,10 @@ import * as React from "react"; import { Switch, Redirect } from "react-router-dom"; import Details from "~/scenes/Settings/Details"; +import Export from "~/scenes/Settings/Export"; import Features from "~/scenes/Settings/Features"; import Groups from "~/scenes/Settings/Groups"; -import ImportExport from "~/scenes/Settings/ImportExport"; +import Import from "~/scenes/Settings/Import"; import Notifications from "~/scenes/Settings/Notifications"; import People from "~/scenes/Settings/People"; import Profile from "~/scenes/Settings/Profile"; @@ -33,7 +34,11 @@ export default function SettingsRoutes() { {isHosted && ( )} - + + + + {/* old routes */} + ); diff --git a/app/scenes/Settings/Export.tsx b/app/scenes/Settings/Export.tsx new file mode 100644 index 0000000000..8ae663c88d --- /dev/null +++ b/app/scenes/Settings/Export.tsx @@ -0,0 +1,105 @@ +import { observer } from "mobx-react"; +import { DownloadIcon } from "outline-icons"; +import * as React from "react"; +import { useTranslation, Trans } from "react-i18next"; +import FileOperation from "~/models/FileOperation"; +import Button from "~/components/Button"; +import Heading from "~/components/Heading"; +import HelpText from "~/components/HelpText"; +import PaginatedList from "~/components/PaginatedList"; +import Scene from "~/components/Scene"; +import Subheading from "~/components/Subheading"; +import useCurrentUser from "~/hooks/useCurrentUser"; +import useStores from "~/hooks/useStores"; +import useToasts from "~/hooks/useToasts"; +import FileOperationListItem from "./components/FileOperationListItem"; + +function Export() { + const { t } = useTranslation(); + const user = useCurrentUser(); + const { fileOperations, collections } = useStores(); + const { showToast } = useToasts(); + const [isLoading, setLoading] = React.useState(false); + const [isExporting, setExporting] = React.useState(false); + + const handleExport = React.useCallback( + async (ev: React.SyntheticEvent) => { + ev.preventDefault(); + setLoading(true); + + try { + await collections.export(); + setExporting(true); + showToast(t("Export in progress…")); + } finally { + setLoading(false); + } + }, + [t, collections, showToast] + ); + + const handleDelete = React.useCallback( + async (fileOperation: FileOperation) => { + try { + await fileOperations.delete(fileOperation); + showToast(t("Export deleted")); + } catch (err) { + showToast(err.message, { + type: "error", + }); + } + }, + [fileOperations, showToast, t] + ); + + return ( + }> + {t("Export")} + + , + }} + /> + + +
+ + Recent exports + + } + renderItem={(item) => ( + + )} + /> +
+ ); +} + +export default observer(Export); diff --git a/app/scenes/Settings/Import.tsx b/app/scenes/Settings/Import.tsx new file mode 100644 index 0000000000..33d5e78ab7 --- /dev/null +++ b/app/scenes/Settings/Import.tsx @@ -0,0 +1,154 @@ +import invariant from "invariant"; +import { observer } from "mobx-react"; +import { NewDocumentIcon } from "outline-icons"; +import * as React from "react"; +import { useTranslation, Trans } from "react-i18next"; +import { VisuallyHidden } from "reakit/VisuallyHidden"; +import getDataTransferFiles from "@shared/utils/getDataTransferFiles"; +import { cdnPath } from "@shared/utils/urls"; +import Button from "~/components/Button"; +import Heading from "~/components/Heading"; +import HelpText from "~/components/HelpText"; +import Item from "~/components/List/Item"; +import OutlineLogo from "~/components/OutlineLogo"; +import PaginatedList from "~/components/PaginatedList"; +import Scene from "~/components/Scene"; +import Subheading from "~/components/Subheading"; +import useStores from "~/hooks/useStores"; +import useToasts from "~/hooks/useToasts"; +import { uploadFile } from "~/utils/uploadFile"; +import FileOperationListItem from "./components/FileOperationListItem"; + +function Import() { + const { t } = useTranslation(); + const fileRef = React.useRef(null); + const { collections, fileOperations } = useStores(); + const { showToast } = useToasts(); + const [isImporting, setImporting] = React.useState(false); + + const handleFilePicked = React.useCallback( + async (ev) => { + const files = getDataTransferFiles(ev); + const file = files[0]; + invariant(file, "File must exist to upload"); + + setImporting(true); + + try { + const attachment = await uploadFile(file, { + name: file.name, + }); + await collections.import(attachment.id); + showToast( + t("Your import is being processed, you can safely leave this page"), + { + type: "success", + timeout: 8000, + } + ); + } catch (err) { + showToast(err.message); + } finally { + if (fileRef.current) { + fileRef.current.value = ""; + } + + setImporting(false); + } + }, + [t, collections, showToast] + ); + + const handlePickFile = React.useCallback( + (ev) => { + ev.preventDefault(); + + if (fileRef.current) { + fileRef.current.click(); + } + }, + [fileRef] + ); + + return ( + }> + {t("Import")} + + + Quickly transfer your existing documents, pages, and files from other + tools and services into Outline. You can also drag and drop any HTML, + Markdown, and text documents directly into Collections in the app. + + + + + + +
+ } + title="Outline" + subtitle={t( + "Import a backup file that was previously exported from Outline" + )} + actions={ + + } + /> + } + title="Confluence" + subtitle={t("Import pages from a Confluence instance")} + actions={ + + } + /> + } + title="Notion" + subtitle={t("Import documents from Notion")} + actions={ + + } + /> +
+
+ + Recent imports + + } + renderItem={(item) => ( + + )} + /> +
+ ); +} + +export default observer(Import); diff --git a/app/scenes/Settings/ImportExport.tsx b/app/scenes/Settings/ImportExport.tsx deleted file mode 100644 index 2b04ffa439..0000000000 --- a/app/scenes/Settings/ImportExport.tsx +++ /dev/null @@ -1,274 +0,0 @@ -import invariant from "invariant"; -import { observer } from "mobx-react"; -import { CollectionIcon, DocumentIcon } from "outline-icons"; -import * as React from "react"; -import { useTranslation, Trans } from "react-i18next"; -import { VisuallyHidden } from "reakit/VisuallyHidden"; -import styled from "styled-components"; -import getDataTransferFiles from "@shared/utils/getDataTransferFiles"; -import { parseOutlineExport, Item } from "@shared/utils/zip"; -import FileOperation from "~/models/FileOperation"; -import Button from "~/components/Button"; -import Heading from "~/components/Heading"; -import HelpText from "~/components/HelpText"; -import Notice from "~/components/Notice"; -import PaginatedList from "~/components/PaginatedList"; -import Scene from "~/components/Scene"; -import Subheading from "~/components/Subheading"; -import useCurrentUser from "~/hooks/useCurrentUser"; -import useStores from "~/hooks/useStores"; -import useToasts from "~/hooks/useToasts"; -import { uploadFile } from "~/utils/uploadFile"; -import FileOperationListItem from "./components/FileOperationListItem"; - -function ImportExport() { - const { t } = useTranslation(); - const user = useCurrentUser(); - const fileRef = React.useRef(null); - const { fileOperations, collections } = useStores(); - const { showToast } = useToasts(); - const [isLoading, setLoading] = React.useState(false); - const [isImporting, setImporting] = React.useState(false); - const [isImported, setImported] = React.useState(false); - const [isExporting, setExporting] = React.useState(false); - const [file, setFile] = React.useState(); - const [importDetails, setImportDetails] = React.useState< - Item[] | undefined - >(); - - const handleImport = React.useCallback(async () => { - setImported(false); - setImporting(true); - - try { - invariant(file, "File must exist to upload"); - const attachment = await uploadFile(file, { - name: file.name, - }); - await collections.import(attachment.id); - showToast(t("Import started")); - setImported(true); - } catch (err) { - showToast(err.message); - } finally { - if (fileRef.current) { - fileRef.current.value = ""; - } - - setImporting(false); - setFile(undefined); - setImportDetails(undefined); - } - }, [t, file, collections, showToast]); - - const handleFilePicked = React.useCallback(async (ev) => { - ev.preventDefault(); - const files = getDataTransferFiles(ev); - const file = files[0]; - setFile(file); - - try { - setImportDetails(await parseOutlineExport(file)); - } catch (err) { - setImportDetails([]); - } - }, []); - - const handlePickFile = React.useCallback( - (ev) => { - ev.preventDefault(); - - if (fileRef.current) { - fileRef.current.click(); - } - }, - [fileRef] - ); - - const handleExport = React.useCallback( - async (ev: React.SyntheticEvent) => { - ev.preventDefault(); - setLoading(true); - - try { - await collections.export(); - setExporting(true); - showToast(t("Export in progress…")); - } finally { - setLoading(false); - } - }, - [t, collections, showToast] - ); - - const handleDelete = React.useCallback( - async (fileOperation: FileOperation) => { - try { - await fileOperations.delete(fileOperation); - showToast(t("Export deleted")); - } catch (err) { - showToast(err.message, { - type: "error", - }); - } - }, - [fileOperations, showToast, t] - ); - - const hasCollections = importDetails - ? !!importDetails.filter((detail) => detail.type === "collection").length - : false; - const hasDocuments = importDetails - ? !!importDetails.filter((detail) => detail.type === "document").length - : false; - const isImportable = hasCollections && hasDocuments; - - return ( - } - > - {t("Import")} - - - It is possible to import a zip file of folders and Markdown files - previously exported from an Outline instance. Support will soon be - added for importing from other services. - - - - - - {isImported && ( - - - Your file has been uploaded and the import is currently being - processed, you can safely leave this page while it completes. - - - )} - {file && !isImportable && ( - - , - }} - /> - - )} - {file && importDetails && isImportable ? ( - <> - - , - }} - /> - - {importDetails - .filter((detail) => detail.type === "collection") - .map((detail) => ( - - - {detail.name} - - ))} - - - - - ) : ( - - )} - {t("Export")} - - , - }} - /> - - -
-
- - Recent exports - - } - renderItem={(item) => ( - - )} - /> -
- ); -} - -const List = styled.ul` - padding: 0; - margin: 8px 0 0; -`; - -const ImportPreview = styled(Notice)` - margin-bottom: 16px; -`; - -const ImportPreviewItem = styled.li` - display: flex; - align-items: center; - list-style: none; -`; - -const CollectionName = styled.span` - font-weight: 500; - margin-left: 4px; -`; - -export default observer(ImportExport); diff --git a/app/scenes/Settings/components/FileOperationListItem.tsx b/app/scenes/Settings/components/FileOperationListItem.tsx index 6ac77e4179..86047e4962 100644 --- a/app/scenes/Settings/components/FileOperationListItem.tsx +++ b/app/scenes/Settings/components/FileOperationListItem.tsx @@ -1,38 +1,54 @@ +import { observer } from "mobx-react"; +import { DoneIcon, WarningIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; +import { useTheme } from "styled-components"; import FileOperation from "~/models/FileOperation"; import { Action } from "~/components/Actions"; import ListItem from "~/components/List/Item"; +import Spinner from "~/components/Spinner"; import Time from "~/components/Time"; import useCurrentUser from "~/hooks/useCurrentUser"; import FileOperationMenu from "~/menus/FileOperationMenu"; type Props = { fileOperation: FileOperation; - handleDelete: (arg0: FileOperation) => Promise; + handleDelete?: (arg0: FileOperation) => Promise; }; const FileOperationListItem = ({ fileOperation, handleDelete }: Props) => { const { t } = useTranslation(); const user = useCurrentUser(); + const theme = useTheme(); const stateMapping = { creating: t("Processing"), expired: t("Expired"), uploading: t("Processing"), - error: t("Error"), + error: t("Failed"), }; + + const iconMapping = { + creating: , + uploading: , + complete: , + error: , + }; + + const title = + fileOperation.type === "import" || fileOperation.collectionId + ? fileOperation.name + : t("All collections"); + return ( {fileOperation.state !== "complete" && ( <>{stateMapping[fileOperation.state]} •  )} + {fileOperation.error && <>{fileOperation.error} • } {t(`{{userName}} requested`, { userName: user.id === fileOperation.user.id @@ -45,7 +61,7 @@ const FileOperationListItem = ({ fileOperation, handleDelete }: Props) => { } actions={ - fileOperation.state === "complete" ? ( + fileOperation.state === "complete" && handleDelete ? ( { ); }; -export default FileOperationListItem; +export default observer(FileOperationListItem); diff --git a/app/stores/FileOperationsStore.ts b/app/stores/FileOperationsStore.ts index c790e8acf4..6191f6722e 100644 --- a/app/stores/FileOperationsStore.ts +++ b/app/stores/FileOperationsStore.ts @@ -12,15 +12,26 @@ export default class FileOperationsStore extends BaseStore { } @computed - get exports(): FileOperation[] { - return Array.from(this.data.values()).reduce( - (acc, fileOp) => (fileOp.type === "export" ? [...acc, fileOp] : acc), - [] + get imports(): FileOperation[] { + return orderBy( + Array.from(this.data.values()).reduce( + (acc, fileOp) => (fileOp.type === "import" ? [...acc, fileOp] : acc), + [] + ), + "createdAt", + "desc" ); } @computed - get orderedDataExports(): FileOperation[] { - return orderBy(this.exports, "createdAt", "desc"); + get exports(): FileOperation[] { + return orderBy( + Array.from(this.data.values()).reduce( + (acc, fileOp) => (fileOp.type === "export" ? [...acc, fileOp] : acc), + [] + ), + "createdAt", + "desc" + ); } } diff --git a/public/images/confluence.png b/public/images/confluence.png new file mode 100644 index 0000000000..467f1632a0 Binary files /dev/null and b/public/images/confluence.png differ diff --git a/public/images/notion.png b/public/images/notion.png new file mode 100644 index 0000000000..956fcfff9d Binary files /dev/null and b/public/images/notion.png differ diff --git a/server/commands/collectionImporter.ts b/server/commands/collectionImporter.ts index 17d5fee70c..887b2ef282 100644 --- a/server/commands/collectionImporter.ts +++ b/server/commands/collectionImporter.ts @@ -5,9 +5,9 @@ import File from "formidable/lib/file"; import invariant from "invariant"; import { values, keys } from "lodash"; import { v4 as uuidv4 } from "uuid"; -import { parseOutlineExport } from "@shared/utils/zip"; import Logger from "@server/logging/logger"; import { Attachment, Event, Document, Collection, User } from "@server/models"; +import { parseOutlineExport, Item } from "@server/utils/zip"; import { FileImportError } from "../errors"; import attachmentCreator from "./attachmentCreator"; import documentCreator from "./documentCreator"; @@ -30,10 +30,10 @@ export default async function collectionImporter({ }) { // load the zip structure into memory const zipData = await fs.promises.readFile(file.path); - let items; + let items: Item[]; try { - items = await await parseOutlineExport(zipData); + items = await parseOutlineExport(zipData); } catch (err) { throw FileImportError(err.message); } diff --git a/server/commands/fileOperationDeleter.ts b/server/commands/fileOperationDeleter.ts index 95f857be89..e45af66bb3 100644 --- a/server/commands/fileOperationDeleter.ts +++ b/server/commands/fileOperationDeleter.ts @@ -2,14 +2,14 @@ import { sequelize } from "@server/database/sequelize"; import { FileOperation, Event, User } from "@server/models"; export default async function fileOperationDeleter( - fileOp: FileOperation, + fileOperation: FileOperation, user: User, ip: string ) { const transaction = await sequelize.transaction(); try { - await fileOp.destroy({ + await fileOperation.destroy({ transaction, }); await Event.create( @@ -17,8 +17,7 @@ export default async function fileOperationDeleter( name: "fileOperations.delete", teamId: user.teamId, actorId: user.id, - // @ts-expect-error dataValues does exist - data: fileOp.dataValues, + modelId: fileOperation.id, ip, }, { diff --git a/server/migrations/20220206225006-add-error-to-file-operation.js b/server/migrations/20220206225006-add-error-to-file-operation.js new file mode 100644 index 0000000000..dd954ceef1 --- /dev/null +++ b/server/migrations/20220206225006-add-error-to-file-operation.js @@ -0,0 +1,13 @@ +"use strict"; + +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.addColumn("file_operations", "error", { + type: Sequelize.STRING, + allowNull: true, + }); + }, + down: async (queryInterface) => { + await queryInterface.removeColumn("file_operations", "error"); + }, +}; diff --git a/server/models/FileOperation.ts b/server/models/FileOperation.ts index 64ef45b752..d49456cda7 100644 --- a/server/models/FileOperation.ts +++ b/server/models/FileOperation.ts @@ -45,6 +45,9 @@ class FileOperation extends BaseModel { @Column url: string; + @Column + error: string | null; + @Column(DataType.BIGINT) size: number; diff --git a/server/presenters/fileOperation.ts b/server/presenters/fileOperation.ts index 5cb6a5a796..ddb14eee21 100644 --- a/server/presenters/fileOperation.ts +++ b/server/presenters/fileOperation.ts @@ -1,13 +1,16 @@ +import path from "path"; import { FileOperation } from "@server/models"; -import { presentCollection, presentUser } from "."; +import { presentUser } from "."; export default function present(data: FileOperation) { return { id: data.id, type: data.type, + name: data.collection?.name || path.basename(data.key || ""), state: data.state, - collection: data.collection ? presentCollection(data.collection) : null, + error: data.error, size: data.size, + collectionId: data.collectionId, user: presentUser(data.user), createdAt: data.createdAt, }; diff --git a/server/queues/processors/exports.ts b/server/queues/processors/exports.ts index 9b3dd75793..dfd85af551 100644 --- a/server/queues/processors/exports.ts +++ b/server/queues/processors/exports.ts @@ -19,60 +19,62 @@ export default class ExportsProcessor { const user = await User.findByPk(actorId); invariant(user, "user operation not found"); - const exportData = await FileOperation.findByPk(event.modelId); - invariant(exportData, "exportData not found"); + const fileOperation = await FileOperation.findByPk(event.modelId); + invariant(fileOperation, "fileOperation not found"); const collectionIds = - // @ts-expect-error ts-migrate(2339) FIXME: Property 'collectionId' does not exist on type 'Co... Remove this comment to see the full error message - event.collectionId || (await user.collectionIds()); + "collectionId" in event + ? event.collectionId + : await user.collectionIds(); const collections = await Collection.findAll({ where: { id: collectionIds, }, }); - this.updateFileOperation(exportData, actorId, teamId, { + this.updateFileOperation(fileOperation, actorId, teamId, { state: "creating", }); // heavy lifting of creating the zip file Logger.info( "processor", - `Archiving collections for file operation ${exportData.id}` + `Archiving collections for file operation ${fileOperation.id}` ); const filePath = await archiveCollections(collections); - let url, state; + let url; + let state: any = "creating"; try { // @ts-expect-error ts-migrate(2769) FIXME: No overload matches this call. const readBuffer = await fs.promises.readFile(filePath); // @ts-expect-error ts-migrate(2769) FIXME: No overload matches this call. const stat = await fs.promises.stat(filePath); - this.updateFileOperation(exportData, actorId, teamId, { + this.updateFileOperation(fileOperation, actorId, teamId, { state: "uploading", size: stat.size, }); Logger.info( "processor", - `Uploading archive for file operation ${exportData.id}` + `Uploading archive for file operation ${fileOperation.id}` ); url = await uploadToS3FromBuffer( readBuffer, "application/zip", - exportData.key, + fileOperation.key, "private" ); Logger.info( "processor", - `Upload complete for file operation ${exportData.id}` + `Upload complete for file operation ${fileOperation.id}` ); state = "complete"; } catch (error) { Logger.error("Error exporting collection data", error, { - fileOperationId: exportData.id, + fileOperationId: fileOperation.id, }); state = "error"; - url = null; + url = undefined; } finally { - this.updateFileOperation(exportData, actorId, teamId, { + this.updateFileOperation(fileOperation, actorId, teamId, { state, url, }); @@ -85,7 +87,7 @@ export default class ExportsProcessor { } else { mailer.sendTemplate("exportSuccess", { to: user.email, - id: exportData.id, + id: fileOperation.id, teamUrl: team.url, }); } @@ -101,15 +103,14 @@ export default class ExportsProcessor { fileOperation: FileOperation, actorId: string, teamId: string, - data: Record + data: Partial ) { await fileOperation.update(data); await Event.add({ name: "fileOperations.update", teamId, actorId, - // @ts-expect-error dataValues exists - data: fileOperation.dataValues, + modelId: fileOperation.id, }); } } diff --git a/server/queues/processors/imports.ts b/server/queues/processors/imports.ts index 871494e102..2dd35da2b2 100644 --- a/server/queues/processors/imports.ts +++ b/server/queues/processors/imports.ts @@ -3,13 +3,14 @@ import os from "os"; import File from "formidable/lib/file"; import invariant from "invariant"; import collectionImporter from "@server/commands/collectionImporter"; -import { Attachment, User } from "@server/models"; -import { Event } from "../../types"; +import { Event, FileOperation, Attachment, User } from "@server/models"; +import { Event as TEvent } from "../../types"; export default class ImportsProcessor { - async on(event: Event) { + async on(event: TEvent) { switch (event.name) { case "collections.import": { + let state, error; const { type } = event.data; const attachment = await Attachment.findByPk(event.modelId); invariant(attachment, "attachment not found"); @@ -17,22 +18,55 @@ export default class ImportsProcessor { const user = await User.findByPk(event.actorId); invariant(user, "user not found"); - const buffer: any = await attachment.buffer; - const tmpDir = os.tmpdir(); - const tmpFilePath = `${tmpDir}/upload-${event.modelId}`; - await fs.promises.writeFile(tmpFilePath, buffer); - const file = new File({ - name: attachment.name, - type: attachment.contentType, - path: tmpFilePath, + const fileOperation = await FileOperation.create({ + type: "import", + state: "creating", + size: attachment.size, + key: attachment.key, + userId: user.id, + teamId: user.teamId, }); - await collectionImporter({ - file, - user, - type, - ip: event.ip, + + await Event.add({ + name: "fileOperations.create", + modelId: fileOperation.id, + teamId: user.teamId, + actorId: user.id, }); - await attachment.destroy(); + + try { + const buffer = await attachment.buffer; + const tmpDir = os.tmpdir(); + const tmpFilePath = `${tmpDir}/upload-${event.modelId}`; + await fs.promises.writeFile(tmpFilePath, buffer as Uint8Array); + const file = new File({ + name: attachment.name, + type: attachment.contentType, + path: tmpFilePath, + }); + + await collectionImporter({ + file, + user, + type, + ip: event.ip, + }); + await attachment.destroy(); + + state = "complete"; + } catch (err) { + state = "error"; + error = err.message; + } finally { + await fileOperation.update({ state, error }); + await Event.add({ + name: "fileOperations.update", + modelId: fileOperation.id, + teamId: user.teamId, + actorId: user.id, + }); + } + return; } diff --git a/server/queues/processors/websockets.ts b/server/queues/processors/websockets.ts index 25dcd6a1ae..15bcf72e2e 100644 --- a/server/queues/processors/websockets.ts +++ b/server/queues/processors/websockets.ts @@ -3,13 +3,18 @@ import { Op } from "sequelize"; import { Document, Collection, + FileOperation, Group, CollectionGroup, GroupUser, Pin, Star, } from "@server/models"; -import { presentPin, presentStar } from "@server/presenters"; +import { + presentFileOperation, + presentPin, + presentStar, +} from "@server/presenters"; import { Event } from "../../types"; export default class WebsocketsProcessor { @@ -354,10 +359,14 @@ export default class WebsocketsProcessor { return; } + case "fileOperations.create": case "fileOperations.update": { - return socketio - .to(`user-${event.actorId}`) - .emit("fileOperations.update", event.data); + const fileOperation = await FileOperation.findByPk(event.modelId); + if (!fileOperation) { + return; + } + const data = await presentFileOperation(fileOperation); + return socketio.to(`user-${event.actorId}`).emit(event.name, data); } case "pins.create": diff --git a/server/routes/api/fileOperations.test.ts b/server/routes/api/fileOperations.test.ts index 50c2049353..964d7953e2 100644 --- a/server/routes/api/fileOperations.test.ts +++ b/server/routes/api/fileOperations.test.ts @@ -118,7 +118,7 @@ describe("#fileOperations.list", () => { expect(data.id).toBe(exportData.id); expect(data.key).toBe(undefined); expect(data.state).toBe(exportData.state); - expect(data.collection.id).toBe(collection.id); + expect(data.collectionId).toBe(collection.id); }); it("should return exports with collection data even if collection is deleted", async () => { @@ -152,7 +152,7 @@ describe("#fileOperations.list", () => { expect(data.id).toBe(exportData.id); expect(data.key).toBe(undefined); expect(data.state).toBe(exportData.state); - expect(data.collection.id).toBe(collection.id); + expect(data.collectionId).toBe(collection.id); }); it("should return exports with user data even if user is deleted", async () => { diff --git a/server/types.ts b/server/types.ts index 07932b45c2..b0c0afe41f 100644 --- a/server/types.ts +++ b/server/types.ts @@ -130,9 +130,13 @@ export type CollectionExportAllEvent = { }; export type FileOperationEvent = { - name: "fileOperations.update" | "fileOperation.delete"; + name: + | "fileOperations.create" + | "fileOperations.update" + | "fileOperation.delete"; teamId: string; actorId: string; + modelId: string; data: { type: string; state: string; diff --git a/server/utils/zip.ts b/server/utils/zip.ts index 1b81fdc4cc..57ce24722d 100644 --- a/server/utils/zip.ts +++ b/server/utils/zip.ts @@ -1,5 +1,6 @@ import fs from "fs"; -import JSZip from "jszip"; +import path from "path"; +import JSZip, { JSZipObject } from "jszip"; import tmp from "tmp"; import Logger from "@server/logging/logger"; import Attachment from "@server/models/Attachment"; @@ -10,6 +11,18 @@ import { serializeFilename } from "./fs"; import parseAttachmentIds from "./parseAttachmentIds"; import { getFileByKey } from "./s3"; +type ItemType = "collection" | "document" | "attachment"; + +export type Item = { + path: string; + dir: string; + name: string; + depth: number; + metadata: Record; + type: ItemType; + item: JSZipObject; +}; + async function addToArchive(zip: JSZip, documents: NavigationNode[]) { for (const doc of documents) { const document = await Document.findByPk(doc.id); @@ -104,3 +117,78 @@ export async function archiveCollections(collections: Collection[]) { return archiveToPath(zip); } + +export async function parseOutlineExport( + input: File | Buffer +): Promise { + const zip = await JSZip.loadAsync(input); + // this is so we can use async / await a little easier + const items: Item[] = []; + + for (const rawPath in zip.files) { + const item = zip.files[rawPath]; + + if (!item) { + throw new Error( + `No item at ${rawPath} in zip file. This zip file might be corrupt.` + ); + } + + const itemPath = rawPath.replace(/\/$/, ""); + const dir = path.dirname(itemPath); + const name = path.basename(item.name); + const depth = itemPath.split("/").length - 1; + + // known skippable items + if (itemPath.startsWith("__MACOSX") || itemPath.endsWith(".DS_Store")) { + continue; + } + + // attempt to parse extra metadata from zip comment + let metadata = {}; + + try { + metadata = item.comment ? JSON.parse(item.comment) : {}; + } catch (err) { + console.log( + `ZIP comment found for ${item.name}, but could not be parsed as metadata: ${item.comment}` + ); + } + + if (depth === 0 && !item.dir) { + throw new Error( + "Root of zip file must only contain folders representing collections" + ); + } + + let type: ItemType | undefined; + + if (depth === 0 && item.dir && name) { + type = "collection"; + } + + if (depth > 0 && !item.dir && item.name.endsWith(".md")) { + type = "document"; + } + + if (depth > 0 && !item.dir && itemPath.includes("uploads")) { + type = "attachment"; + } + + if (!type) { + continue; + } + + items.push({ + path: itemPath, + dir, + name, + depth, + type, + metadata, + item, + }); + } + + return items; +} diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 7f195cf07b..798a67d2bf 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -514,7 +514,7 @@ "No documents found for your search filters.": "No documents found for your search filters.", "Processing": "Processing", "Expired": "Expired", - "Error": "Error", + "Failed": "Failed", "All collections": "All collections", "{{userName}} requested": "{{userName}} requested", "Upload": "Upload", @@ -541,6 +541,13 @@ "Logo": "Logo", "Subdomain": "Subdomain", "Your knowledge base will be accessible at": "Your knowledge base will be accessible at", + "Export in progress…": "Export in progress…", + "Export deleted": "Export deleted", + "A full export might take some time, consider exporting a single document or collection. The exported data is a zip of your documents in Markdown format. You may leave this page once the export has started – we will email a link to {{ userEmail }} when it’s complete.": "A full export might take some time, consider exporting a single document or collection. The exported data is a zip of your documents in Markdown format. You may leave this page once the export has started – we will email a link to {{ userEmail }} when it’s complete.", + "Export Requested": "Export Requested", + "Requesting Export": "Requesting Export", + "Export Data": "Export Data", + "Recent exports": "Recent exports", "Manage optional and beta features. Changing these settings will affect the experience for all team members.": "Manage optional and beta features. Changing these settings will affect the experience for all team members.", "Collaborative editing": "Collaborative editing", "When enabled multiple people can edit documents at the same time with shared presence and live cursors.": "When enabled multiple people can edit documents at the same time with shared presence and live cursors.", @@ -548,21 +555,14 @@ "Groups can be used to organize and manage the people on your team.": "Groups can be used to organize and manage the people on your team.", "All groups": "All groups", "No groups have been created yet": "No groups have been created yet", - "Import started": "Import started", - "Export in progress…": "Export in progress…", - "Export deleted": "Export deleted", - "It is possible to import a zip file of folders and Markdown files previously exported from an Outline instance. Support will soon be added for importing from other services.": "It is possible to import a zip file of folders and Markdown files previously exported from an Outline instance. Support will soon be added for importing from other services.", - "Your file has been uploaded and the import is currently being processed, you can safely leave this page while it completes.": "Your file has been uploaded and the import is currently being processed, you can safely leave this page while it completes.", - "Sorry, the file {{ fileName }} is missing valid collections or documents.": "Sorry, the file {{ fileName }} is missing valid collections or documents.", - "{{ fileName }} looks good, the following collections and their documents will be imported:": "{{ fileName }} looks good, the following collections and their documents will be imported:", + "Your import is being processed, you can safely leave this page": "Your import is being processed, you can safely leave this page", + "Quickly transfer your existing documents, pages, and files from other tools and services into Outline. You can also drag and drop any HTML, Markdown, and text documents directly into Collections in the app.": "Quickly transfer your existing documents, pages, and files from other tools and services into Outline. You can also drag and drop any HTML, Markdown, and text documents directly into Collections in the app.", + "Import a backup file that was previously exported from Outline": "Import a backup file that was previously exported from Outline", "Uploading": "Uploading", - "Confirm & Import": "Confirm & Import", - "Choose File": "Choose File", - "A full export might take some time, consider exporting a single document or collection. The exported data is a zip of your documents in Markdown format. You may leave this page once the export has started – we will email a link to {{ userEmail }} when it’s complete.": "A full export might take some time, consider exporting a single document or collection. The exported data is a zip of your documents in Markdown format. You may leave this page once the export has started – we will email a link to {{ userEmail }} when it’s complete.", - "Export Requested": "Export Requested", - "Requesting Export": "Requesting Export", - "Export Data": "Export Data", - "Recent exports": "Recent exports", + "Import pages from a Confluence instance": "Import pages from a Confluence instance", + "Coming soon": "Coming soon", + "Import documents from Notion": "Import documents from Notion", + "Recent imports": "Recent imports", "Receive a notification whenever a new document is published": "Receive a notification whenever a new document is published", "Receive a notification when a document you created is edited": "Receive a notification when a document you created is edited", "Collection created": "Collection created", diff --git a/shared/utils/zip.ts b/shared/utils/zip.ts deleted file mode 100644 index 1377831278..0000000000 --- a/shared/utils/zip.ts +++ /dev/null @@ -1,79 +0,0 @@ -import path from "path"; -import JSZip, { JSZipObject } from "jszip"; - -type ItemType = "collection" | "document" | "attachment"; - -export type Item = { - path: string; - dir: string; - name: string; - depth: number; - metadata: Record; - type: ItemType; - item: JSZipObject; -}; - -export async function parseOutlineExport( - input: File | Buffer -): Promise { - const zip = await JSZip.loadAsync(input); - // this is so we can use async / await a little easier - const items: Item[] = []; - zip.forEach(async function (rawPath, item) { - const itemPath = rawPath.replace(/\/$/, ""); - const dir = path.dirname(itemPath); - const name = path.basename(item.name); - const depth = itemPath.split("/").length - 1; - - // known skippable items - if (itemPath.startsWith("__MACOSX") || itemPath.endsWith(".DS_Store")) { - return; - } - - // attempt to parse extra metadata from zip comment - let metadata = {}; - - try { - metadata = item.comment ? JSON.parse(item.comment) : {}; - } catch (err) { - console.log( - `ZIP comment found for ${item.name}, but could not be parsed as metadata: ${item.comment}` - ); - } - - if (depth === 0 && !item.dir) { - throw new Error( - "Root of zip file must only contain folders representing collections" - ); - } - - let type: ItemType | undefined; - - if (depth === 0 && item.dir && name) { - type = "collection"; - } - - if (depth > 0 && !item.dir && item.name.endsWith(".md")) { - type = "document"; - } - - if (depth > 0 && !item.dir && itemPath.includes("uploads")) { - type = "attachment"; - } - - if (!type) { - return; - } - - items.push({ - path: itemPath, - dir, - name, - depth, - type, - metadata, - item, - }); - }); - return items; -}