Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed May 30, 2024
1 parent 2569d9f commit 5b490a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 78 deletions.
7 changes: 0 additions & 7 deletions d.ts/src/helpers/socket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,6 @@ export type ClientToServerEventsWithNamespace = {
'overlays::tick': (opts: {groupId: string, id: string, millis: number}) => void,
'parse': (text: string, cb: (err: Error | string | null | unknown, data: string) => void) => void,
},
'/overlays/gallery': GenericEvents & {
'generic::getOne': generic<GalleryInterface>['getOne'],
'generic::getAll': generic<GalleryInterface>['getAll'],
'generic::deleteById': generic<GalleryInterface>['deleteById'],
'generic::setById': generic<GalleryInterface>['setById'],
'gallery::upload': (data: [filename: string, data: { id: string, b64data: string, folder?: string }], cb: (err: Error | string | null | unknown, item?: OverlayMapperMarathon) => void) => void,
},
'/overlays/media': GenericEvents & {
'alert': (data: any) => void,
'cache': (cacheLimit: number, cb: (err: Error | string | null | unknown, data: any) => void) => void,
Expand Down
104 changes: 33 additions & 71 deletions src/overlays/gallery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { fileURLToPath } from 'node:url';
import path, { dirname } from 'path';

import { Gallery as GalleryEntity } from '@entity/gallery.js';
import { Request } from 'express';

import Overlay from './_interface.js';

import { AppDataSource } from '~/database.js';
import { Delete, Get, Post } from '~/decorators/endpoint.js';
import { debug } from '~/helpers/log.js';
import { app } from '~/helpers/panel.js';
import { adminEndpoint } from '~/helpers/socket.js';

// __dirname is not available in ES6 module
const __filename = fileURLToPath(import.meta.url);
Expand Down Expand Up @@ -66,76 +67,37 @@ class Gallery extends Overlay {
init();
}

sockets () {
adminEndpoint('/overlays/gallery', 'generic::getOne', async (id, cb) => {
try {
const item = await AppDataSource.getRepository(GalleryEntity).findOne({
where: { id },
select: ['id', 'name', 'type', 'folder'],
});
cb(null, item);
} catch (e: any) {
cb(e.stack, null);
}
});
adminEndpoint('/overlays/gallery', 'generic::getAll', async (cb) => {
try {
const items = await AppDataSource.getRepository(GalleryEntity).find({ select: ['id', 'name', 'type', 'folder'] });
cb(null, items);
} catch (e: any) {
cb(e.stack, []);
}
});
adminEndpoint('/overlays/gallery', 'generic::deleteById', async (id, cb) => {
try {
await AppDataSource.getRepository(GalleryEntity).delete({ id: String(id) });
cb(null);
} catch (e: any) {
cb(e.stack);
}
});
adminEndpoint('/overlays/gallery', 'generic::setById', async (opts, cb) => {
try {
cb(null, await AppDataSource.getRepository(GalleryEntity).save({
...(await AppDataSource.getRepository(GalleryEntity).findOneBy({ id: String(opts.id) })),
...opts.item,
}));
cb(null);
} catch (e: any) {
cb(e.stack);
}
});
adminEndpoint('/overlays/gallery', 'gallery::upload', async (data, cb) => {
try {
const filename = data[0];
const filedata = data[1] as { id: string, b64data: string, folder: string };
const matches = filedata.b64data.match(/^data:([0-9A-Za-z-+/]+);base64,(.+)$/);
if (!matches) {
// update entity
const item = await AppDataSource.getRepository(GalleryEntity).findOneByOrFail({ id: filedata.id });
await AppDataSource.getRepository(GalleryEntity).save({
id: item.id,
type: item.type,
data: item.data + filedata.b64data,
folder: filedata.folder,
name: item.name,
});
} else {
// new entity
const type = matches[1];
await AppDataSource.getRepository(GalleryEntity).save({
id: filedata.id, type, data: filedata.b64data, name: filename, folder: filedata.folder,
});
}
if (cb) {
cb(null);
}
} catch (e: any) {
if (cb) {
cb(e.stack);
}
}
});
@Get('/')
async getAll () {
return AppDataSource.getRepository(GalleryEntity).find({ select: ['id', 'name', 'type', 'folder'] });
}

@Delete('/:id')
async delete (req: Request) {
return AppDataSource.getRepository(GalleryEntity).delete({ id: req.params.id });
}

@Post('/')
async upload (req: Request) {
const { id, b64data, folder, name } = req.body;
const matches = b64data.match(/^data:([0-9A-Za-z-+/]+);base64,(.+)$/);
if (!matches) {
// update entity
const item = await AppDataSource.getRepository(GalleryEntity).findOneByOrFail({ id });
await AppDataSource.getRepository(GalleryEntity).save({
id: item.id,
type: item.type,
data: item.data + b64data,
folder: folder,
name: item.name,
});
} else {
// new entity
const type = matches[1];
await AppDataSource.getRepository(GalleryEntity).save({
id, type, data: b64data, name: name, folder,
});
}
}
}

Expand Down

0 comments on commit 5b490a7

Please sign in to comment.