mirror of
https://github.com/outline/outline.git
synced 2026-06-13 11:25:03 +03:00
42959d66db
* wip * Implementation complete * tidying * test * Address feedback * Remove duplicative retry logic from UpdateDocumentsPopularityScoreTask. Now that we're split across many runs this is not neccessary * Refactor to subclass, config to instance * Refactor BaseTask to named export * fix: Missing partition * tsc * Feedback
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import { createContext } from "@server/context";
|
|
import { Attachment } from "@server/models";
|
|
import FileStorage from "@server/storage/files";
|
|
import { BaseTask, TaskPriority } from "./base/BaseTask";
|
|
import { sequelize } from "@server/storage/database";
|
|
|
|
type Props = {
|
|
/** The ID of the attachment */
|
|
attachmentId: string;
|
|
/** The remote URL to upload */
|
|
url: string;
|
|
};
|
|
|
|
/**
|
|
* A task that uploads the provided url to a known attachment.
|
|
*/
|
|
export default class UploadAttachmentFromUrlTask extends BaseTask<Props> {
|
|
public async perform(props: Props) {
|
|
const attachment = await Attachment.findByPk(props.attachmentId, {
|
|
rejectOnEmpty: true,
|
|
include: [{ association: "user" }],
|
|
});
|
|
|
|
try {
|
|
const res = await FileStorage.storeFromUrl(
|
|
props.url,
|
|
attachment.key,
|
|
attachment.acl
|
|
);
|
|
|
|
if (res?.url) {
|
|
await sequelize.transaction(async (transaction) => {
|
|
const ctx = createContext({ user: attachment.user, transaction });
|
|
await attachment.updateWithCtx(ctx, {
|
|
url: res.url,
|
|
size: res.contentLength,
|
|
contentType: res.contentType,
|
|
});
|
|
});
|
|
}
|
|
} catch (err) {
|
|
return { error: err.message };
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
public get options() {
|
|
return {
|
|
attempts: 3,
|
|
priority: TaskPriority.Normal,
|
|
};
|
|
}
|
|
}
|