mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* First pass * fix * tidy, tidy * Determine dimensions * docs * test getFileNameFromUrl * PR feedback * tsc
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { createContext } from "@server/context";
|
|
import { Attachment } from "@server/models";
|
|
import FileStorage from "@server/storage/files";
|
|
import BaseTask, { TaskPriority } from "./BaseTask";
|
|
|
|
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) {
|
|
const ctx = createContext({ user: attachment.user });
|
|
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,
|
|
};
|
|
}
|
|
}
|