Skip to content

Commit

Permalink
Add missings data from the downloaded zip #129
Browse files Browse the repository at this point in the history
  • Loading branch information
haimkastner committed May 3, 2020
1 parent 1dc1ac7 commit ff7a8ee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
9 changes: 9 additions & 0 deletions backend/package-lock.json

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

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"yeelight-awesome": "^1.0.12"
},
"devDependencies": {
"@types/adm-zip": "^0.4.33",
"@types/bcryptjs": "^2.4.2",
"@types/body-parser": "^1.17.0",
"@types/chai": "^4.1.7",
Expand Down
17 changes: 15 additions & 2 deletions backend/src/controllers/backupController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import * as express from 'express';
import * as fse from 'fs-extra';
import * as path from 'path';
import { Controller, Get, Request, Response, Route, Security } from 'tsoa';
import { DevicesBlSingleton } from '../business-layer/devicesBl';
import { MinionsBlSingleton } from '../business-layer/minionsBl';
import { OperationsBlSingleton } from '../business-layer/operationsBl';
import { TimelineBlSingleton } from '../business-layer/timelineBl';
import { TimingsBlSingleton } from '../business-layer/timingsBl';
import { UsersBlSingleton } from '../business-layer/usersBl';
import { ErrorResponse, UpdateResults, VersionInfo, VersionUpdateStatus } from '../models/sharedInterfaces';
import { CACHE_DIRECTORY } from '../modules/brandModuleBase';
import { logger } from '../utilities/logger';

@Route('backup')
Expand All @@ -22,6 +25,10 @@ export class BackupController extends Controller {
public async getSettingsBackup(@Request() request: express.Request) {
const zip = new AdmZip();

zip.addZipComment(`Generated by casanet server, in ${new Date().toLocaleDateString()}`);

zip.addLocalFolder(CACHE_DIRECTORY, 'cache');

const dataToZip: Array<{ name: string, data: any }> = [{
name: 'minions',
data: await MinionsBlSingleton.getMinions()
Expand All @@ -34,18 +41,24 @@ export class BackupController extends Controller {
}, {
name: 'timeline',
data: await TimelineBlSingleton.getTimeline()
}, {
name: 'devices',
data: await DevicesBlSingleton.getDevices()
}, {
name: 'users',
data: await UsersBlSingleton.getUsers()
}]

for (const data of dataToZip) {
const dataAsString = JSON.stringify(data.data, null, 2);
zip.addFile(`${data.name}.json`, Buffer.from(dataAsString), data.name);
}

const res = request.res as express.Response;
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Disposition', `attachment; filename=casanet_backup_${new Date().toLocaleDateString()}.zip`);

res.end(zip.toBuffer());
logger.info(`[backup ctrl] The backup sent successfully to the admin ${request.user}`);
logger.info(`[backup ctrl] The backup sent successfully`);
}
}
5 changes: 3 additions & 2 deletions backend/src/modules/brandModuleBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { AcCommands, CommandsSet, RollerCommands, ToggleCommands } from '../mode
import { DeviceKind, ErrorResponse, Minion, MinionStatus } from '../models/sharedInterfaces';
import { logger } from '../utilities/logger';

export const CACHE_DIRECTORY = path.join('./data/', Configuration.runningMode, '/cache/');

/**
* Any smart devices brand communication module needs to inherit..
*/
Expand All @@ -15,9 +17,8 @@ export abstract class BrandModuleBase {
* Cache file pull path.
*/
private get cacheFilePath(): string {
return `${path.join(BrandModuleBase.CACHE_DIRACTORY, this.brandName)}.json`;
return `${path.join(CACHE_DIRECTORY, this.brandName)}.json`;
}
public static readonly CACHE_DIRACTORY = path.join('./data/', Configuration.runningMode, '/cache/');

/**
* Brand name, should be unique in system.
Expand Down

0 comments on commit ff7a8ee

Please sign in to comment.