From bc694dc76e8e40281780994043f77d94907614d6 Mon Sep 17 00:00:00 2001 From: Flavien David Date: Thu, 4 Apr 2024 17:53:15 +0200 Subject: [PATCH] Avoid requiring env variable when importing file (#4573) * Avoid requiring env variable when importing file * s/dfs/file_storage --- front/lib/api/assistant/configuration.ts | 2 +- front/lib/{dfs => file_storage}/config.ts | 0 front/lib/{dfs => file_storage}/index.ts | 30 ++++++++----------- .../resources/content_fragment_resource.ts | 2 +- .../assistant/agent_configurations/avatar.ts | 2 +- .../[mId]/raw_content_fragment/index.ts | 2 +- 6 files changed, 17 insertions(+), 21 deletions(-) rename front/lib/{dfs => file_storage}/config.ts (100%) rename front/lib/{dfs => file_storage}/index.ts (69%) diff --git a/front/lib/api/assistant/configuration.ts b/front/lib/api/assistant/configuration.ts index 35f8324b3c24..73cfe98c5024 100644 --- a/front/lib/api/assistant/configuration.ts +++ b/front/lib/api/assistant/configuration.ts @@ -34,7 +34,7 @@ import { agentConfigurationWasUpdatedBy } from "@app/lib/api/assistant/recent_au import { agentUserListStatus } from "@app/lib/api/assistant/user_relation"; import { compareAgentsForSort } from "@app/lib/assistant"; import type { Authenticator } from "@app/lib/auth"; -import { getPublicUploadBucket } from "@app/lib/dfs"; +import { getPublicUploadBucket } from "@app/lib/file_storage"; import { AgentConfiguration, AgentDataSourceConfiguration, diff --git a/front/lib/dfs/config.ts b/front/lib/file_storage/config.ts similarity index 100% rename from front/lib/dfs/config.ts rename to front/lib/file_storage/config.ts diff --git a/front/lib/dfs/index.ts b/front/lib/file_storage/index.ts similarity index 69% rename from front/lib/dfs/index.ts rename to front/lib/file_storage/index.ts index 6fd3bf0bec2a..cbfbf8650c07 100644 --- a/front/lib/dfs/index.ts +++ b/front/lib/file_storage/index.ts @@ -4,24 +4,18 @@ import type formidable from "formidable"; import fs from "fs"; import { pipeline } from "stream/promises"; -import config from "@app/lib/dfs/config"; +import config from "@app/lib/file_storage/config"; -type BucketKeyType = "PRIVATE_UPLOAD" | "PUBLIC_UPLOAD"; - -const storage = new Storage({ - keyFilename: config.getServiceAccount(), -}); - -const bucketKeysToBucket: Record = { - PRIVATE_UPLOAD: storage.bucket(config.getGcsPrivateUploadsBucket()), - PUBLIC_UPLOAD: storage.bucket(config.getGcsPublicUploadBucket()), -}; - -class DFS { +class FileStorage { private readonly bucket: Bucket; + private readonly storage: Storage; + + constructor(bucketKey: string) { + this.storage = new Storage({ + keyFilename: config.getServiceAccount(), + }); - constructor(bucketKey: BucketKeyType) { - this.bucket = bucketKeysToBucket[bucketKey]; + this.bucket = this.storage.bucket(bucketKey); } /** @@ -87,6 +81,8 @@ class DFS { } } -export const getPrivateUploadBucket = () => new DFS("PRIVATE_UPLOAD"); +export const getPrivateUploadBucket = () => + new FileStorage(config.getGcsPrivateUploadsBucket()); -export const getPublicUploadBucket = () => new DFS("PUBLIC_UPLOAD"); +export const getPublicUploadBucket = () => + new FileStorage(config.getGcsPublicUploadBucket()); diff --git a/front/lib/resources/content_fragment_resource.ts b/front/lib/resources/content_fragment_resource.ts index eec89e70f31b..97bf05a5baaf 100644 --- a/front/lib/resources/content_fragment_resource.ts +++ b/front/lib/resources/content_fragment_resource.ts @@ -8,7 +8,7 @@ import type { } from "sequelize"; import appConfig from "@app/lib/api/config"; -import { getPrivateUploadBucket } from "@app/lib/dfs"; +import { getPrivateUploadBucket } from "@app/lib/file_storage"; import { Message } from "@app/lib/models"; import { BaseResource } from "@app/lib/resources/base_resource"; import { ContentFragmentModel } from "@app/lib/resources/storage/models/content_fragment"; diff --git a/front/pages/api/w/[wId]/assistant/agent_configurations/avatar.ts b/front/pages/api/w/[wId]/assistant/agent_configurations/avatar.ts index 926a110d973f..4f7a67bdb419 100644 --- a/front/pages/api/w/[wId]/assistant/agent_configurations/avatar.ts +++ b/front/pages/api/w/[wId]/assistant/agent_configurations/avatar.ts @@ -1,7 +1,7 @@ import { IncomingForm } from "formidable"; import type { NextApiRequest, NextApiResponse } from "next"; -import { getPublicUploadBucket } from "@app/lib/dfs"; +import { getPublicUploadBucket } from "@app/lib/file_storage"; import { withLogging } from "@app/logger/withlogging"; export const config = { diff --git a/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/raw_content_fragment/index.ts b/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/raw_content_fragment/index.ts index 1256e7cfb4b0..39b7ae6e59f4 100644 --- a/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/raw_content_fragment/index.ts +++ b/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/[mId]/raw_content_fragment/index.ts @@ -5,7 +5,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { getConversation } from "@app/lib/api/assistant/conversation"; import { Authenticator, getSession } from "@app/lib/auth"; -import { getPrivateUploadBucket } from "@app/lib/dfs"; +import { getPrivateUploadBucket } from "@app/lib/file_storage"; import { fileAttachmentLocation } from "@app/lib/resources/content_fragment_resource"; import { apiError, withLogging } from "@app/logger/withlogging";