* Allow setting permission of collections during import closes #6767 * Remove unused column
97 lines
1.9 KiB
TypeScript
97 lines
1.9 KiB
TypeScript
import { Transaction } from "sequelize";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import {
|
|
FileOperationFormat,
|
|
FileOperationType,
|
|
FileOperationState,
|
|
} from "@shared/types";
|
|
import { traceFunction } from "@server/logging/tracing";
|
|
import { Collection, Event, Team, User, FileOperation } from "@server/models";
|
|
import { Buckets } from "@server/models/helpers/AttachmentHelper";
|
|
|
|
type Props = {
|
|
collection?: Collection;
|
|
team: Team;
|
|
user: User;
|
|
format?: FileOperationFormat;
|
|
includeAttachments?: boolean;
|
|
ip: string;
|
|
transaction: Transaction;
|
|
};
|
|
|
|
function getKeyForFileOp(
|
|
teamId: string,
|
|
format: FileOperationFormat,
|
|
name: string
|
|
) {
|
|
return `${
|
|
Buckets.uploads
|
|
}/${teamId}/${uuidv4()}/${name}-export.${format.replace(/outline-/, "")}.zip`;
|
|
}
|
|
|
|
async function collectionExporter({
|
|
collection,
|
|
team,
|
|
user,
|
|
format = FileOperationFormat.MarkdownZip,
|
|
includeAttachments = true,
|
|
ip,
|
|
transaction,
|
|
}: Props) {
|
|
const collectionId = collection?.id;
|
|
const key = getKeyForFileOp(
|
|
user.teamId,
|
|
format,
|
|
collection?.name || team.name
|
|
);
|
|
const fileOperation = await FileOperation.create(
|
|
{
|
|
type: FileOperationType.Export,
|
|
state: FileOperationState.Creating,
|
|
format,
|
|
key,
|
|
url: null,
|
|
size: 0,
|
|
collectionId,
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
options: {
|
|
includeAttachments,
|
|
},
|
|
},
|
|
{
|
|
transaction,
|
|
}
|
|
);
|
|
|
|
await Event.create(
|
|
{
|
|
name: "fileOperations.create",
|
|
teamId: user.teamId,
|
|
actorId: user.id,
|
|
modelId: fileOperation.id,
|
|
collectionId,
|
|
ip,
|
|
data: {
|
|
type: FileOperationType.Export,
|
|
format,
|
|
},
|
|
},
|
|
{
|
|
transaction,
|
|
}
|
|
);
|
|
|
|
fileOperation.user = user;
|
|
|
|
if (collection) {
|
|
fileOperation.collection = collection;
|
|
}
|
|
|
|
return fileOperation;
|
|
}
|
|
|
|
export default traceFunction({
|
|
spanName: "collectionExporter",
|
|
})(collectionExporter);
|