Skip to content

Commit e48717c

Browse files
committed
Merge branch 'develop'
2 parents 14d5d96 + 7987547 commit e48717c

File tree

7 files changed

+325
-184
lines changed

7 files changed

+325
-184
lines changed

server/src/auth/auth.module.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@ export class AuthModule {
9696
useFactory: (configService: ConfigService) =>
9797
configService.getOrThrow<string>('JWT_REFRESH_EXPIRES_IN'),
9898
},
99-
{
100-
inject: [ConfigService],
101-
provide: 'WHITELISTED_USERS',
102-
useFactory: (configService: ConfigService) =>
103-
configService.getOrThrow<string>('WHITELISTED_USERS'),
104-
},
10599
{
106100
inject: [ConfigService],
107101
provide: 'MAGIC_LINK_SECRET',

server/src/auth/auth.service.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ export class AuthService {
3434
private readonly JWT_REFRESH_SECRET: string,
3535
@Inject('JWT_REFRESH_EXPIRES_IN')
3636
private readonly JWT_REFRESH_EXPIRES_IN: string,
37-
@Inject('WHITELISTED_USERS')
38-
private readonly WHITELISTED_USERS: string,
3937
@Inject('APP_DOMAIN')
4038
private readonly APP_DOMAIN?: string,
4139
) {}
@@ -83,10 +81,6 @@ export class AuthService {
8381
profileImage: user.photos[0].value,
8482
};
8583

86-
if (!(await this.verifyWhitelist(profile.username))) {
87-
return res.redirect(this.FRONTEND_URL + '/login');
88-
}
89-
9084
// verify if user exists
9185
const user_registered = await this.verifyAndGetUser(profile);
9286

@@ -123,23 +117,6 @@ export class AuthService {
123117
return user_registered;
124118
}
125119

126-
private async verifyWhitelist(username: string) {
127-
const whitelist = this.WHITELISTED_USERS;
128-
129-
if (whitelist.length === 0) {
130-
return true;
131-
}
132-
133-
if (whitelist.includes(username.toLowerCase())) {
134-
this.logger.log(`User ${username} is whitelisted; approving login`);
135-
return true;
136-
}
137-
138-
this.logger.log(`User ${username} is not whitelisted; rejecting login`);
139-
140-
return false;
141-
}
142-
143120
public async githubLogin(req: Request, res: Response) {
144121
const user = req.user as GithubAccessToken;
145122
const { profile } = user;
@@ -156,10 +133,6 @@ export class AuthService {
156133

157134
const email = response.data.filter((email) => email.primary)[0].email;
158135

159-
if (!(await this.verifyWhitelist(profile.username))) {
160-
return res.redirect(this.FRONTEND_URL + '/login');
161-
}
162-
163136
const user_registered = await this.verifyAndGetUser({
164137
username: profile.username,
165138
email: email,
@@ -180,10 +153,6 @@ export class AuthService {
180153
profileImage: profilePictureUrl,
181154
};
182155

183-
if (!(await this.verifyWhitelist(profile.username))) {
184-
return res.redirect(this.FRONTEND_URL + '/login');
185-
}
186-
187156
// verify if user exists
188157
const user_registered = await this.verifyAndGetUser(profile);
189158

shared/features/thumbnail/canvasFactory.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,7 @@ import type Path from 'path';
33

44
import type NapiRs from '@napi-rs/canvas';
55

6-
/*
7-
import type {
8-
Canvas as NapiCanvas,
9-
Image as NapiImage,
10-
GlobalFonts as NapiGlobalFonts,
11-
} from '@napi-rs/canvas';
12-
*/
13-
14-
export interface CanvasUtils {
15-
createCanvas(width: number, height: number): any;
16-
loadImage(src: string): Promise<any>;
17-
getPath(filename: string): string | URL;
18-
useFont(): void;
19-
saveToImage(canvas: HTMLCanvasElement | NapiRs.Canvas): Promise<Uint8Array>;
20-
noteBlockImage: Promise<any> | any;
21-
DrawingCanvas: any;
22-
RenderedImage: any;
23-
}
6+
import { CanvasUtils } from './types';
247

258
let canvasUtils: CanvasUtils;
269

@@ -39,24 +22,24 @@ if (typeof document === 'undefined') {
3922
const workingDir = process.cwd();
4023
const fullPath = path.join(workingDir, filename.split('/').join(path.sep));
4124

42-
return 'file://' + fullPath;
25+
return fullPath;
4326
};
4427

4528
const saveToImage = (canvas: NapiRs.Canvas) => canvas.encode('png');
4629

4730
const useFont = () => {
48-
GlobalFonts.registerFromPath(
49-
getPath('assets/fonts/Lato-Regular.ttf').toString(),
50-
'Lato',
51-
);
31+
const path = getPath('assets/fonts/Lato-Regular.ttf').toString();
32+
console.log('Font path: ', path);
33+
34+
GlobalFonts.registerFromPath(path, 'Lato');
5235
};
5336

5437
let noteBlockImage: Promise<any>;
5538

5639
try {
57-
noteBlockImage = nodeLoadImage(
58-
new URL(getPath('assets/img/note-block-grayscale.png')),
59-
);
40+
const path = getPath('assets/img/note-block-grayscale.png');
41+
42+
noteBlockImage = nodeLoadImage(path);
6043
} catch (error) {
6144
console.error('Error loading image: ', error);
6245
noteBlockImage = Promise.reject(error);
@@ -102,7 +85,9 @@ if (typeof document === 'undefined') {
10285
});
10386
};
10487

105-
const noteBlockImage = loadImage(getPath('/img/note-block-grayscale.png'));
88+
const noteBlockImagePath = getPath('/img/note-block-grayscale.png');
89+
90+
const noteBlockImage = loadImage(noteBlockImagePath);
10691

10792
canvasUtils = {
10893
createCanvas,

shared/features/thumbnail/index.ts

Lines changed: 11 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,16 @@
11
import {
2-
DrawingCanvas,
3-
RenderedImage,
42
createCanvas,
53
noteBlockImage,
64
saveToImage,
5+
useFont,
76
} from './canvasFactory';
8-
import { NoteQuadTree } from '../song/notes';
7+
import { Canvas, DrawParams } from './types';
8+
import { getKeyText, instrumentColors, isDarkColor, tintImage } from './utils';
99

1010
export { bgColorsArray } from './colors';
11+
useFont();
1112

12-
interface DrawParams {
13-
notes: NoteQuadTree;
14-
startTick: number;
15-
startLayer: number;
16-
zoomLevel: number;
17-
backgroundColor: string;
18-
canvasWidth?: number;
19-
canvasHeight?: number;
20-
imgWidth: number;
21-
imgHeight: number;
22-
}
23-
24-
type Canvas = typeof DrawingCanvas;
25-
type Image = typeof RenderedImage;
26-
27-
const instrumentColors = [
28-
'#1964ac',
29-
'#3c8e48',
30-
'#be6b6b',
31-
'#bebe19',
32-
'#9d5a98',
33-
'#572b21',
34-
'#bec65c',
35-
'#be19be',
36-
'#52908d',
37-
'#bebebe',
38-
'#1991be',
39-
'#be2328',
40-
'#be5728',
41-
'#19be19',
42-
'#be1957',
43-
'#575757',
44-
];
45-
46-
const tintedImages: Record<string, Canvas> = {};
47-
48-
// Function to apply tint to an image
49-
function tintImage(image: Image, color: string): Canvas {
50-
if (tintedImages[color]) {
51-
return tintedImages[color];
52-
}
53-
54-
const canvas = createCanvas(image.width, image.height);
55-
const ctx = canvas.getContext('2d');
56-
57-
if (!ctx) {
58-
throw new Error('Could not get canvas context');
59-
}
60-
61-
// Fill background with the color
62-
ctx.fillStyle = color;
63-
ctx.fillRect(0, 0, canvas.width, canvas.height);
64-
65-
// Apply the note block texture to the color
66-
ctx.globalCompositeOperation = 'hard-light';
67-
ctx.globalAlpha = 0.67;
68-
ctx.drawImage(image, 0, 0);
69-
70-
// Reset canvas settings
71-
ctx.globalCompositeOperation = 'source-over';
72-
ctx.globalAlpha = 1;
73-
74-
tintedImages[color] = canvas;
75-
76-
return canvas;
77-
}
78-
79-
// Function to convert key number to key text
80-
function getKeyText(key: number): string {
81-
const octaves = Math.floor((key + 9) / 12);
82-
83-
const notes = [
84-
'C',
85-
'C#',
86-
'D',
87-
'D#',
88-
'E',
89-
'F',
90-
'F#',
91-
'G',
92-
'G#',
93-
'A',
94-
'A#',
95-
'B',
96-
];
97-
98-
const note = notes[(key + 9) % 12];
99-
100-
return `${note}${octaves}`;
101-
}
102-
103-
function getLuma(color: string): number {
104-
// source: https://stackoverflow.com/a/12043228/9045426
105-
106-
const c = color?.substring(1) || ''; // strip #
107-
const rgb = parseInt(c, 16); // convert rrggbb to decimal
108-
const r = (rgb >> 16) & 0xff; // extract red
109-
const g = (rgb >> 8) & 0xff; // extract green
110-
const b = (rgb >> 0) & 0xff; // extract blue
111-
112-
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b; // per ITU-R BT.709
113-
114-
return luma;
115-
}
116-
117-
function isDarkColor(color: string, threshold = 40): boolean {
118-
return getLuma(color) < threshold;
119-
}
120-
121-
export async function swap(src: Canvas, dst: Canvas) {
13+
export const swap = async (src: Canvas, dst: Canvas) => {
12214
/**
12315
* Run a `drawFunction` that returns a canvas and draw it to the passed `canvas`.
12416
*
@@ -127,7 +19,6 @@ export async function swap(src: Canvas, dst: Canvas) {
12719
*
12820
* @returns Nothing
12921
*/
130-
13122
// Get canvas context
13223
const ctx = dst.getContext('2d');
13324

@@ -137,9 +28,9 @@ export async function swap(src: Canvas, dst: Canvas) {
13728

13829
// Swap the canvas
13930
ctx.drawImage(src, 0, 0);
140-
}
31+
};
14132

142-
export async function drawNotesOffscreen({
33+
export const drawNotesOffscreen = async ({
14334
notes,
14435
startTick,
14536
startLayer,
@@ -149,7 +40,7 @@ export async function drawNotesOffscreen({
14940
//canvasHeight,
15041
imgWidth = 1280,
15142
imgHeight = 768,
152-
}: DrawParams) {
43+
}: DrawParams) => {
15344
// Create new offscreen canvas
15445
const canvas = createCanvas(imgWidth, imgHeight);
15546
const ctx = canvas.getContext('2d');
@@ -235,9 +126,9 @@ export async function drawNotesOffscreen({
235126
});
236127

237128
return canvas;
238-
}
129+
};
239130

240-
export async function drawToImage(params: DrawParams): Promise<Buffer> {
131+
export const drawToImage = async (params: DrawParams): Promise<Buffer> => {
241132
let canvas;
242133
const { imgWidth, imgHeight } = params;
243134

@@ -251,4 +142,4 @@ export async function drawToImage(params: DrawParams): Promise<Buffer> {
251142
// Convert to Buffer
252143
const buffer = Buffer.from(byteArray);
253144
return buffer;
254-
}
145+
};

shared/features/thumbnail/types.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type NapiRs from '@napi-rs/canvas';
2+
3+
import { DrawingCanvas, RenderedImage } from './canvasFactory';
4+
import { NoteQuadTree } from '../song/notes';
5+
6+
export interface DrawParams {
7+
notes: NoteQuadTree;
8+
startTick: number;
9+
startLayer: number;
10+
zoomLevel: number;
11+
backgroundColor: string;
12+
canvasWidth?: number;
13+
canvasHeight?: number;
14+
imgWidth: number;
15+
imgHeight: number;
16+
}
17+
18+
export type Canvas = typeof DrawingCanvas;
19+
20+
export type Image = typeof RenderedImage; /*
21+
import type {
22+
Canvas as NapiCanvas,
23+
Image as NapiImage,
24+
GlobalFonts as NapiGlobalFonts,
25+
} from '@napi-rs/canvas';
26+
*/
27+
28+
export interface CanvasUtils {
29+
createCanvas(width: number, height: number): any;
30+
loadImage(src: string): Promise<any>;
31+
getPath(filename: string): string | URL;
32+
useFont(): void;
33+
saveToImage(canvas: HTMLCanvasElement | NapiRs.Canvas): Promise<Uint8Array>;
34+
noteBlockImage: Promise<any> | any;
35+
DrawingCanvas: any;
36+
RenderedImage: any;
37+
}

0 commit comments

Comments
 (0)