mirror of
https://github.com/outline/outline.git
synced 2026-11-30 00:50:41 +03:00
* wip * Route document list queries through findAllWithCtx Add findAllWithCtx and countWithCtx on Document and Template, unsafe* methods for system reads, and a lint rule that bans direct list queries. Remove the cached membership list, accessibleSql and findByIds in favour of one gate. * Remove test * Include drafts when loading starred documents * Scope the access gate to the user's team, move whereToSql to QueryHelper * Move collection access to Collection.findAllWithCtx Collection.accessibleIds keeps the cached id list inside the collection gate, and user.collectionIds() is a shortcut to it. List queries use findAllWithCtx and countWithCtx, system reads use the shared unsafe methods, and the admin listing in collections.list is explicit. * Check permanentDelete for each document in empty_trash
292 lines
8.4 KiB
TypeScript
292 lines
8.4 KiB
TypeScript
import fs from "fs-extra";
|
|
import { truncate } from "es-toolkit/compat";
|
|
import { Op } from "sequelize";
|
|
import type { WhereOptions } from "sequelize";
|
|
import { toError } from "@shared/utils/error";
|
|
import type { NavigationNode } from "@shared/types";
|
|
import { FileOperationState, NotificationEventType } from "@shared/types";
|
|
import { bytesToHumanReadable } from "@shared/utils/files";
|
|
import env from "@server/env";
|
|
import { ValidationError } from "@server/errors";
|
|
import Logger from "@server/logging/Logger";
|
|
import {
|
|
Attachment,
|
|
Collection,
|
|
Document,
|
|
Event,
|
|
FileOperation,
|
|
Notification,
|
|
User,
|
|
} from "@server/models";
|
|
import fileOperationPresenter from "@server/presenters/fileOperation";
|
|
import { sequelizeReadOnly } from "@server/storage/database";
|
|
import FileStorage from "@server/storage/files";
|
|
import { BaseTask, TaskPriority } from "./base/BaseTask";
|
|
|
|
type Props = {
|
|
fileOperationId: string;
|
|
};
|
|
|
|
export default abstract class ExportTask extends BaseTask<Props> {
|
|
protected jobId({ fileOperationId }: Props) {
|
|
return `export:${fileOperationId}`;
|
|
}
|
|
|
|
/**
|
|
* Transforms the data to be exported, uploads, and notifies user.
|
|
*
|
|
* @param props The props
|
|
*/
|
|
public async perform({ fileOperationId }: Props) {
|
|
Logger.info("task", `ExportTask fetching data for ${fileOperationId}`);
|
|
const fileOperation = await FileOperation.findByPk(fileOperationId, {
|
|
rejectOnEmpty: true,
|
|
});
|
|
const user = await User.findByPk(fileOperation.userId, {
|
|
rejectOnEmpty: true,
|
|
});
|
|
|
|
let filePath: string | undefined;
|
|
let readStream: fs.ReadStream | undefined;
|
|
|
|
try {
|
|
Logger.info("task", `ExportTask processing data for ${fileOperationId}`, {
|
|
options: fileOperation.options,
|
|
});
|
|
|
|
await this.updateFileOperation(fileOperation, {
|
|
state: FileOperationState.Creating,
|
|
});
|
|
|
|
filePath = await this.loadDataAndExport(fileOperation, user);
|
|
|
|
Logger.info("task", `ExportTask uploading data for ${fileOperationId}`);
|
|
|
|
await this.updateFileOperation(fileOperation, {
|
|
state: FileOperationState.Uploading,
|
|
});
|
|
|
|
const stat = await fs.stat(filePath);
|
|
readStream = fs.createReadStream(filePath);
|
|
const url = await FileStorage.store({
|
|
body: readStream,
|
|
contentLength: stat.size,
|
|
contentType: "application/zip",
|
|
key: fileOperation.key,
|
|
acl: "private",
|
|
});
|
|
|
|
await this.updateFileOperation(fileOperation, {
|
|
size: stat.size,
|
|
state: FileOperationState.Complete,
|
|
url,
|
|
});
|
|
|
|
await this.createNotification(fileOperation, user);
|
|
} catch (error) {
|
|
await this.updateFileOperation(fileOperation, {
|
|
state: FileOperationState.Error,
|
|
error: toError(error),
|
|
});
|
|
await this.createNotification(fileOperation, user);
|
|
throw error;
|
|
} finally {
|
|
// Destroy the read stream first to release the file handle before deletion
|
|
if (readStream) {
|
|
readStream.destroy();
|
|
}
|
|
if (filePath) {
|
|
void fs.unlink(filePath).catch((error) => {
|
|
Logger.error(`Failed to delete temporary file ${filePath}`, error);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
public async loadDataAndExport(
|
|
fileOperation: FileOperation,
|
|
user: User
|
|
): Promise<string> {
|
|
if (fileOperation.documentId) {
|
|
const document = await sequelizeReadOnly.transaction((transaction) =>
|
|
Document.findByPk(fileOperation.documentId!, {
|
|
include: {
|
|
model: Collection.scope("withDocumentStructure"),
|
|
as: "collection",
|
|
},
|
|
rejectOnEmpty: true,
|
|
transaction,
|
|
})
|
|
);
|
|
|
|
const [documentStructure] =
|
|
(await document.collection?.structureFor(user, {
|
|
documentId: document.id,
|
|
})) ?? [];
|
|
|
|
if (!documentStructure) {
|
|
throw new Error("Document not found in collection tree");
|
|
}
|
|
|
|
return this.exportDocument(
|
|
document,
|
|
documentStructure.children ?? [],
|
|
fileOperation.options?.includeAttachments ?? true
|
|
);
|
|
}
|
|
|
|
// ensure attachment size is within limits
|
|
if (
|
|
!fileOperation.collectionId &&
|
|
fileOperation.options?.includeAttachments &&
|
|
env.MAXIMUM_EXPORT_SIZE
|
|
) {
|
|
const totalAttachmentsSize = await Attachment.getTotalSizeForTeam(
|
|
sequelizeReadOnly,
|
|
user.teamId
|
|
);
|
|
|
|
if (totalAttachmentsSize > env.MAXIMUM_EXPORT_SIZE) {
|
|
throw ValidationError(
|
|
`${bytesToHumanReadable(
|
|
totalAttachmentsSize
|
|
)} of attachments in workspace is larger than maximum export size of ${bytesToHumanReadable(
|
|
env.MAXIMUM_EXPORT_SIZE
|
|
)}.`
|
|
);
|
|
}
|
|
}
|
|
|
|
const where: WhereOptions<Collection> = {
|
|
teamId: user.teamId,
|
|
};
|
|
|
|
if (!fileOperation.options?.includePrivate) {
|
|
where.permission = {
|
|
[Op.ne]: null,
|
|
};
|
|
}
|
|
|
|
if (fileOperation.collectionId) {
|
|
where.id = fileOperation.collectionId;
|
|
// Only the owner can export a personal collection.
|
|
where.ownerId = { [Op.or]: [{ [Op.is]: null }, { [Op.eq]: user.id }] };
|
|
} else {
|
|
where.archivedAt = {
|
|
[Op.eq]: null,
|
|
};
|
|
// A workspace export never includes personal collections.
|
|
where.ownerId = { [Op.is]: null };
|
|
}
|
|
|
|
const collections = await sequelizeReadOnly.transaction((transaction) =>
|
|
Collection.unsafeFindAll({
|
|
scope: "withDocumentStructure",
|
|
where,
|
|
transaction,
|
|
})
|
|
);
|
|
|
|
return this.exportCollections(collections, fileOperation, user);
|
|
}
|
|
|
|
/**
|
|
* Transform the data in all of the passed collections into a single Buffer.
|
|
*
|
|
* @param collections The collections to export
|
|
* @param fileOperation The file operation of the export
|
|
* @param user The user that requested the export
|
|
* @returns A promise that resolves to a temporary file path
|
|
*/
|
|
protected abstract exportCollections(
|
|
collections: Collection[],
|
|
fileOperation: FileOperation,
|
|
user: User
|
|
): Promise<string>;
|
|
|
|
/**
|
|
* Transform the data in the document into a single Buffer.
|
|
*
|
|
* @param document The document to export
|
|
* @param documentStructure Structure of document's children
|
|
* @param includeAttachments Whether to include attachments in the export
|
|
* @returns A promise that resolves to a temporary file path
|
|
*/
|
|
protected abstract exportDocument(
|
|
document: Document,
|
|
documentStructure: NavigationNode[],
|
|
includeAttachments: boolean
|
|
): Promise<string>;
|
|
|
|
/**
|
|
* Notify the user that requested the export, in the app and by email. A
|
|
* failure is logged and does not change the result of the export.
|
|
*
|
|
* @param fileOperation The FileOperation that finished.
|
|
* @param user The user that requested the export.
|
|
*/
|
|
private async createNotification(fileOperation: FileOperation, user: User) {
|
|
if (!user.subscribedToEventType(NotificationEventType.ExportCompleted)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await Notification.create({
|
|
event: NotificationEventType.ExportCompleted,
|
|
userId: user.id,
|
|
actorId: user.id,
|
|
teamId: user.teamId,
|
|
fileOperationId: fileOperation.id,
|
|
// The source lets the notification be revoked if access is lost.
|
|
documentId: fileOperation.documentId ?? undefined,
|
|
collectionId: fileOperation.collectionId ?? undefined,
|
|
});
|
|
} catch (error) {
|
|
Logger.error("Failed to create export notification", toError(error), {
|
|
fileOperationId: fileOperation.id,
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the state of the underlying FileOperation in the database and send
|
|
* an event to the client.
|
|
*
|
|
* @param fileOperation The FileOperation to update
|
|
*/
|
|
private async updateFileOperation(
|
|
fileOperation: FileOperation,
|
|
options: Omit<Partial<FileOperation>, "error"> & { error?: Error }
|
|
) {
|
|
await fileOperation.update(
|
|
{
|
|
...options,
|
|
error: options.error
|
|
? truncate(options.error.message, { length: 255 })
|
|
: undefined,
|
|
},
|
|
{
|
|
hooks: false,
|
|
}
|
|
);
|
|
|
|
await Event.schedule({
|
|
name: "fileOperations.update",
|
|
modelId: fileOperation.id,
|
|
teamId: fileOperation.teamId,
|
|
actorId: fileOperation.userId,
|
|
data: fileOperationPresenter(fileOperation),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Job options such as priority and retry strategy, as defined by Bull.
|
|
*/
|
|
public get options() {
|
|
return {
|
|
priority: TaskPriority.Background,
|
|
attempts: 1,
|
|
};
|
|
}
|
|
}
|