diff --git a/plugins/linear/server/api/linear.ts b/plugins/linear/server/api/linear.ts index 97d47ff85b..033c2fde88 100644 --- a/plugins/linear/server/api/linear.ts +++ b/plugins/linear/server/api/linear.ts @@ -8,6 +8,7 @@ import validate from "@server/middlewares/validate"; import { IntegrationAuthentication, Integration, Team } from "@server/models"; import { APIContext } from "@server/types"; import { Linear } from "../linear"; +import UploadLinearWorkspaceLogoTask from "../tasks/UploadLinearWorkspaceLogoTask"; import * as T from "./schema"; import { LinearUtils } from "plugins/linear/shared/LinearUtils"; @@ -82,7 +83,9 @@ router.get( }, { transaction } ); - await Integration.create>( + const integration = await Integration.create< + Integration + >( { service: IntegrationService.Linear, type: IntegrationType.Embed, @@ -103,6 +106,13 @@ router.get( { transaction } ); + transaction.afterCommit(async () => { + await UploadLinearWorkspaceLogoTask.schedule({ + integrationId: integration.id, + logoUrl: workspace.logoUrl, + }); + }); + ctx.redirect(LinearUtils.successUrl()); } ); diff --git a/plugins/linear/server/index.ts b/plugins/linear/server/index.ts index 567b5ac168..32d06ce409 100644 --- a/plugins/linear/server/index.ts +++ b/plugins/linear/server/index.ts @@ -4,6 +4,7 @@ import config from "../plugin.json"; import router from "./api/linear"; import env from "./env"; import { Linear } from "./linear"; +import UploadLinearWorkspaceLogoTask from "./tasks/UploadLinearWorkspaceLogoTask"; import { uninstall } from "./uninstall"; const enabled = !!env.LINEAR_CLIENT_ID && !!env.LINEAR_CLIENT_SECRET; @@ -15,6 +16,10 @@ if (enabled) { type: Hook.API, value: router, }, + { + type: Hook.Task, + value: UploadLinearWorkspaceLogoTask, + }, { type: Hook.UnfurlProvider, value: { unfurl: Linear.unfurl, cacheExpiry: Minute.seconds }, diff --git a/plugins/linear/server/tasks/UploadLinearWorkspaceLogoTask.ts b/plugins/linear/server/tasks/UploadLinearWorkspaceLogoTask.ts new file mode 100644 index 0000000000..30529300c3 --- /dev/null +++ b/plugins/linear/server/tasks/UploadLinearWorkspaceLogoTask.ts @@ -0,0 +1,52 @@ +import { v4 as uuidv4 } from "uuid"; +import { IntegrationService, IntegrationType } from "@shared/types"; +import { Integration } from "@server/models"; +import { Buckets } from "@server/models/helpers/AttachmentHelper"; +import BaseTask, { TaskPriority } from "@server/queues/tasks/BaseTask"; +import FileStorage from "@server/storage/files"; + +type Props = { + /** The integrationId to operate on */ + integrationId: string; + /** The original logoUrl from Linear */ + logoUrl: string; +}; + +/** + * A task that uploads the provided logoUrl to storage and updates the + * Linear integration record with the new url. + */ +export default class UploadLinearWorkspaceLogoTask extends BaseTask { + public async perform(props: Props) { + const integration = await Integration.scope("withAuthentication").findByPk< + Integration + >(props.integrationId); + if (!integration || integration.service !== IntegrationService.Linear) { + return; + } + + const res = await FileStorage.storeFromUrl( + props.logoUrl, + `${Buckets.avatars}/${integration.teamId}/${uuidv4()}`, + "public-read", + { + headers: { + Authorization: `Bearer ${integration.authentication.token}`, + }, + } + ); + + if (res?.url) { + integration.settings.linear!.workspace.logoUrl = res.url; + integration.changed("settings", true); + await integration.save(); + } + } + + public get options() { + return { + attempts: 3, + priority: TaskPriority.Normal, + }; + } +} diff --git a/server/storage/files/BaseStorage.ts b/server/storage/files/BaseStorage.ts index d8ddbebb8a..0753142780 100644 --- a/server/storage/files/BaseStorage.ts +++ b/server/storage/files/BaseStorage.ts @@ -1,6 +1,7 @@ import { Blob } from "buffer"; import { Readable } from "stream"; import { PresignedPost } from "@aws-sdk/s3-presigned-post"; +import omit from "lodash/omit"; import FileHelper from "@shared/editor/lib/FileHelper"; import { isBase64Url, isInternalUrl } from "@shared/utils/urls"; import env from "@server/env"; @@ -162,6 +163,12 @@ export default abstract class BaseStorage { buffer = Buffer.from(match[2], "base64"); } else { try { + const headers = { + "User-Agent": chromeUserAgent, + ...init?.headers, + }; + const initWithoutHeaders = omit(init, ["headers"]); + const res = await fetch(url, { follow: 3, redirect: "follow", @@ -169,11 +176,9 @@ export default abstract class BaseStorage { options?.maxUploadSize ?? Infinity, env.FILE_STORAGE_UPLOAD_MAX_SIZE ), - headers: { - "User-Agent": chromeUserAgent, - }, + headers, timeout: 10000, - ...init, + ...initWithoutHeaders, }); if (!res.ok) {