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:
52
src/index.ts
52
src/index.ts
@@ -6,13 +6,12 @@ import json from 'koa-json';
|
||||
import bodyParser from 'koa-bodyparser';
|
||||
import cors from '@koa/cors';
|
||||
|
||||
import fs from 'fs';
|
||||
import mv from 'mv';
|
||||
import { model, connect } from 'mongoose';
|
||||
import short from 'short-uuid';
|
||||
import WebTorrent from 'webtorrent';
|
||||
|
||||
import { errorResponse, findInDir } from './utils';
|
||||
import { errorResponse } from './utils';
|
||||
import processDownloaded from './processDownloaded';
|
||||
import { Room, roomSchema } from './interfaces';
|
||||
|
||||
const RoomModel = model<Room>('Room', roomSchema);
|
||||
@@ -23,36 +22,29 @@ const app = new Koa();
|
||||
const router = new Router({ prefix: '/api' });
|
||||
|
||||
router.post('/room', async (ctx) => {
|
||||
return new Promise(function (resolve) {
|
||||
tCli.add(ctx.request.body.magnet, { path: process.env.TEMP_FILES }, async function (torrent) {
|
||||
const room = {
|
||||
id: (short()).new(),
|
||||
magnet: ctx.request.body.magnet,
|
||||
createdAt: new Date(),
|
||||
movie: torrent.name,
|
||||
position: 0,
|
||||
};
|
||||
const doc = new RoomModel(room);
|
||||
|
||||
torrent.on('done', function () {
|
||||
const extensionsRegEx = /(\.mp4|\.mkv)$/;
|
||||
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 });
|
||||
});
|
||||
if (ctx.request.body.magnet) {
|
||||
return new Promise(function (resolve) {
|
||||
tCli.add(ctx.request.body.magnet, { path: process.env.TEMP_FILES }, async function (torrent) {
|
||||
const room = {
|
||||
id: (short()).new(),
|
||||
magnet: ctx.request.body.magnet,
|
||||
createdAt: new Date(),
|
||||
movie: torrent.name,
|
||||
position: 0,
|
||||
};
|
||||
const doc = new RoomModel(room);
|
||||
|
||||
torrent.on('done', function () {
|
||||
processDownloaded(torrent, room.id);
|
||||
});
|
||||
|
||||
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) => {
|
||||
|
||||
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