<commit_before>#! /usr/bin/env node
"use strict";
/* jshint esnext:true, node:true */

const yazl = require('yazl')
  , messages_pb = require('protocolbuffers/messages_pb')
  , {StorageKey, File, Files} = messages_pb
  , { status: grpcStatus } = require('grpc')
  ;

/**
 *
 */
function StorageDownload(server, app, logging, storages /* { e.g.: cache, persistence } */) {
    this._server = server;
    this._app = app;// === express()
    this._log = logging;
    this._storages = storages;
    this._knownTypes = messages_pb;
    this._app.get('/:storage/:key_extension', this._download.bind(this));
}

const _p = StorageDownload.prototype;
_p.constructor = StorageDownload;

function _parseKeyExt(keyExt){
    var dotPos = keyExt.indexOf('.')
      , key, ext
      ;
    if(dotPos === -1)
        return [null, null, 'Extension not found'];
    key = keyExt.slice(0, dotPos);
    ext = keyExt.slice(dotPos+1);
    // more checks?
    return [key, ext, null];
}


function getZipableTypeName(message) {
    var zippableTypes = { file: File, files: Files };
    return getTypeName(zippableTypes, message);
}
function getTypeName(knownTypes, message) {
    // as in protobufAnyHandler.getTypeNameForMessage(message)
    for(let [typeName, Ctor] of Object.entries(knownTypes) ) {
        if(message instanceof Ctor)
            return typeName;
    }
    return null;
}

function _zipAndSendMessage(res, message, filename) {
    res.setHeader('Content-Type', 'application/zip');
    // mark as downloadable file
    res.setHeader('Content-Disposition'
                            , 'attachment; filename=' + filename);
    var zipfile = new yazl.ZipFile()
      , files
      ;

    if(message instanceof Files)
        files = message.getFilesList();
    else
        // getName and getData_asU8 are expected
        files = [message];

    for(let file of files)
        zipfile.addBuffer(Buffer.from(file.getData_asU8()), file.getName());

    zipfile.outputStream.pipe(res);
    zipfile.end();
}

/**
 * GET
 *
 */
_p._download = function(req, res, next) {
    // jshint unused:vars
    var storage, errorMessage, key, extension, filename
      , storageKey, typeName, contentType, data;

    if(!(req.params.storage in this._storages)) {<commit_msg>[PRDispatcher] WIP apiServices/StorageDownload add support to zip-download the files of a FamilyData message.<commit_after>#! /usr/bin/env node
"use strict";
/* jshint esnext:true, node:true */

const yazl = require('yazl')
  , messages_pb = require('protocolbuffers/messages_pb')
  , {StorageKey, File, Files, FamilyData} = messages_pb
  , { status: grpcStatus } = require('grpc')
  ;

/**
 *
 */
function StorageDownload(server, app, logging, storages /* { e.g.: cache, persistence } */) {
    this._server = server;
    this._app = app;// === express()
    this._log = logging;
    this._storages = storages;
    this._knownTypes = messages_pb;
    this._app.get('/:storage/:key_extension', this._download.bind(this));
}

const _p = StorageDownload.prototype;
_p.constructor = StorageDownload;

function _parseKeyExt(keyExt){
    var dotPos = keyExt.indexOf('.')
      , key, ext
      ;
    if(dotPos === -1)
        return [null, null, 'Extension not found'];
    key = keyExt.slice(0, dotPos);
    ext = keyExt.slice(dotPos+1);
    // more checks?
    return [key, ext, null];
}


function getZipableTypeName(message) {
    var zippableTypes = { file: File, files: Files , familyData: FamilyData};
    return getTypeName(zippableTypes, message);
}
function getTypeName(knownTypes, message) {
    // as in protobufAnyHandler.getTypeNameForMessage(message)
    for(let [typeName, Ctor] of Object.entries(knownTypes) ) {
        if(message instanceof Ctor)
            return typeName;
    }
    return null;
}

function _zipAndSendMessage(res, message, filename) {
    res.setHeader('Content-Type', 'application/zip');
    // mark as downloadable file
    res.setHeader('Content-Disposition'
                            , 'attachment; filename=' + filename);
    var zipfile = new yazl.ZipFile()
      , files
      ;

    if(message instanceof FamilyData)
        files = message.getFiles().getFilesList();
    else if(message instanceof Files)
        files = message.getFilesList();
    else
        // getName and getData_asU8 are expected
        files = [message];

    for(let file of files)
        zipfile.addBuffer(Buffer.from(file.getData_asU8()), file.getName());

    zipfile.outputStream.pipe(res);
    zipfile.end();
}

/**
 * GET
 *
 */
_p._download = function(req, res, next) {
    // jshint unused:vars
    var storage, errorMessage, key, extension, filename
      , storageKey, typeName, contentType, data;

    if(!(req.params.storage in this._storages)) {
{'fraction_non_alphanumeric': 0.13476975073933248, 'fraction_numerical': 0.0021123785382340513, 'mean_word_length': 3.117391304347826, '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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '256283', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1482, 'n_words': 343}