Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
feat(server): upgrade to v1.3.0
Browse files Browse the repository at this point in the history
- install command keep exists secret
- http actions optimize
  • Loading branch information
iamcco committed Nov 12, 2021
1 parent c7bf744 commit 02150d0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hello-dt",
"version": "1.2.7",
"version": "1.3.0",
"main": "index.js",
"repository": "git@github.com:iamcco/hello-dt.git",
"author": "iamcco <ooiss@qq.com>",
Expand Down
18 changes: 15 additions & 3 deletions server/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { exec } from 'child_process';
import qrcode from 'qrcode-terminal';
import { program } from 'commander';
import { writeFileSync } from 'fs';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join, dirname } from 'path';
import { nanoid } from 'nanoid';
import packageInfo from '../package.json';
Expand Down Expand Up @@ -41,9 +41,21 @@ program.parse();

const options = program.opts();

const servicePath = '/etc/systemd/system/hello-dt.service';

if (options.install) {
try {
writeFileSync('/etc/systemd/system/hello-dt.service', serviceTemplate);
if (existsSync(servicePath)) {
const lines = readFileSync(servicePath).toString().split('\n');
lines.some((line) => {
if (line.startsWith('ExecStart')) {
serviceTemplate.replace(secret, line.trim().split(' ').pop()!);
return true;
}
return false;
});
}
writeFileSync(servicePath, serviceTemplate);
exec('systemctl daemon-reload');
exec('systemctl status hello-dt.service', (err) => {
if (!err) {
Expand All @@ -59,7 +71,7 @@ if (options.install) {
}
} else if (options.uninstall) {
exec('systemctl stop hello-dt.service');
exec('rm /etc/systemd/system/hello-dt.service', (err, _stdout, stderr) => {
exec(`rm ${servicePath}`, (err, _stdout, stderr) => {
if (err) {
console.error(err);
console.log(stderr);
Expand Down
44 changes: 19 additions & 25 deletions server/src/http/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ use((req, res, next) => (params) => {
return res.end();
}

const formParser = new multiparty.Form({
maxFieldsSize: 20971520,
});
if (req.headers['content-type'] !== undefined) {
const formParser = new multiparty.Form();
formParser.parse(req, (err, fields, files) => {
if (err) {
console.error(err);
Expand All @@ -90,47 +88,43 @@ use((req, res, next) => (params) => {

use((_req, res, next) => (params) => {
if (params?.action === 'brightness') {
const access = createWriteStream('/sys/class/backlight/backlight@0/brightness');
access.write(params?.value || 1);
access.close();
return res.end();
if (params?.method === 'post') {
const access = createWriteStream('/sys/class/backlight/backlight@0/brightness');
access.write(params?.value || 1);
access.close();
return res.end();
} else if (params?.method === 'get') {
const level = readFileSync('/sys/class/backlight/backlight@0/actual_brightness').toString().trim();
res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify({ level: parseFloat(level) }));
}
}
next(params);
});

use((_req, res, next) => (params) => {
if (params?.action === 'shutdown') {
if (params?.method === 'post' && params?.action === 'shutdown') {
exec('shutdown now');
return res.end();
}
next(params);
});

use((_req, res, next) => (params) => {
if (params?.action === 'reboot') {
if (params?.method === 'post' && params?.action === 'reboot') {
exec('reboot');
return res.end();
}
next(params);
});

use((_req, res, next) => (params) => {
if (params?.method === 'get' && params?.action === 'getBrightness') {
const level = readFileSync('/sys/class/backlight/backlight@0/actual_brightness').toString().trim();
res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify({ level: parseFloat(level) }));
}
next(params);
});

use((_req, res, next) => (params) => {
const targets = params?.files?.target || [];
if (params?.action === 'print' && targets[0] !== undefined && params?.fields?.size[0] !== undefined) {
const target = `${targets[0].path}.png`;
renameSync(targets[0].path, target);
exec(
`lp -d devterm_printer -o media=Custom.${params.fields.size[0]}mm -o scaling=100 -o print-quality=5 ${target}`,
);
const target = (params?.files?.target || [])[0];
const size = (params?.fields?.size || [])[0];
if (params?.method === 'post' && params?.action === 'print' && target !== undefined && size !== undefined) {
const targetName = '/tmp/hello-dt-print-target.png';
renameSync(target.path, targetName);
exec(`lp -d devterm_printer -o media=Custom.${size}mm -o scaling=100 -o print-quality=5 ${targetName}`);
return res.end();
}
next(params);
Expand Down

0 comments on commit 02150d0

Please sign in to comment.