Skip to content

Commit

Permalink
mimic hue dump file download for deconz (#1620)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwp91 authored Nov 4, 2023
1 parent 0786274 commit a49d68b
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 70 deletions.
81 changes: 45 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"bash-color": "^0.0.4",
"buffer-shims": "^1.0.0",
"concurrently": "^8.2.2",
"eslint": "^8.52.0",
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-jest": "^27.6.0",
Expand Down Expand Up @@ -153,4 +153,4 @@
"smart home",
"hb-service"
]
}
}
2 changes: 2 additions & 0 deletions src/modules/custom-plugins/custom-plugins.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { HomebridgeDeconzModule } from './homebridge-deconz/homebridge-deconz.module';
import { HomebridgeHueModule } from './homebridge-hue/homebridge-hue.module';
import { PluginsSettingsUiModule } from './plugins-settings-ui/plugins-settings-ui.module';

@Module({
imports: [
HomebridgeDeconzModule,
HomebridgeHueModule,
PluginsSettingsUiModule,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Controller, UseGuards, Get, Header, StreamableFile } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AdminGuard } from '../../../core/auth/guards/admin.guard';
import { HomebridgeDeconzService } from './homebridge-deconz.service';

@ApiTags('Plugins')
@ApiBearerAuth()
@UseGuards(AuthGuard())
@Controller('plugins/custom-plugins/homebridge-deconz')
export class HomebridgeDeconzController {

constructor(
private homebridgeDeconzService: HomebridgeDeconzService,
) { }

@UseGuards(AdminGuard)
@Get('/dump-file')
@Header('Content-disposition', 'attachment; filename=homebridge-deconz.json.gz')
@Header('Content-Type', 'application/json+gzip')
async exchangeCredentials(): Promise<StreamableFile> {
return this.homebridgeDeconzService.streamDumpFile();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { LoggerModule } from '../../../core/logger/logger.module';
import { ConfigModule } from '../../../core/config/config.module';
import { HomebridgeDeconzController } from './homebridge-deconz.controller';
import { HomebridgeDeconzService } from './homebridge-deconz.service';

@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
ConfigModule,
LoggerModule,
],
providers: [
HomebridgeDeconzService,
],
exports: [
],
controllers: [
HomebridgeDeconzController,
],
})
export class HomebridgeDeconzModule { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as path from 'path';
import * as fs from 'fs-extra';
import { Injectable, NotFoundException, StreamableFile } from '@nestjs/common';
import { ConfigService } from '../../../core/config/config.service';

@Injectable()
export class HomebridgeDeconzService {
constructor(
private configService: ConfigService,
) { }

async streamDumpFile(): Promise<StreamableFile> {
const dumpPath = path.resolve(this.configService.storagePath, 'homebridge-deconz.json.gz');

// check file exists
if (!await fs.pathExists(dumpPath)) {
throw new NotFoundException();
}

// stream file to client
return new StreamableFile(fs.createReadStream(dumpPath));
}
}
28 changes: 28 additions & 0 deletions test/e2e/custom-plugins.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ describe('CustomPluginsController (e2e)', () => {
expect(res.statusCode).toBe(404);
});

it('GET /plugins/custom-plugins/homebridge-deconz/dump-file (dump file exists)', async () => {
await fs.writeJson(path.resolve(process.env.UIX_STORAGE_PATH, 'homebridge-deconz.json.gz'), {});

const res = await app.inject({
method: 'GET',
path: '/plugins/custom-plugins/homebridge-deconz/dump-file',
headers: {
authorization,
},
});

expect(res.statusCode).toBe(200);
});

it('GET /plugins/custom-plugins/homebridge-deconz/dump-file (dump file missing)', async () => {
await fs.remove(path.resolve(process.env.UIX_STORAGE_PATH, 'homebridge-deconz.json.gz'));

const res = await app.inject({
method: 'GET',
path: '/plugins/custom-plugins/homebridge-deconz/dump-file',
headers: {
authorization,
},
});

expect(res.statusCode).toBe(404);
});

afterAll(async () => {
await app.close();
});
Expand Down
Loading

0 comments on commit a49d68b

Please sign in to comment.