Moved after download processing to another file

This commit is contained in:
2021-12-30 23:49:26 +03:00
parent 5e6a557a44
commit 942d341636
2 changed files with 47 additions and 30 deletions

View File

@@ -6,13 +6,12 @@ import json from 'koa-json';
import bodyParser from 'koa-bodyparser'; import bodyParser from 'koa-bodyparser';
import cors from '@koa/cors'; import cors from '@koa/cors';
import fs from 'fs';
import mv from 'mv';
import { model, connect } from 'mongoose'; import { model, connect } from 'mongoose';
import short from 'short-uuid'; import short from 'short-uuid';
import WebTorrent from 'webtorrent'; import WebTorrent from 'webtorrent';
import { errorResponse, findInDir } from './utils'; import { errorResponse } from './utils';
import processDownloaded from './processDownloaded';
import { Room, roomSchema } from './interfaces'; import { Room, roomSchema } from './interfaces';
const RoomModel = model<Room>('Room', roomSchema); const RoomModel = model<Room>('Room', roomSchema);
@@ -23,36 +22,29 @@ const app = new Koa();
const router = new Router({ prefix: '/api' }); const router = new Router({ prefix: '/api' });
router.post('/room', async (ctx) => { router.post('/room', async (ctx) => {
return new Promise(function (resolve) { if (ctx.request.body.magnet) {
tCli.add(ctx.request.body.magnet, { path: process.env.TEMP_FILES }, async function (torrent) { return new Promise(function (resolve) {
const room = { tCli.add(ctx.request.body.magnet, { path: process.env.TEMP_FILES }, async function (torrent) {
id: (short()).new(), const room = {
magnet: ctx.request.body.magnet, id: (short()).new(),
createdAt: new Date(), magnet: ctx.request.body.magnet,
movie: torrent.name, createdAt: new Date(),
position: 0, movie: torrent.name,
}; position: 0,
const doc = new RoomModel(room); };
const doc = new RoomModel(room);
torrent.on('done', function () {
const extensionsRegEx = /(\.mp4|\.mkv)$/; torrent.on('done', function () {
findInDir(torrent.path, extensionsRegEx, (filename: string) => { processDownloaded(torrent, room.id);
const extension = filename.split('.').pop();
mv(`./${filename}`, `${process.env.FILES}/${room.id}.${extension}`, () => {
doc.filename = room.id + '.' + extension;
doc.downloaded = true;
doc.downloadedAt = new Date();
doc.save();
fs.rmSync(__dirname + '/../' + torrent.path + '/' + torrent.name, { recursive: true });
});
}); });
await doc.save();
ctx.body = room;
resolve(null);
}); });
await doc.save();
ctx.body = room;
resolve(null);
}); });
}); }
({ status: ctx.status, body: ctx.body } = errorResponse('room-01', 'Invalid magnet link'));
}); });
router.get('/room', async (ctx) => { router.get('/room', async (ctx) => {

25
src/processDownloaded.ts Normal file
View File

@@ -0,0 +1,25 @@
import fs from 'fs';
import mv from 'mv';
import type short from 'short-uuid';
import type WebTorrent from 'webtorrent';
import { model } from 'mongoose';
import { findInDir } from './utils';
import { Room, roomSchema } from './interfaces';
const RoomModel = model<Room>('Room', roomSchema);
export default function (torrent: WebTorrent.Torrent, id: short.SUUID) {
const extensionsRegEx = /(\.mp4|\.mkv)$/;
findInDir(torrent.path, extensionsRegEx, (filename: string) => {
const extension = filename.split('.').pop();
mv(`./${filename}`, `${process.env.FILES}/${id}.${extension}`, async () => {
await RoomModel.updateOne({ id: id }, {
filename: id + '.' + extension,
downloaded: true,
downloadedAt: new Date(),
});
fs.rmSync(__dirname + '/../' + torrent.path + '/' + torrent.name, { recursive: true });
});
});
}