mirror of https://github.com/tycrek/ass
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.9 KiB
67 lines
1.9 KiB
import { AssFile, AssUser, NID, FilesSchema, UsersSchema } from 'ass';
|
|
|
|
import fs from 'fs-extra';
|
|
import { path } from '@tycrek/joint';
|
|
|
|
import { log } from './log';
|
|
import { UserConfig } from './UserConfig';
|
|
import { DBManager } from './sql/database';
|
|
|
|
/**
|
|
* Switcher type for exported functions
|
|
*/
|
|
type DataSector = 'files' | 'users';
|
|
|
|
/**
|
|
* Absolute filepaths for JSON data files
|
|
*/
|
|
const PATHS = {
|
|
files: path.join('.ass-data/files.json'),
|
|
users: path.join('.ass-data/users.json')
|
|
};
|
|
|
|
/**
|
|
* database kind -> name mapping
|
|
*/
|
|
const DBNAMES = {
|
|
'mysql': 'MySQL',
|
|
'postgres': 'PostgreSQL',
|
|
'json': 'JSON'
|
|
};
|
|
|
|
export const put = (sector: DataSector, key: NID, data: AssFile | AssUser): Promise<void> => new Promise(async (resolve, reject) => {
|
|
try {
|
|
if (sector === 'files') {
|
|
// * 1: Save as files (image, video, etc)
|
|
await DBManager.put('assfiles', key, data as AssFile);
|
|
} else {
|
|
// * 2: Save as users
|
|
await DBManager.put('assusers', key, data as AssUser);
|
|
}
|
|
|
|
log.info(`PUT ${sector} data`, `using ${DBNAMES[UserConfig.config.database?.kind ?? 'json']}`, key);
|
|
resolve(void 0);
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
});
|
|
|
|
export const get = (sector: DataSector, key: NID): Promise<AssFile | AssUser | false> => new Promise(async (resolve, reject) => {
|
|
try {
|
|
const data: AssFile | AssUser | undefined = await DBManager.get(sector === 'files' ? 'assfiles' : 'assusers', key) as AssFile | AssUser | undefined
|
|
(!data) ? resolve(false) : resolve(data);
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
});
|
|
|
|
export const getAll = (sector: DataSector): Promise<{ [key: string]: AssFile | AssUser }> => new Promise(async (resolve, reject) => {
|
|
try {
|
|
// todo: fix MySQL
|
|
const data: { [key: string]: AssFile | AssUser } = await DBManager.getAll(sector === 'files' ? 'assfiles' : 'assusers') as /* AssFile[] | AssUser[] | */ {}
|
|
resolve(data);
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
});
|