mirror of
https://github.com/anatolykopyl/movieroom-core.git
synced 2026-03-26 12:54:51 +00:00
Added download and reencoding progress to mongo
This commit is contained in:
19
package-lock.json
generated
19
package-lock.json
generated
@@ -22,6 +22,7 @@
|
||||
"webtorrent": "^1.5.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/handbrake-js": "^5.0.1",
|
||||
"@types/koa": "^2.13.4",
|
||||
"@types/koa__cors": "^3.1.1",
|
||||
"@types/koa-bodyparser": "^4.3.5",
|
||||
@@ -290,6 +291,15 @@
|
||||
"@types/range-parser": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/handbrake-js": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/handbrake-js/-/handbrake-js-5.0.1.tgz",
|
||||
"integrity": "sha512-e9hUk3kSYYGeUCE4VjMPI6SKHUF3Y43PVPNwGgBhDcRJBFokhdx9YiHEgt28krJfb7pnerReasObRzHP0qI4vA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/http-assert": {
|
||||
"version": "1.5.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/http-assert/-/http-assert-1.5.3.tgz",
|
||||
@@ -7008,6 +7018,15 @@
|
||||
"@types/range-parser": "*"
|
||||
}
|
||||
},
|
||||
"@types/handbrake-js": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/handbrake-js/-/handbrake-js-5.0.1.tgz",
|
||||
"integrity": "sha512-e9hUk3kSYYGeUCE4VjMPI6SKHUF3Y43PVPNwGgBhDcRJBFokhdx9YiHEgt28krJfb7pnerReasObRzHP0qI4vA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/http-assert": {
|
||||
"version": "1.5.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/http-assert/-/http-assert-1.5.3.tgz",
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
"webtorrent": "^1.5.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/handbrake-js": "^5.0.1",
|
||||
"@types/koa": "^2.13.4",
|
||||
"@types/koa__cors": "^3.1.1",
|
||||
"@types/koa-bodyparser": "^4.3.5",
|
||||
|
||||
28
src/index.ts
28
src/index.ts
@@ -35,7 +35,18 @@ router.post('/room', async (ctx) => {
|
||||
};
|
||||
const doc = new RoomModel(room);
|
||||
|
||||
torrent.on('done', function () {
|
||||
async function setDownloadProg() {
|
||||
await RoomModel.updateOne({ id: room.id }, {
|
||||
downloadedProg: torrent.progress,
|
||||
});
|
||||
}
|
||||
|
||||
torrent
|
||||
.on('download', async () => {
|
||||
setDownloadProg();
|
||||
})
|
||||
.on('done', async () => {
|
||||
setDownloadProg();
|
||||
torrent.destroy();
|
||||
processDownloaded(torrent, room.id);
|
||||
});
|
||||
@@ -84,22 +95,11 @@ router.get('/status', async (ctx) => {
|
||||
const room = await RoomModel.findOne({ id: ctx.request.query.id }).exec();
|
||||
|
||||
if (room) {
|
||||
if (room.downloaded) {
|
||||
ctx.body = {
|
||||
progress: 1,
|
||||
downloadedProg: room.downloadedProg,
|
||||
reencodedProg: room.reencodedProg,
|
||||
downloaded: room.downloaded,
|
||||
};
|
||||
} else {
|
||||
const torrent = tCli.get(room.magnet);
|
||||
if (torrent) {
|
||||
ctx.body = {
|
||||
progress: (torrent as WebTorrent.Torrent).progress,
|
||||
downloaded: room.downloaded,
|
||||
};
|
||||
} else {
|
||||
({ status: ctx.status, body: ctx.body } = errorResponse('status-00', 'No torrent found'));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
({ status: ctx.status, body: ctx.body } = errorResponse('status-01', 'No room found'));
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ export interface Room {
|
||||
createdAt: Date;
|
||||
movie?: string;
|
||||
filename?: string;
|
||||
downloadedProg?: number;
|
||||
reencodedProg?: number;
|
||||
downloaded?: boolean;
|
||||
downloadedAt?: Date;
|
||||
position: number;
|
||||
@@ -19,6 +21,8 @@ export const roomSchema = new Schema<Room>({
|
||||
movie: { type: String, required: false },
|
||||
filename: { type: String, required: false },
|
||||
downloaded: { type: Boolean, required: false },
|
||||
downloadedProg: { type: Number, required: false },
|
||||
reencodedProg: { type: Number, required: false },
|
||||
downloadedAt: { type: Date, required: false },
|
||||
position: { type: Number, required: true },
|
||||
syncedAt: { type: Date, required: true },
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fs from 'fs';
|
||||
import mv from 'mv';
|
||||
import { spawn } from 'handbrake-js';
|
||||
import type short from 'short-uuid';
|
||||
import type WebTorrent from 'webtorrent';
|
||||
import { model } from 'mongoose';
|
||||
@@ -8,18 +9,38 @@ import { Room, roomSchema } from './interfaces';
|
||||
|
||||
const RoomModel = model<Room>('Room', roomSchema);
|
||||
|
||||
function deleteTemp(torrent: WebTorrent.Torrent) {
|
||||
fs.rmSync(__dirname + '/../' + torrent.path + '/' + torrent.name, { recursive: true });
|
||||
}
|
||||
|
||||
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 () => {
|
||||
const extensionsRegEx = /(\.mp4|\.mkv|\.avi)$/;
|
||||
findInDir(torrent.path, extensionsRegEx, async (filename: string) => {
|
||||
const originalExtension = filename.split('.').pop();
|
||||
const extension = '.mp4';
|
||||
const outputLocation = `${process.env.FILES}/${id}.${extension}`;
|
||||
|
||||
if (originalExtension !== 'mp4') {
|
||||
spawn({ input: filename, output: outputLocation })
|
||||
.on('error', console.error)
|
||||
.on('progress', async (progress) => {
|
||||
await RoomModel.updateOne({ id: id }, {
|
||||
reencodedProg: progress.percentComplete / 100,
|
||||
});
|
||||
})
|
||||
.on('complete', () => {
|
||||
deleteTemp(torrent);
|
||||
});
|
||||
} else {
|
||||
mv(filename, outputLocation, async () => {
|
||||
await RoomModel.updateOne({ id: id }, {
|
||||
filename: id + '.' + extension,
|
||||
downloaded: true,
|
||||
downloadedAt: new Date(),
|
||||
});
|
||||
|
||||
fs.rmSync(__dirname + '/../' + torrent.path + '/' + torrent.name, { recursive: true });
|
||||
});
|
||||
deleteTemp(torrent);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user