Reencoding working

This commit is contained in:
2022-01-01 22:09:25 +03:00
parent 690731ff81
commit 6e89f72c4c

View File

@@ -9,7 +9,13 @@ import { Room, roomSchema } from './interfaces';
const RoomModel = model<Room>('Room', roomSchema); const RoomModel = model<Room>('Room', roomSchema);
function deleteTemp(torrent: WebTorrent.Torrent) { async function completeDownload(torrent: WebTorrent.Torrent, id: short.SUUID, ext: string) {
await RoomModel.updateOne({ id: id }, {
filename: id + '.' + ext,
downloaded: true,
downloadedAt: new Date(),
});
fs.rmSync(__dirname + '/../' + torrent.path + '/' + torrent.name, { recursive: true }); fs.rmSync(__dirname + '/../' + torrent.path + '/' + torrent.name, { recursive: true });
} }
@@ -17,7 +23,7 @@ export default function (torrent: WebTorrent.Torrent, id: short.SUUID) {
const extensionsRegEx = /(\.mp4|\.mkv|\.avi)$/; const extensionsRegEx = /(\.mp4|\.mkv|\.avi)$/;
findInDir(torrent.path, extensionsRegEx, async (filename: string) => { findInDir(torrent.path, extensionsRegEx, async (filename: string) => {
const originalExtension = filename.split('.').pop(); const originalExtension = filename.split('.').pop();
const extension = '.mp4'; const extension = 'mp4';
const outputLocation = `${process.env.FILES}/${id}.${extension}`; const outputLocation = `${process.env.FILES}/${id}.${extension}`;
if (originalExtension !== 'mp4') { if (originalExtension !== 'mp4') {
@@ -29,17 +35,11 @@ export default function (torrent: WebTorrent.Torrent, id: short.SUUID) {
}); });
}) })
.on('complete', () => { .on('complete', () => {
deleteTemp(torrent); completeDownload(torrent, id, extension);
}); });
} else { } else {
mv(filename, outputLocation, async () => { mv(filename, outputLocation, async () => {
await RoomModel.updateOne({ id: id }, { completeDownload(torrent, id, extension);
filename: id + '.' + extension,
downloaded: true,
downloadedAt: new Date(),
});
deleteTemp(torrent);
}); });
} }
}); });