From 861e480b7076fe0e02f96908f4f30ac626722a9a Mon Sep 17 00:00:00 2001 From: 4gray Date: Sat, 12 Sep 2020 11:36:30 +0200 Subject: [PATCH] feat: open playlist from file system --- api.ts | 23 +++++++++++++++++++++++ src/app/app.component.ts | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/api.ts b/api.ts index 78b1c1ebf..f77b0af16 100644 --- a/api.ts +++ b/api.ts @@ -5,6 +5,7 @@ import { guid } from '@datorama/akita'; import { Playlist } from './src/app/playlist-uploader/playlist.interface'; import Nedb from 'nedb-promises-ts'; +const fs = require('fs'); const join = require('path').join; const openAboutWindow = require('about-window').default; const userData = app.getPath('userData'); @@ -70,6 +71,28 @@ export class Api { package_json_dir: __dirname, }); }); + + // open playlist from file system + ipcMain.on('open-file', (event, args) => { + fs.readFile(args.filePath, 'utf-8', (err, data) => { + if (err) { + console.log( + 'An error ocurred reading the file :' + err.message + ); + return; + } + const array = (data as string).split('\n'); + const parsedPlaylist = this.parsePlaylist(array); + const playlistObject = this.createPlaylistObject( + args.fileName, + parsedPlaylist + ); + this.insertToDb(playlistObject); + event.sender.send('parse-response', { + payload: playlistObject, + }); + }); + }); } /** diff --git a/src/app/app.component.ts b/src/app/app.component.ts index a8e75640f..d00afc0e0 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -50,5 +50,24 @@ export class AppComponent { this.router.navigateByUrl('/', { skipLocationChange: true }); }); }); + + if ( + (this.electronService.remote.process.platform === 'linux' || + this.electronService.remote.process.platform === 'win32') && + this.electronService.remote.process.argv.length > 2 + ) { + const filePath = this.electronService.remote.process.argv.find( + (filepath) => + filepath.endsWith('.m3u') || filepath.endsWith('.m3u8') + ); + if (filePath) { + const filePathsArray = filePath.split('/'); + const fileName = filePathsArray[filePathsArray.length - 1]; + this.electronService.ipcRenderer.send('open-file', { + filePath, + fileName, + }); + } + } } }