-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
77 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
examples/common_nestjs_remix/apps/api/src/common/files/factory/file-adapters.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/common_nestjs_remix/apps/api/src/common/files/file.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/common_nestjs_remix/apps/api/src/common/files/files.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
32 changes: 0 additions & 32 deletions
32
examples/common_nestjs_remix/apps/api/src/files/file.service.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
examples/common_nestjs_remix/apps/api/src/files/files.module.ts
This file was deleted.
Oops, something went wrong.