-
Notifications
You must be signed in to change notification settings - Fork 32
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
10 changed files
with
1,341 additions
and
267 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { Controller, Get, HttpException, HttpStatus, Param, Res, Response, StreamableFile } from '@nestjs/common'; | ||
import { contentType } from 'mime-types'; | ||
import { VfsService } from './vfs.service'; | ||
import * as path from 'node:path'; | ||
import { ExpressAdapter } from '@nestjs/platform-express'; | ||
import { HttpAdapterHost } from '@nestjs/core'; | ||
|
||
@ApiTags('VFS') | ||
@Controller('api/v1/remote-app/vfs-proxy') | ||
export class VfsController { | ||
constructor( | ||
private readonly vfsService: VfsService, | ||
private readonly httpAdapterHost: HttpAdapterHost<ExpressAdapter>, | ||
) {} | ||
|
||
@Get('/:filePath(*)') | ||
async getFile(@Param('filePath') filePath: string, @Res({ passthrough: true }) res: Response) { | ||
if (this.vfsService.requestFile === null) { | ||
throw new HttpException( | ||
'The VFS service is not ready to serve files at this moment. Is an airplane client connected to the remote bridge?', | ||
HttpStatus.SERVICE_UNAVAILABLE, | ||
); | ||
} | ||
|
||
const ext = path.extname(filePath); | ||
|
||
if (ext === '') { | ||
throw new HttpException('Malformed request: Could not find a file extension', HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
const contentTypeHeader = contentType(ext); | ||
|
||
if (contentTypeHeader === false) { | ||
throw new HttpException('Malformed request: Could not find a valid file extension', HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
const data = await this.vfsService.requestFile(filePath); | ||
|
||
this.httpAdapterHost.httpAdapter.setHeader(res, 'Content-Type', contentTypeHeader); | ||
|
||
return new StreamableFile(data); | ||
} | ||
} |
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,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { VfsController } from './vfs.controller'; | ||
import { VfsService } from './vfs.service'; | ||
|
||
@Module({ | ||
controllers: [VfsController], | ||
providers: [VfsService], | ||
exports: [VfsService], | ||
}) | ||
export class VfsModule {} |
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,8 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
export type RequestFileFunction = (filePath: string) => Promise<Buffer>; | ||
|
||
@Injectable() | ||
export class VfsService { | ||
public requestFile: RequestFileFunction | null = null; | ||
} |
Oops, something went wrong.