Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to upload files #11

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/common_nestjs_remix/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ yarn-error.log*
# Misc
.DS_Store
*.pem

uploads/*
9 changes: 7 additions & 2 deletions examples/common_nestjs_remix/apps/api/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# GENERAL
CORS_ORIGIN="https://app.guidebook.localhost"
EMAIL_ADAPTER="mailhog"

FILE_ADAPTER="local"
# DATABASE
DATABASE_URL="postgres://postgres:guidebook@localhost:5432/guidebook"

Expand All @@ -17,6 +17,11 @@ SMTP_USER=
SMTP_PASSWORD=

# AWS
AWS_REGION=
AWS_REGION=eu-central-1
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_BUCKET_NAME=test-bucket


# LOCAL UPLOADS
LOCAL_UPLOAD_FOLDER=uploads
5 changes: 5 additions & 0 deletions examples/common_nestjs_remix/apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"db:seed": "ts-node -r tsconfig-paths/register ./src/seed.ts"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.617.0",
"@aws-sdk/s3-request-presigner": "^3.617.0",
"@aws-sdk/client-ses": "^3.616.0",
"@knaadh/nestjs-drizzle-postgres": "^1.0.0",
"@nestjs/axios": "^3.0.3",
Expand All @@ -47,6 +49,7 @@
"drizzle-orm": "^0.31.2",
"drizzle-typebox": "^0.1.1",
"lodash": "^4.17.21",
"multer": "1.4.5-lts.1",
"nestjs-typebox": "3.0.0-next.8",
"nodemailer": "^6.9.14",
"passport": "^0.7.0",
Expand All @@ -63,12 +66,14 @@
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@testcontainers/localstack": "^10.10.4",
"@types/bcrypt": "^5.0.2",
"@types/cookie": "^0.6.0",
"@types/cookie-parser": "^1.4.7",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/lodash": "^4.17.6",
"@types/multer": "^1.4.11",
"@types/node": "^20.3.1",
"@types/nodemailer": "^6.4.15",
"@types/passport-jwt": "^4.0.1",
Expand Down
7 changes: 5 additions & 2 deletions examples/common_nestjs_remix/apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import { JwtAuthGuard } from "./common/guards/jwt-auth.guard";
import { EmailModule } from "./common/emails/emails.module";
import { TestConfigModule } from "./test-config/test-config.module";
import { StagingGuard } from "./common/guards/staging.guard";
import { HealthModule } from './health/health.module';
import { HealthModule } from "./health/health.module";
import { FilesModule } from "./common/files/files.module";
import localFile from "./common/configuration/local_file";

@Module({
imports: [
ConfigModule.forRoot({
load: [database, jwtConfig, emailConfig, awsConfig],
load: [database, jwtConfig, emailConfig, awsConfig, localFile],
isGlobal: true,
}),
DrizzlePostgresModule.registerAsync({
Expand Down Expand Up @@ -53,6 +55,7 @@ import { HealthModule } from './health/health.module';
EmailModule,
TestConfigModule,
HealthModule,
FilesModule,
],
controllers: [],
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const schema = Type.Object({
AWS_REGION: Type.String(),
AWS_ACCESS_KEY_ID: Type.String(),
AWS_SECRET_ACCESS_KEY: Type.String(),
BUCKET_NAME: Type.String(),
});

type AWSConfigSchema = Static<typeof schema>;
Expand All @@ -17,6 +18,7 @@ export default registerAs("aws", (): AWSConfigSchema => {
AWS_REGION: process.env.AWS_REGION,
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
BUCKET_NAME: process.env.AWS_BUCKET_NAME,
};

return validateAwsConfig(values);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { registerAs } from "@nestjs/config";
import { Static, Type } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";

const schema = Type.Object({
uploadDir: Type.Optional(Type.String()),
});

type LocalFileConfig = Static<typeof schema>;

export default registerAs("localFile", (): LocalFileConfig => {
const values = {
uploadDir: process.env.LOCAL_UPLOAD_FOLDER,
};

return Value.Decode(schema, values);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export abstract class FilesAdapter {
abstract uploadFile(
path: string,
file: Express.Multer.File,
): Promise<{ path: string }>;

abstract deleteFile(id: string): Promise<void>;

abstract getFileUrl(id: string): Promise<{ url: string }>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./s3.adapter";
export * from "./local.adapter";
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Injectable, InternalServerErrorException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { v7 as uuid } from "uuid";
import * as fs from "node:fs";
import * as path from "node:path";
import { FilesAdapter } from "./files.adapter";

@Injectable()
export class LocalFilesAdapter extends FilesAdapter {
uploadsDir = path.join(
__dirname,
"..",
"..",
"..",
"..",
"..",
this.configService.getOrThrow<string>("localFile.uploadDir"),
);
constructor(private configService: ConfigService) {
super();
}

async uploadFile(directory: string, file: Express.Multer.File) {
try {
const key = `${directory}/${uuid()}-${file.originalname}`;
fs.writeFile(path.join(this.uploadsDir, key), file.buffer, (err) => {
if (err) {
throw new InternalServerErrorException("Failed to upload file");
}
});

return { path: key };
} catch (error) {
throw new InternalServerErrorException("Failed to upload file");
}
}

async getFileUrl(path: string): Promise<{ url: string }> {
try {
const url = `https://storage.guidebook.localhost/${path}`;
return { url: url };
} catch (error) {
throw new InternalServerErrorException("Failed to get file");
}
}

async deleteFile(url: string): Promise<void> {
try {
fs.unlinkSync(path.join(this.uploadsDir, url));
} catch (error) {
throw new InternalServerErrorException("Failed to delete file");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Injectable, InternalServerErrorException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import {
GetObjectCommand,
PutObjectCommand,
S3Client,
} from "@aws-sdk/client-s3";
import { v7 as uuid } from "uuid";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { FilesAdapter } from "./files.adapter";

@Injectable()
export class S3FileAdapter extends FilesAdapter {
private client: S3Client;
private bucketName = this.configService.getOrThrow<string>("aws.BUCKET_NAME");

constructor(private configService: ConfigService) {
super();
const region = this.configService.getOrThrow<string>("aws.AWS_REGION");
const endpoint = `https://s3.${region}.amazonaws.com`;
this.client = new S3Client({
region,
endpoint,
credentials: {
accessKeyId: this.configService.getOrThrow<string>(
"aws.AWS_ACCESS_KEY_ID",
),
secretAccessKey: this.configService.getOrThrow<string>(
"aws.AWS_SECRET_ACCESS_KEY",
),
},
forcePathStyle: true,
});
}

async uploadFile(path: string, file: Express.Multer.File) {
try {
const key = `${path}/${uuid()}-${file.originalname}`;
const command = new PutObjectCommand({
Bucket: this.bucketName,
Key: key,
Body: file.buffer,
ContentType: file.mimetype,
Metadata: {
originalname: file.originalname,
},
});

const uploadResult = await this.client.send(command);

if (
!uploadResult.$metadata.httpStatusCode ||
uploadResult.$metadata.httpStatusCode !== 200
) {
throw new InternalServerErrorException("Failed to upload file");
}

return { path: key };
} catch (error) {
throw new InternalServerErrorException("Failed to upload file");
}
}

async getFileUrl(path: string): Promise<{ url: string }> {
try {
return this.getPublicUrl(path);
} catch (error) {
throw new InternalServerErrorException("Failed to get file");
}
}

async deleteFile(path: string): Promise<void> {
try {
const command = new GetObjectCommand({
Bucket: this.bucketName,
Key: path,
});
await this.client.send(command);
} catch (error) {
throw new InternalServerErrorException("Failed to delete file");
}
}

private async getPublicUrl(key: string) {
try {
const command = new GetObjectCommand({
Bucket: this.bucketName,
Key: key,
});

const url = await getSignedUrl(this.client, command, {
expiresIn: 60 * 60 * 24,
});

return { url };
} catch (error) {
throw new InternalServerErrorException("Failed to get file");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from "@nestjs/common";
import { S3FileAdapter } from "../adapters/s3.adapter";
import { match, P } from "ts-pattern";
import { ConfigService } from "@nestjs/config";
import { FilesAdapter } from "../adapters/files.adapter";
import { LocalFilesAdapter } from "../adapters";
import { ModuleRef } from "@nestjs/core";

type AdapterType = "local" | "s3";

@Injectable()
export class FileAdapterFactory {
constructor(
private moduleRef: ModuleRef,
private configService: ConfigService,
) {}

async createAdapter(): Promise<FilesAdapter> {
const adapterType = this.configService.get<AdapterType>("FILE_ADAPTER");
const adapter = match(adapterType)
.with("local", () => LocalFilesAdapter)
.with("s3", () => S3FileAdapter)
.with(P.nullish, () => {
throw new Error("FILE_ADAPTER is not defined in configuration");
})
.otherwise((type) => {
throw new Error(`Unknown file adapter type: ${type}`);
});

return await this.moduleRef.create<FilesAdapter>(adapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from "@nestjs/common";
import { FilesAdapter } from "./adapters/files.adapter";

@Injectable()
export class FileService {
constructor(private fileAdapter: FilesAdapter) {}

async uploadFile(
directory: string,
file: Express.Multer.File,
): Promise<{ path: string }> {
return await this.fileAdapter.uploadFile(directory, file);
}

async deleteFile(key: string): Promise<void> {
return await this.fileAdapter.deleteFile(key);
}

async getFileUrl(key: string): Promise<{ url: string }> {
return await this.fileAdapter.getFileUrl(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Module } from "@nestjs/common";
import { FileService } from "./file.service";
import { ConfigModule } from "@nestjs/config";
import { FileAdapterFactory } from "./factory/file-adapters.factory";
import { FilesAdapter } from "./adapters/files.adapter";

@Module({
imports: [ConfigModule],
providers: [
FileService,
FileAdapterFactory,
{
provide: FilesAdapter,
useFactory: (factory: FileAdapterFactory) => factory.createAdapter(),
inject: [FileAdapterFactory],
},
],
exports: [FileService],
})
export class FilesModule {}
Loading