{"size": 5432, "ext": "js", "max_stars_repo_path": "mediaserver/api/subroutines/playlistArtGenerator.js", "max_stars_repo_name": "Mynk-9/beautplayer", "max_stars_repo_stars_event_min_datetime": "2021-06-11T14:10:00.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-02T20:25:04.000Z", "max_issues_repo_path": "mediaserver/api/subroutines/playlistArtGenerator.js", "max_issues_repo_name": "Mynk-9/beautplayer", "max_issues_repo_issues_event_min_datetime": "2021-06-11T09:31:28.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-11T09:31:28.000Z", "max_forks_repo_path": "mediaserver/api/subroutines/playlistArtGenerator.js", "max_forks_repo_name": "Mynk-9/beautplayer", "max_forks_repo_forks_event_min_datetime": "2021-12-21T13:29:39.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-21T13:29:39.000Z"}
{"max_stars_count": 3, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 40.8421052632, "max_line_length": 116, "alphanum_fraction": 0.4248895434}
const fs = require('fs');
const path = require('path');
const createCollage = require('nf-photo-collage');

const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');

const Playlists = require('../models/playlists');

function getAlbumArtFilePath(name) {
    name = name.replace(/[^a-z0-9]/gi, '_').toLowerCase();  // thanks to https://stackoverflow.com/a/8485137/6262571
    return path.join(__dirname, `/../../public/coverArt/${name}.jpg`);
}
function getPlaylistArtPath(name, isCompressed = false, directoryOnly = false) {
    name = name.replace(/[^a-z0-9]/gi, '_').toLowerCase();  // thanks to https://stackoverflow.com/a/8485137/6262571

    if (directoryOnly)
        if (isCompressed)
            return (path.join(__dirname, `/../../public/coverArt/playlists/compressed`));
        else
            return (path.join(__dirname, `/../../public/coverArt/playlists`));
    else
        if (isCompressed)
            return (path.join(__dirname, `/../../public/coverArt/playlists/compressed/${name}.jpg`));
        else
            return (path.join(__dirname, `/../../public/coverArt/playlists/${name}.jpg`));
}

module.exports = (plName) => {
    Playlists.findById(plName)
        .populate('tracks')
        .then(async playlist => {
            const tracks = playlist.tracks;
            let options;

            // imageWidth and imageHeight properties are for individual images
            switch (tracks.length) {
                case 0:
                    break;
                case 1:
                    options = {
                        sources: [
                            getAlbumArtFilePath(playlist.tracks[0].album)
                        ],
                        width: 1,
                        height: 1,
                        imageWidth: 500,
                        imageHeight: 500,
                        backgroundImage: '',
                        spacing: 0
                    };
                    break;
                case 2:
                    options = {
                        sources: [
                            getAlbumArtFilePath(playlist.tracks[0].album),
                            getAlbumArtFilePath(playlist.tracks[1].album)
                        ],
                        width: 2,
                        height: 1,
                        imageWidth: 500,
                        imageHeight: 500,
                        backgroundImage: '',
                        spacing: 0
                    };
                    break;
                case 3:
                    options = {
                        sources: [
                            getAlbumArtFilePath(playlist.tracks[0].album),
                            getAlbumArtFilePath(playlist.tracks[1].album),
                            getAlbumArtFilePath(playlist.tracks[2].album)
                        ],
                        width: 2,
                        height: 2,
                        imageWidth: 500,
                        imageHeight: 500,
                        backgroundImage: '',
                        spacing: 0
                    };
                    break;
                default:
                    options = {
                        sources: [
                            getAlbumArtFilePath(playlist.tracks[0].album),
                            getAlbumArtFilePath(playlist.tracks[1].album),
                            getAlbumArtFilePath(playlist.tracks[2].album),
                            getAlbumArtFilePath(playlist.tracks[3].album)
                        ],
                        width: 2,
                        height: 2,
                        imageWidth: 500,
                        imageHeight: 500,
                        backgroundImage: '',
                        spacing: 0
                    };
                    break;
            }

            if (options)
                await createCollage(options)
                    .then(async canvas => {
                        const src = canvas.jpegStream();
                        const dest = fs.createWriteStream(getPlaylistArtPath(plName));
                        src.pipe(dest);
                        dest.on('close', async () => {
                            // imagemin module has support problems with windows forward-slash paths
                            // https://github.com/imagemin/imagemin/issues/352
                            // hence if platform is windows, we replace backward slashes with forward
                            // https://github.com/imagemin/imagemin/issues/352#issuecomment-682215303

                            let playlistArtPath = getPlaylistArtPath(plName, false, false);
                            if (process.platform == 'win32')
                                playlistArtPath = playlistArtPath.replace(/\\/g, '/');

                            await imagemin([playlistArtPath], {
                                destination: getPlaylistArtPath("", true, true),
                                plugins: [
                                    imageminMozjpeg({ quality: 10 })
                                ]
                            });
                        })
                    })
                    .catch(err => {
                        console.log(err);
                    });
        })
        .catch(err => {
            console.log(err);
            throw err;
        });
};
