Skip to content

Commit

Permalink
feat: open playlist from file system
Browse files Browse the repository at this point in the history
  • Loading branch information
4gray committed Sep 12, 2020
1 parent 5a12c6c commit 861e480
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
});
});
});
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
}
}

0 comments on commit 861e480

Please sign in to comment.