mirror of
https://github.com/jeffvli/feishin.git
synced 2026-06-09 22:02:19 +02:00
Update scanner (server)
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import { albumArtistsController } from './album-artists.controller';
|
||||
import { albumsController } from './albums.controller';
|
||||
import { artistsController } from './artists.controller';
|
||||
import { authController } from './auth.controller';
|
||||
import { serversController } from './servers.controller';
|
||||
import { songsController } from './songs.controller';
|
||||
import { usersController } from './users.controller';
|
||||
import { albumArtistsController } from '@controllers/album-artists.controller';
|
||||
import { albumsController } from '@controllers/albums.controller';
|
||||
import { artistsController } from '@controllers/artists.controller';
|
||||
import { authController } from '@controllers/auth.controller';
|
||||
import { serversController } from '@controllers/servers.controller';
|
||||
import { songsController } from '@controllers/songs.controller';
|
||||
import { tasksController } from '@controllers/tasks.controller';
|
||||
import { usersController } from '@controllers/users.controller';
|
||||
|
||||
export const controller = {
|
||||
albumArtists: albumArtistsController,
|
||||
@@ -13,5 +14,6 @@ export const controller = {
|
||||
auth: authController,
|
||||
servers: serversController,
|
||||
songs: songsController,
|
||||
tasks: tasksController,
|
||||
users: usersController,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ServerType } from '@prisma/client';
|
||||
import { Response } from 'express';
|
||||
import { ApiSuccess, getSuccessResponse } from '@/utils';
|
||||
import { ApiError, ApiSuccess, getSuccessResponse } from '@/utils';
|
||||
import { toApiModel } from '@helpers/api-model';
|
||||
import { service } from '@services/index';
|
||||
import { TypedRequest, validation } from '@validations/index';
|
||||
@@ -109,7 +109,7 @@ const refreshServer = async (
|
||||
return res.status(success.statusCode).json(getSuccessResponse(success));
|
||||
};
|
||||
|
||||
const scanServer = async (
|
||||
const fullScanServer = async (
|
||||
req: TypedRequest<typeof validation.servers.scan>,
|
||||
res: Response
|
||||
) => {
|
||||
@@ -118,15 +118,56 @@ const scanServer = async (
|
||||
|
||||
// TODO: Check that server is accessible first with the saved token, otherwise throw error
|
||||
|
||||
const data = await service.servers.fullScan({
|
||||
const scansInProgress = await service.servers.findScanInProgress({
|
||||
serverId,
|
||||
});
|
||||
|
||||
if (scansInProgress.length > 0) {
|
||||
throw ApiError.badRequest('Scan already in progress');
|
||||
}
|
||||
|
||||
const io = req.app.get('socketio');
|
||||
await io.emit('task:started');
|
||||
|
||||
const data = await service.servers.fullScan(req.authUser, {
|
||||
id: serverId,
|
||||
serverFolderId,
|
||||
});
|
||||
|
||||
// return res.status(200).json({ data: null });
|
||||
const success = ApiSuccess.ok({ data });
|
||||
return res.status(success.statusCode).json(getSuccessResponse(success));
|
||||
};
|
||||
|
||||
const quickScanServer = async (
|
||||
req: TypedRequest<typeof validation.servers.scan>,
|
||||
res: Response
|
||||
) => {
|
||||
const { serverId } = req.params;
|
||||
const { serverFolderId } = req.body;
|
||||
|
||||
// TODO: Check that server is accessible first with the saved token, otherwise throw error
|
||||
|
||||
const scansInProgress = await service.servers.findScanInProgress({
|
||||
serverId,
|
||||
});
|
||||
|
||||
if (scansInProgress.length > 0) {
|
||||
throw ApiError.badRequest('Scan already in progress');
|
||||
}
|
||||
|
||||
const io = req.app.get('socketio');
|
||||
await io.emit('task:started');
|
||||
|
||||
// await service.servers.fullScan({
|
||||
// id: serverId,
|
||||
// serverFolderId,
|
||||
// });
|
||||
|
||||
const success = ApiSuccess.ok({ data: null });
|
||||
return res.status(success.statusCode).json(getSuccessResponse(success));
|
||||
};
|
||||
|
||||
const createServerUrl = async (
|
||||
req: TypedRequest<typeof validation.servers.createUrl>,
|
||||
res: Response
|
||||
@@ -228,9 +269,10 @@ export const serversController = {
|
||||
disableServerUrl,
|
||||
enableServerFolder,
|
||||
enableServerUrl,
|
||||
fullScanServer,
|
||||
getServerDetail,
|
||||
getServerList,
|
||||
quickScanServer,
|
||||
refreshServer,
|
||||
scanServer,
|
||||
updateServer,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { queue } from '@/queue/queues';
|
||||
import { toApiModel } from '@helpers/api-model';
|
||||
import { prisma } from '@lib/prisma';
|
||||
import { ApiSuccess } from '@utils/api-success';
|
||||
import { getSuccessResponse } from '@utils/get-success-response';
|
||||
import { validation } from '@validations/index';
|
||||
import { TypedRequest } from '@validations/shared.validation';
|
||||
import { SortOrder } from '../types/types';
|
||||
|
||||
const getActiveTasks = async (_req: Request, res: Response) => {
|
||||
const tasks = await prisma.task.findMany({
|
||||
include: {
|
||||
server: true,
|
||||
user: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: SortOrder.ASC,
|
||||
},
|
||||
where: {
|
||||
completed: false,
|
||||
isError: false,
|
||||
},
|
||||
});
|
||||
|
||||
if (queue.scanner.length === 0) {
|
||||
await prisma.task.updateMany({
|
||||
data: { completed: true, isError: true, message: 'Task not found' },
|
||||
where: { completed: false },
|
||||
});
|
||||
}
|
||||
|
||||
const success = ApiSuccess.ok({ data: toApiModel.tasks({ items: tasks }) });
|
||||
return res.status(success.statusCode).json(getSuccessResponse(success));
|
||||
};
|
||||
|
||||
const cancelAllTasks = async (
|
||||
_req: TypedRequest<typeof validation.tasks.cancelAll>,
|
||||
res: Response
|
||||
) => {
|
||||
const runningTasks = await prisma.task.findMany({
|
||||
include: {
|
||||
server: true,
|
||||
user: true,
|
||||
},
|
||||
where: {
|
||||
completed: false,
|
||||
isError: false,
|
||||
},
|
||||
});
|
||||
|
||||
for (const task of runningTasks) {
|
||||
queue.scanner.push({
|
||||
fn: async () => {
|
||||
return {};
|
||||
},
|
||||
id: task.id,
|
||||
});
|
||||
}
|
||||
|
||||
await prisma.task.updateMany({
|
||||
data: {
|
||||
completed: true,
|
||||
message: 'Task was cancelled by user',
|
||||
},
|
||||
where: { completed: false },
|
||||
});
|
||||
|
||||
const success = ApiSuccess.noContent({ data: null });
|
||||
return res.status(success.statusCode).json(getSuccessResponse(success));
|
||||
};
|
||||
|
||||
const cancelTaskById = async (
|
||||
req: TypedRequest<typeof validation.tasks.cancel>,
|
||||
res: Response
|
||||
) => {
|
||||
const { taskId } = req.params;
|
||||
|
||||
const task = await prisma.task.update({
|
||||
data: {
|
||||
completed: true,
|
||||
message: 'Task was cancelled by user',
|
||||
},
|
||||
include: {
|
||||
server: true,
|
||||
user: true,
|
||||
},
|
||||
where: { id: taskId },
|
||||
});
|
||||
|
||||
queue.scanner.push({
|
||||
fn: async () => {
|
||||
return {};
|
||||
},
|
||||
id: taskId,
|
||||
});
|
||||
|
||||
const success = ApiSuccess.ok({
|
||||
data: toApiModel.tasks({ items: [task] })[0],
|
||||
});
|
||||
return res.status(success.statusCode).json(getSuccessResponse(success));
|
||||
};
|
||||
|
||||
export const tasksController = {
|
||||
cancelAllTasks,
|
||||
cancelTaskById,
|
||||
getActiveTasks,
|
||||
};
|
||||
Reference in New Issue
Block a user