Skip to content

Commit

Permalink
Avoid requiring env variable when importing file (#4573)
Browse files Browse the repository at this point in the history
* Avoid requiring env variable when importing file

* s/dfs/file_storage
  • Loading branch information
flvndvd committed May 26, 2024
1 parent c3f2d12 commit a0d6dc0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion front/lib/api/assistant/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
File renamed without changes.
30 changes: 13 additions & 17 deletions front/lib/dfs/index.ts → front/lib/file_storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BucketKeyType, Bucket> = {
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);
}

/**
Expand Down Expand Up @@ -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());
2 changes: 1 addition & 1 deletion front/lib/resources/content_fragment_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit a0d6dc0

Please sign in to comment.