<commit_before>/**
 * Created by elad.benedict on 8/12/2015.
 */

var m3u8Parser = require('./promise-m3u8');
var loggerDecorator = require('./utils/log-decorator');
var qio = require('q-io/fs');
var Q = require('q');
var path = require('path');
var _ = require('underscore');

module.exports = function(outputFolderPath, filename, dvrWindowSize, logger){

    var messageDecoration = function(msg) {
        return "Chunklist manifest generator: " + msg;
    };
    var decoratedLogger = loggerDecorator(logger, messageDecoration);

    var currentManifest;
    var shouldAddDiscontinuityTag = false;

    var manifestFullPath = path.join(outputFolderPath, filename);

    var readCurrentManifest = function readCurrentManifest(){
        return m3u8Parser.parseM3U8(manifestFullPath);
    };

    var writeCurrentManifest = function writeCurrentManifest(){
        logger.debug('Writing manifest ' + manifestFullPath + ': ' + currentManifest.toString());
        return qio.write(manifestFullPath, currentManifest.toString());
    };

    // Generate an empty M3U8 or read the existing manifest from disk
    var init = function init()
    {
        var manifestPath = path.join(outputFolderPath, filename);
        return qio.exists(manifestPath).then(function(result)
        {
            var currentManifest;
            if (!result) {
                currentManifest = createEmptyM3U8();
                return qio.write(manifestPath, currentManifest.toString()).then(function()
                {
                    return currentManifest;
                });
            }
            else
            {
                // A manifest already exists - add a discontinuity tag upon next chunk addition
                shouldAddDiscontinuityTag = true;
                return readCurrentManifest();
            }
        }).then(function(manifest)
        {
            // Update in-memory manifest
            currentManifest = manifest;
            return currentManifest;
        });
    };

    var createEmptyM3U8 = function createEmptyM3U8()
    {
        var manifest = new m3u8Parser.M3U();
        manifest.set("allowCache", "NO");
        manifest.set("version", 3);
        manifest.set("mediaSequence", 0);
        manifest.set("targetDuration", 0);
        return manifest;
    };

    var update = function updateChunklist(newItems) {

        // 1. Read or create chunklist
        // 2. Add discontinuity if relevant
        // 3. Append chunks
        // 4. Update media sequence number
        // 5. Add EXT-X-ENDLIST if relevant<commit_msg>Temporarily disabled since our player currently doesn't support this tag<commit_after>/**
 * Created by elad.benedict on 8/12/2015.
 */

var m3u8Parser = require('./promise-m3u8');
var loggerDecorator = require('./utils/log-decorator');
var qio = require('q-io/fs');
var Q = require('q');
var path = require('path');
var _ = require('underscore');

module.exports = function(outputFolderPath, filename, dvrWindowSize, logger){

    var messageDecoration = function(msg) {
        return "Chunklist manifest generator: " + msg;
    };
    var decoratedLogger = loggerDecorator(logger, messageDecoration);

    var currentManifest;
    var shouldAddDiscontinuityTag = false;

    var manifestFullPath = path.join(outputFolderPath, filename);

    var readCurrentManifest = function readCurrentManifest(){
        return m3u8Parser.parseM3U8(manifestFullPath);
    };

    var writeCurrentManifest = function writeCurrentManifest(){
        logger.debug('Writing manifest ' + manifestFullPath + ': ' + currentManifest.toString());
        return qio.write(manifestFullPath, currentManifest.toString());
    };

    // Generate an empty M3U8 or read the existing manifest from disk
    var init = function init()
    {
        var manifestPath = path.join(outputFolderPath, filename);
        return qio.exists(manifestPath).then(function(result)
        {
            var currentManifest;
            if (!result) {
                currentManifest = createEmptyM3U8();
                return qio.write(manifestPath, currentManifest.toString()).then(function()
                {
                    return currentManifest;
                });
            }
            else
            {
                // A manifest already exists - add a discontinuity tag upon next chunk addition
                // *** Temporarily disabled since our player currently doesn't support this tag **
                shouldAddDiscontinuityTag = false; // should have been true
                return readCurrentManifest();
            }
        }).then(function(manifest)
        {
            // Update in-memory manifest
            currentManifest = manifest;
            return currentManifest;
        });
    };

    var createEmptyM3U8 = function createEmptyM3U8()
    {
        var manifest = new m3u8Parser.M3U();
        manifest.set("allowCache", "NO");
        manifest.set("version", 3);
        manifest.set("mediaSequence", 0);
        manifest.set("targetDuration", 0);
        return manifest;
    };

    var update = function updateChunklist(newItems) {

        // 1. Read or create chunklist
        // 2. Add discontinuity if relevant
        // 3. Append chunks
        // 4. Update media sequence number
        // 5. Add EXT-X-ENDLIST if relevant
{'fraction_non_alphanumeric': 0.10255927738050433, 'fraction_numerical': 0.01279638690252164, 'mean_word_length': 2.3135910224438905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3290134', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1369, 'n_words': 358}