mirror of
https://github.com/anatolykopyl/movieroom-core.git
synced 2026-03-26 12:54:51 +00:00
Moved after download processing to another file
This commit is contained in:
20
src/index.ts
20
src/index.ts
@@ -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,6 +22,7 @@ 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) => {
|
||||||
|
if (ctx.request.body.magnet) {
|
||||||
return new Promise(function (resolve) {
|
return new Promise(function (resolve) {
|
||||||
tCli.add(ctx.request.body.magnet, { path: process.env.TEMP_FILES }, async function (torrent) {
|
tCli.add(ctx.request.body.magnet, { path: process.env.TEMP_FILES }, async function (torrent) {
|
||||||
const room = {
|
const room = {
|
||||||
@@ -35,17 +35,7 @@ router.post('/room', async (ctx) => {
|
|||||||
const doc = new RoomModel(room);
|
const doc = new RoomModel(room);
|
||||||
|
|
||||||
torrent.on('done', function () {
|
torrent.on('done', function () {
|
||||||
const extensionsRegEx = /(\.mp4|\.mkv)$/;
|
processDownloaded(torrent, room.id);
|
||||||
findInDir(torrent.path, extensionsRegEx, (filename: string) => {
|
|
||||||
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();
|
await doc.save();
|
||||||
@@ -53,6 +43,8 @@ router.post('/room', async (ctx) => {
|
|||||||
resolve(null);
|
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
25
src/processDownloaded.ts
Normal 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 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user