From ddd845d9d16e2ac629a8de22484a56092f091354 Mon Sep 17 00:00:00 2001 From: Tommy Mikkelsen Date: Thu, 17 Nov 2022 14:10:02 +0100 Subject: [PATCH] #507 WIP --- package-lock.json | 32 ++-- src/background.js | 29 ++++ .../modules/Download/scripts/Download.js | 140 +++++++++++++++++- 3 files changed, 178 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0ed07bc..6ff1998 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10077,7 +10077,7 @@ "node_modules/html-webpack-plugin": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-3.2.0.tgz", - "integrity": "sha1-sBq71yOsqqeze2r0SS69oD2d03s=", + "integrity": "sha512-Br4ifmjQojUP4EmHnRBoUIYcZ9J7M4bTMcm7u6xoIAIuq2Nte4TzXX0533owvkQKQD1WeMTTTyD4Ni4QKxS0Bg==", "deprecated": "3.x is no longer supported", "dev": true, "dependencies": { @@ -10126,7 +10126,7 @@ "node_modules/html-webpack-plugin/node_modules/loader-utils": { "version": "0.2.17", "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-0.2.17.tgz", - "integrity": "sha1-+G5jdNQyBabmxg6RlvF8Apm/s0g=", + "integrity": "sha512-tiv66G0SmiOx+pLWMtGEkfSEejxvb6N6uRrQjfWJIT79W9GMpgKeCAmm9aVBKtd4WEgntciI8CsGqjpDoCWJug==", "dev": true, "dependencies": { "big.js": "^3.1.3", @@ -11766,9 +11766,9 @@ } }, "node_modules/loader-utils": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-1.4.0.tgz", - "integrity": "sha512-qH0WSMBtn/oHuwjy/NucEgbx5dbxxnxup9s4PVXJUDHZBQY+s0NWA9rJf53RBnQZxfch7euUui7hpoAPvALZdA==", + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-1.4.2.tgz", + "integrity": "sha512-I5d00Pd/jwMD2QCduo657+YM/6L3KZu++pmX9VFncxaxvHcru9jx1lBaFft+r4Mt2jK0Yhp41XlRAihzPxHNCg==", "dev": true, "dependencies": { "big.js": "^5.2.2", @@ -12512,9 +12512,9 @@ "dev": true }, "node_modules/minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.8.tgz", + "integrity": "sha512-6FsRAQsxQ61mw+qP1ZzbL9Bc78x2p5OqNgNpnoAFLTrX8n5Kxph0CsnhmKKNXTWjXqU5L0pGPR7hYk+XWZr60Q==", "dependencies": { "brace-expansion": "^1.1.7" }, @@ -29939,7 +29939,7 @@ "html-webpack-plugin": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-3.2.0.tgz", - "integrity": "sha1-sBq71yOsqqeze2r0SS69oD2d03s=", + "integrity": "sha512-Br4ifmjQojUP4EmHnRBoUIYcZ9J7M4bTMcm7u6xoIAIuq2Nte4TzXX0533owvkQKQD1WeMTTTyD4Ni4QKxS0Bg==", "dev": true, "requires": { "html-minifier": "^3.2.3", @@ -29972,7 +29972,7 @@ "loader-utils": { "version": "0.2.17", "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-0.2.17.tgz", - "integrity": "sha1-+G5jdNQyBabmxg6RlvF8Apm/s0g=", + "integrity": "sha512-tiv66G0SmiOx+pLWMtGEkfSEejxvb6N6uRrQjfWJIT79W9GMpgKeCAmm9aVBKtd4WEgntciI8CsGqjpDoCWJug==", "dev": true, "requires": { "big.js": "^3.1.3", @@ -31250,9 +31250,9 @@ "dev": true }, "loader-utils": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-1.4.0.tgz", - "integrity": "sha512-qH0WSMBtn/oHuwjy/NucEgbx5dbxxnxup9s4PVXJUDHZBQY+s0NWA9rJf53RBnQZxfch7euUui7hpoAPvALZdA==", + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-1.4.2.tgz", + "integrity": "sha512-I5d00Pd/jwMD2QCduo657+YM/6L3KZu++pmX9VFncxaxvHcru9jx1lBaFft+r4Mt2jK0Yhp41XlRAihzPxHNCg==", "dev": true, "requires": { "big.js": "^5.2.2", @@ -31855,9 +31855,9 @@ "dev": true }, "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.8.tgz", + "integrity": "sha512-6FsRAQsxQ61mw+qP1ZzbL9Bc78x2p5OqNgNpnoAFLTrX8n5Kxph0CsnhmKKNXTWjXqU5L0pGPR7hYk+XWZr60Q==", "requires": { "brace-expansion": "^1.1.7" } diff --git a/src/background.js b/src/background.js index 095c553..3c1dbd2 100644 --- a/src/background.js +++ b/src/background.js @@ -145,3 +145,32 @@ ipcMain.on('downloadFile', function (event, data) { event.sender.send('downloadError', error); }) }) + +ipcMain.on('downloadMedia', function (event, data) { + const filePath = data.filePath; + const item = data.item; + const https = require('https'); + const agent = new https.Agent({ + rejectUnauthorized: false + }); + axios({ + method: 'GET', + url: item, + headers: data.header, + responseType: 'stream', + httpsAgent: agent + }).then((response) => { + response.data.pipe(fs.createWriteStream(filePath)) + response.data.on('end', () => { + event.sender.send('downloadMediaEnd'); + }) + response.data.on('error', (error) => { + log.error(`[background.js] (downloadFile) - Failed to download ${item.split('&X-Plex-Token=')[0]}`); + event.sender.send('downloadMediaError', error); + }) + }).catch((error) => { + log.error(`[background.js] (downloadFile) - ${item.split('&X-Plex-Token=')[0]}`); + event.sender.send('downloadMediaError', error); + }) +}) + diff --git a/src/components/modules/Download/scripts/Download.js b/src/components/modules/Download/scripts/Download.js index 0363db8..2502264 100644 --- a/src/components/modules/Download/scripts/Download.js +++ b/src/components/modules/Download/scripts/Download.js @@ -4,19 +4,21 @@ const log = require('electron-log'); console.log = log.log; const fs = require('fs'); const path = require('path'); +//const stream = require('stream') //const controller = new AbortController(); +import {ipcRenderer} from 'electron'; import { wtconfig, wtutils } from '../../General/wtutils'; import { ptv } from '../../General/plextv'; import axios from 'axios'; -import * as stream from 'stream'; +//import * as stream from 'stream'; -fs, path, stream +fs, path, axios const download = new class DOWNLOAD { constructor() { @@ -69,15 +71,34 @@ const download = new class DOWNLOAD { async downloadItem(){ // Download the actual item log.info(`[Download.js] (downloadItem) Started download of file: ${this.item.targetFile}`); // Get the header + console.log('Ged 1-3-3') let header = wtutils.PMSHeader; // Add Auth Token header['X-Plex-Token'] = this.accessToken; // Start by checking, if media is already partially downloaded let rangeStart = 0; - const fs = require('fs'); + // Make a stream writer + let writer; + writer; + /* + let options = { + 'flags': 'a', + 'encoding': null, + 'mode': '0666' + } */ + + console.log('Ged 1-3-4') + console.log('Ged 1-3-4-2', this.item.targetFile) if (fs.existsSync(this.item.targetFile)) { console.log('Ged 12-3 exists Need to adjust start') + } else { + console.log('Ged 12-3-2 New file') + //writer = stream.createWriteStream(this.item.targetFile); + writer = fs.createWriteStream(this.item.targetFile); + + // writer = stream.createWriteStream(this.item.targetFile); } + console.log('Ged 1-3-5') header['Content-Range'] = rangeStart; // Url to download const url = this.item.baseAddress + this.item.key + '?download=1'; @@ -90,10 +111,98 @@ const download = new class DOWNLOAD { let downloadprocentlog = 1; + downloadprocentlog; + this.controller = new AbortController(); this.lastError = null; + const _this = this; + return new Promise((resolve) => { + try + { + _this.isDownloading = true; + ipcRenderer.send('downloadMedia', { + item: url, + filePath: this.item.targetFile, + header: header + }) + } + catch (error) + { + log.error(`etHelper (forceDownload) downloading pic for ${this.item.title} cougth an exception as: ${error}`); + } + + ipcRenderer.on('downloadEnd', () => { + try + { + ipcRenderer.removeAllListeners('downloadEnd'); + ipcRenderer.removeAllListeners('downloadError'); + resolve(this.item.targetFile); + } + catch (error) + { + log.error(`etHelper (forceDownload-downloadEnd) downloading pic for "${this.item.title}" caused an exception as: ${error}`); + } + }) + + }) + } + + + + async downloadItem1(){ // Download the actual item + log.info(`[Download.js] (downloadItem) Started download of file: ${this.item.targetFile}`); + // Get the header + console.log('Ged 1-3-3') + let header = wtutils.PMSHeader; + // Add Auth Token + header['X-Plex-Token'] = this.accessToken; + // Start by checking, if media is already partially downloaded + let rangeStart = 0; + // Make a stream writer + let writer; + writer; + /* + let options = { + 'flags': 'a', + 'encoding': null, + 'mode': '0666' + } */ + + console.log('Ged 1-3-4') + console.log('Ged 1-3-4-2', this.item.targetFile) + if (fs.existsSync(this.item.targetFile)) { + console.log('Ged 12-3 exists Need to adjust start') + } else { + console.log('Ged 12-3-2 New file') + //writer = stream.createWriteStream(this.item.targetFile); + writer = fs.createWriteStream(this.item.targetFile); + + // writer = stream.createWriteStream(this.item.targetFile); + } + console.log('Ged 1-3-5') + header['Content-Range'] = rangeStart; + // Url to download + const url = this.item.baseAddress + this.item.key + '?download=1'; + + console.log('Ged 12-3 url', url) + console.log('Ged 12-4 Header', JSON.stringify(header)) + this.downloadProcent = 0; + + console.log('Ged 88-3-0 Item', JSON.stringify(this.item)) + + let downloadprocentlog = 1; + + downloadprocentlog; + + this.controller = new AbortController(); + this.lastError = null; + + + + // item.targetFile + // const response = await axios({ await axios({ method: 'get', url: url, @@ -111,10 +220,28 @@ const download = new class DOWNLOAD { }, signal: this.controller.signal }) - .then((response) => { + .then((response) => { + response log.debug('[Download.js] (downloadItem) Response from downloadItem recieved'); log.info(`[Download.js] (downloadItem) - Download completed for file: ${this.item.targetFile}`) - log.silly(`downloadItem returned as: ${JSON.stringify(response.data)}`); + //log.silly(`downloadItem returned as: ${JSON.stringify(response.data)}`); + return new Promise((resolve, reject) => { + //writer.write(response.data); + response.data.pipe(writer); + let error = null; + writer.on('error', err => { + error = err; + writer.close(); + reject(err); + }); + writer.on('close', () => { + if (!error) { + resolve(true); + } + //no need to call the reject here, as it will have been called in the + //'error' stream; + }); + }); }) .catch(function (error) { if (error.code == 'ERR_CANCELED'){ @@ -130,8 +257,6 @@ const download = new class DOWNLOAD { log.error('[Download.js] (downloadItem) last error: ' + error.message); } }); - - } async stopProcess(){ // Abort current download @@ -153,6 +278,7 @@ const download = new class DOWNLOAD { await this.getSrvInfo(); await this.createOutDir(); await this.downloadItem(); + if (!this.lastError){ // Remove from queue if no error this.removeFirstEntry(); }