Skip to content

Commit

Permalink
added download only mode that simply downloads files to the client wh…
Browse files Browse the repository at this point in the history
…en the server finishes the download

added dependency on savefile library for download-only mode
  • Loading branch information
Tzahi12345 committed Feb 11, 2020
1 parent 10c90a0 commit ba55920
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 37 deletions.
17 changes: 14 additions & 3 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ app.post('/fileStatusMp4', function(req, res) {
var fullpath = videoPath + name + ".mp4";
if (fs.existsSync(fullpath)) {
exists = [basePath + videoPath + name, getFileSizeMp4(name)];
}
else
{
} else {
var percent = 0;
var size = getFileSizeMp4(name);
var downloaded = getAmountDownloadedMp4(name);
Expand Down Expand Up @@ -386,6 +384,19 @@ app.post('/deleteMp4', function(req, res) {
}
});

app.post('/downloadFile', function(req, res) {
let fileName = req.body.fileName;
let type = req.body.type;
let file = null;
if (type === 'audio') {
file = __dirname + '/' + 'audio/' + fileName + '.mp3';
} else if (type === 'video') {
file = __dirname + '/' + 'video/' + fileName + '.mp4';
}

res.sendFile(file);
});


app.get('/video/:id', function(req , res){
var head;
Expand Down
3 changes: 2 additions & 1 deletion backend/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"Extra": {
"title_top": "Youtube Downloader",
"file_manager_enabled": true
"download_only_mode": false,
"file_manager_enabled": true
}
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
"@angular/platform-browser-dynamic": "^8.2.11",
"@angular/router": "^8.2.11",
"core-js": "^2.4.1",
"file-saver": "^2.0.2",
"ng4-configure": "^0.1.7",
"rxjs": "^6.5.3",
"rxjs-compat": "^6.0.0-rc.0",
"tslib": "^1.10.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.803.12",
"@angular-devkit/build-angular": "^0.803.24",
"@angular/cli": "^8.3.12",
"@angular/compiler-cli": "^8.2.11",
"@angular/language-service": "^8.2.11",
"@types/file-saver": "^2.0.1",
"@types/jasmine": "2.5.45",
"@types/node": "~6.0.60",
"codelyzer": "^5.0.1",
Expand Down
76 changes: 45 additions & 31 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Observable } from 'rxjs/Observable';
import {FormControl, Validators} from '@angular/forms';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatSnackBar} from '@angular/material';
import { saveAs } from 'file-saver';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/mapTo';
import 'rxjs/add/operator/toPromise';
Expand All @@ -14,7 +15,9 @@ import 'rxjs/add/operator/toPromise';
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
export class AppComponent implements OnInit {
iOS = false;

determinateProgress = false;
downloadingfile = false;
audioOnly: boolean;
Expand All @@ -25,6 +28,7 @@ export class AppComponent {
topBarTitle = 'Youtube Downloader';
percentDownloaded: number;
fileManagerEnabled = false;
downloadOnlyMode = false;

mp3s: any[] = [];
mp4s: any[] = [];
Expand All @@ -35,11 +39,12 @@ export class AppComponent {
this.audioOnly = false;



// loading config
this.postsService.loadNavItems().subscribe(result => { // loads settings
const backendUrl = result['YoutubeDLMaterial']['Host']['backendurl'];
this.topBarTitle = result['YoutubeDLMaterial']['Extra']['title_top'];
this.fileManagerEnabled = result['YoutubeDLMaterial']['Extra']['file_manager_enabled'];
this.downloadOnlyMode = result['YoutubeDLMaterial']['Extra']['download_only_mode'];

this.postsService.path = backendUrl;
this.postsService.startPath = backendUrl;
Expand All @@ -54,30 +59,8 @@ export class AppComponent {
});

}
/*
doHandshake(url: string) {
this.postsService.startHandshake(url).subscribe(theurl => {
this.postsService.path = theurl;
this.postsService.handShakeComplete = true;
console.log('Handshake complete!');
}, error => {
console.log('Initial handshake failed on http.');
this.doHandshakeSSL(url);
});
}

doHandshakeSSL(url: string) {
this.postsService.startHandshakeSSL(url).subscribe(theurl => {
this.postsService.path = theurl;
this.postsService.handShakeComplete = true;
console.log('Handshake complete!');
},
error => {
console.log('Initial handshake failed on https too! Make sure port 17442 is open.');
this.postsService.handShakeComplete = false;
});
}*/
// file manager stuff

getMp3s() {
this.postsService.getMp3s().subscribe(result => {
Expand All @@ -100,9 +83,9 @@ export class AppComponent {

public goToFile(name, isAudio) {
if (isAudio) {
this.downloadHelperMp3(name);
this.downloadHelperMp3(name, true);
} else {
this.downloadHelperMp4(name);
this.downloadHelperMp4(name, true);
}
}

Expand All @@ -124,10 +107,14 @@ export class AppComponent {
}
}

// app initialization.
ngOnInit() {
this.iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window['MSStream'];
}

downloadHelperMp3(name: string) {
// download helpers

downloadHelperMp3(name: string, forceView = false) {
this.postsService.getFileStatusMp3(name).subscribe(fileExists => {
const exists = fileExists;
this.exists = exists[0];
Expand All @@ -140,13 +127,25 @@ export class AppComponent {
}
setTimeout(() => this.downloadHelperMp3(name), 500);
} else {
window.location.href = this.exists;
// if download only mode, just download the file. no redirect
if (forceView === false && this.downloadOnlyMode && !this.iOS) {
this.postsService.downloadFileFromServer(name, 'audio').subscribe(res => {
const blob: Blob = res;
saveAs(blob, name + '.mp3');
this.downloadingfile = false;
});
} else {
window.location.href = this.exists;
}

// reloads mp3s
this.getMp3s();
}
});

}

downloadHelperMp4(name: string) {
downloadHelperMp4(name: string, forceView = false) {
this.postsService.getFileStatusMp4(name).subscribe(fileExists => {
const exists = fileExists;
this.exists = exists[0];
Expand All @@ -158,12 +157,25 @@ export class AppComponent {
}
setTimeout(() => this.downloadHelperMp4(name), 500);
} else {
window.location.href = this.exists;
// if download only mode, just download the file. no redirect
if (forceView === false && this.downloadOnlyMode) {
this.postsService.downloadFileFromServer(name, 'video').subscribe(res => {
const blob: Blob = res;
saveAs(blob, name + '.mp4');
this.downloadingfile = false;
});
} else {
window.location.href = this.exists;
}

// reloads mp4s
this.getMp4s();
}
});

}

// download click handler
downloadClicked() {
if (this.ValidURL(this.url)) {
this.urlError = false;
Expand Down Expand Up @@ -197,13 +209,15 @@ export class AppComponent {
}
}

// checks if url is a valid URL
ValidURL(str) {
// tslint:disable-next-line: max-line-length
const strRegex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/;
const re = new RegExp(strRegex);
return re.test(str);
}

// snackbar helper
public openSnackBar(message: string, action: string) {
this.snackBar.open(message, action, {
duration: 2000,
Expand Down
6 changes: 5 additions & 1 deletion src/app/posts.services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Injectable, isDevMode} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpHeaders, HttpRequest, HttpResponseBase } from '@angular/common/http';
import config from '../assets/default.json';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
Expand Down Expand Up @@ -75,6 +75,10 @@ export class PostsService {
getMp4s() {
return this.http.post(this.path + 'getMp4s', {});
}

downloadFileFromServer(fileName, type) {
return this.http.post(this.path + 'downloadFile', {fileName: fileName, type: type}, {responseType: 'blob'});
}
}


Expand Down

0 comments on commit ba55920

Please sign in to comment.