Skip to content

Commit 0c365d6

Browse files
committed
updated logging
1 parent 12ad453 commit 0c365d6

File tree

7 files changed

+38
-30
lines changed

7 files changed

+38
-30
lines changed

src/lib/Config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { readFileSync } from 'fs';
22
import { resolve } from 'path';
33
import { parse } from 'toml-patch';
44
import { ConnectionOptions } from 'typeorm';
5-
import { WebhookHelper, WebhookType } from './Webhooks';
5+
import { Webhooks, WebhookType } from './Webhooks';
66

77
export interface Config {
88
database: ConnectionOptions;
@@ -79,7 +79,7 @@ export class Configuration {
7979
const data = readFileSync(resolve(process.cwd(), 'Zipline.toml'), 'utf8');
8080
const parsed = parse(data);
8181
if (parsed.webhooks)
82-
parsed.webhooks.events = WebhookHelper.convert(parsed.webhooks.events);
82+
parsed.webhooks.events = Webhooks.convert(parsed.webhooks.events);
8383
return parsed;
8484
} catch (e) {
8585
return null;

src/lib/Webhooks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export type WebhookSendText =
4646
| 'user_delete'
4747
| 'user_edit';
4848

49-
export class WebhookHelper {
49+
export class Webhooks {
5050
public static convert(strings: WebhookSendText[]) {
5151
return strings.map(x => WebhookType[x.toUpperCase()]);
5252
}
@@ -81,11 +81,11 @@ export class WebhookHelper {
8181
},
8282
body: JSON.stringify({
8383
username: config.webhooks.username,
84-
content: WebhookHelper.parseContent(content, data)
84+
content: Webhooks.parseContent(content, data)
8585
})
8686
});
8787
} catch (e) {
88-
Console.logger(WebhookHelper).error(e);
88+
Console.logger(Webhooks).error(e);
8989
}
9090
}
9191
}

src/lib/api/controllers/ImagesController.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { LoginError } from '../APIErrors';
1414
import { Configuration, ConfigWebhooks } from '../../Config';
1515
import { Console } from '../../logger';
1616
import { readBaseCookie } from '../../Util';
17-
import { WebhookHelper, WebhookType } from '../../Webhooks';
17+
import { Webhooks, WebhookType } from '../../Webhooks';
1818

1919
const config = Configuration.readConfig();
2020

@@ -24,7 +24,7 @@ export class ImagesController {
2424
private instance!: FastifyInstance;
2525

2626
private images: Repository<Image> = this.instance.orm.getRepository(Image);
27-
private webhooks: ConfigWebhooks = WebhookHelper.conf(config);
27+
private webhooks: ConfigWebhooks = Webhooks.conf(config);
2828

2929
@GET('/')
3030
async allImages(req: FastifyRequest, reply: FastifyReply) {
@@ -59,17 +59,24 @@ export class ImagesController {
5959
id: req.params.id
6060
});
6161

62-
const dir = config.uploader.directory ? config.uploader.directory : 'uploads';
63-
const path = join(dir.charAt(0) == '/' ? dir : join(process.cwd(), dir), image.file);
62+
const dir = config.uploader.directory
63+
? config.uploader.directory
64+
: 'uploads';
65+
const path = join(
66+
dir.charAt(0) == '/' ? dir : join(process.cwd(), dir),
67+
image.file
68+
);
6469

6570
try {
6671
unlinkSync(path);
6772

6873
Console.logger(Image).info(`image ${image.id} was deleted`);
6974
if (this.webhooks.events.includes(WebhookType.DELETE_IMAGE))
70-
WebhookHelper.sendWebhook(this.webhooks.upload.content, {
75+
Webhooks.sendWebhook(this.webhooks.upload.content, {
7176
image,
72-
host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${config.uploader.route}/`
77+
host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${
78+
config.uploader.route
79+
}/`
7380
});
7481

7582
return reply.send(image);

src/lib/api/controllers/MultiFactorController.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { toDataURL } from 'qrcode';
1313
import { checkPassword, createBaseCookie, readBaseCookie } from '../../Util';
1414
import { LoginError, MissingBodyData, UserNotFoundError } from '../APIErrors';
1515
import { Console } from '../../logger';
16-
import { WebhookType, WebhookHelper } from '../../Webhooks';
16+
import { WebhookType, Webhooks } from '../../Webhooks';
1717
import { Configuration, ConfigWebhooks } from '../../Config';
1818

1919
const config = Configuration.readConfig();
@@ -25,7 +25,7 @@ export class MultiFactorController {
2525

2626
private users: Repository<User> = this.instance.orm.getRepository(User);
2727
private logger: Console = Console.logger(User);
28-
private webhooks: ConfigWebhooks = WebhookHelper.conf(config);
28+
private webhooks: ConfigWebhooks = Webhooks.conf(config);
2929

3030
@GET('/qrcode')
3131
async qrcode(req: FastifyRequest, reply: FastifyReply) {
@@ -73,6 +73,7 @@ export class MultiFactorController {
7373

7474
this.users.save(user);
7575

76+
this.logger.info(`disabled mfa ${user.username} (${user.id})`);
7677
reply.send({ disabled: true });
7778
}
7879

@@ -98,7 +99,7 @@ export class MultiFactorController {
9899
throw new UserNotFoundError(`User "${req.body.username}" was not found.`);
99100
if (!checkPassword(req.body.password, user.password)) {
100101
this.logger.error(
101-
`${user.username} (${user.id}) tried to login but failed`
102+
`${user.username} (${user.id}) tried to login but failed with mfa`
102103
);
103104
throw new LoginError('Wrong credentials!');
104105
}
@@ -116,9 +117,9 @@ export class MultiFactorController {
116117
maxAge: 1036800000
117118
});
118119

119-
this.logger.info(`${user.username} (${user.id}) logged in`);
120+
this.logger.info(`${user.username} (${user.id}) logged in with mfa`);
120121
if (this.webhooks.events.includes(WebhookType.LOGIN))
121-
WebhookHelper.sendWebhook(this.webhooks.login.content, {
122+
Webhooks.sendWebhook(this.webhooks.login.content, {
122123
user
123124
});
124125

src/lib/api/controllers/RootController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { AuthError } from '../APIErrors';
1818
import { Configuration, ConfigWebhooks } from '../../Config';
1919
import { createRandomId, getFirst } from '../../Util';
2020
import { Console } from '../../logger';
21-
import { WebhookHelper, WebhookType } from '../../Webhooks';
21+
import { Webhooks, WebhookType } from '../../Webhooks';
2222
const pump = promisify(pipeline);
2323

2424
const config = Configuration.readConfig();
@@ -40,7 +40,7 @@ export class RootController {
4040

4141
private users: Repository<User> = this.instance.orm.getRepository(User);
4242
private images: Repository<Image> = this.instance.orm.getRepository(Image);
43-
private webhooks: ConfigWebhooks = WebhookHelper.conf(config);
43+
private webhooks: ConfigWebhooks = Webhooks.conf(config);
4444
private logger: Console = Console.logger(Image);
4545

4646
@GET('/first')
@@ -149,7 +149,7 @@ export class RootController {
149149
}/${fileName}.${ext}`;
150150

151151
if (this.webhooks.events.includes(WebhookType.UPLOAD))
152-
WebhookHelper.sendWebhook(this.webhooks.upload.content, {
152+
Webhooks.sendWebhook(this.webhooks.upload.content, {
153153
image,
154154
host
155155
});

src/lib/api/controllers/URLSController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { LoginError } from '../APIErrors';
1414
import { Configuration, ConfigWebhooks } from '../../Config';
1515
import { Console } from '../../logger';
1616
import { createRandomId, readBaseCookie } from '../../Util';
17-
import { WebhookType, WebhookHelper } from '../../Webhooks';
17+
import { WebhookType, Webhooks } from '../../Webhooks';
1818

1919
const config = Configuration.readConfig();
2020

@@ -26,7 +26,7 @@ export class URLSController {
2626
private urls: Repository<URL> = this.instance.orm.getRepository(URL);
2727
private users: Repository<User> = this.instance.orm.getRepository(User);
2828
private logger: Console = Console.logger(URL);
29-
private webhooks: ConfigWebhooks = WebhookHelper.conf(config);
29+
private webhooks: ConfigWebhooks = Webhooks.conf(config);
3030

3131
@GET('/')
3232
async allURLS(req: FastifyRequest, reply: FastifyReply) {
@@ -64,7 +64,7 @@ export class URLSController {
6464

6565
this.logger.info(`url ${url.id} was deleted`);
6666
if (this.webhooks.events.includes(WebhookType.DELETE_URL))
67-
WebhookHelper.sendWebhook(this.webhooks.delete_url.content, {
67+
Webhooks.sendWebhook(this.webhooks.delete_url.content, {
6868
url,
6969
host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${config.urls.route}/`
7070
});
@@ -105,7 +105,7 @@ export class URLSController {
105105

106106
this.logger.info(`saved url ${url.id}`);
107107
if (this.webhooks.events.includes(WebhookType.SHORTEN))
108-
WebhookHelper.sendWebhook(this.webhooks.shorten.content, {
108+
Webhooks.sendWebhook(this.webhooks.shorten.content, {
109109
url,
110110
host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${config.urls.route}/`
111111
});

src/lib/api/controllers/UserController.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
getFirst,
2828
readBaseCookie
2929
} from '../../Util';
30-
import { WebhookType, WebhookHelper } from '../../Webhooks';
30+
import { WebhookType, Webhooks } from '../../Webhooks';
3131

3232
const config = Configuration.readConfig();
3333

@@ -38,7 +38,7 @@ export class UserController {
3838

3939
private users: Repository<User> = this.instance.orm.getRepository(User);
4040
private logger: Console = Console.logger(User);
41-
private webhooks: ConfigWebhooks = WebhookHelper.conf(config);
41+
private webhooks: ConfigWebhooks = Webhooks.conf(config);
4242

4343
@GET('/login-status')
4444
async loginStatus(req: FastifyRequest, reply: FastifyReply) {
@@ -86,7 +86,7 @@ export class UserController {
8686

8787
this.logger.info(`saved ${user.username} (${user.id})`);
8888
if (this.webhooks.events.includes(WebhookType.USER_EDIT))
89-
WebhookHelper.sendWebhook(this.webhooks.user_edit.content, {
89+
Webhooks.sendWebhook(this.webhooks.user_edit.content, {
9090
user
9191
});
9292

@@ -156,7 +156,7 @@ export class UserController {
156156

157157
this.logger.info(`${user.username} (${user.id}) logged in`);
158158
if (this.webhooks.events.includes(WebhookType.LOGIN))
159-
WebhookHelper.sendWebhook(this.webhooks.login.content, {
159+
Webhooks.sendWebhook(this.webhooks.login.content, {
160160
user
161161
});
162162

@@ -194,7 +194,7 @@ export class UserController {
194194

195195
this.logger.info(`reset token ${user.username} (${user.id})`);
196196
if (this.webhooks.events.includes(WebhookType.TOKEN_RESET))
197-
WebhookHelper.sendWebhook(this.webhooks.token_reset.content, {
197+
Webhooks.sendWebhook(this.webhooks.token_reset.content, {
198198
user
199199
});
200200

@@ -228,7 +228,7 @@ export class UserController {
228228
);
229229
this.logger.info(`created user ${user.username} (${user.id})`);
230230
if (this.webhooks.events.includes(WebhookType.CREATE_USER))
231-
WebhookHelper.sendWebhook(this.webhooks.create_user.content, {
231+
Webhooks.sendWebhook(this.webhooks.create_user.content, {
232232
user
233233
});
234234

@@ -272,7 +272,7 @@ export class UserController {
272272

273273
this.logger.info(`deleted ${existing.username} (${existing.id})`);
274274
if (this.webhooks.events.includes(WebhookType.USER_DELETE))
275-
WebhookHelper.sendWebhook(this.webhooks.user_delete.content, {
275+
Webhooks.sendWebhook(this.webhooks.user_delete.content, {
276276
user: existing
277277
});
278278

0 commit comments

Comments
 (0)