<commit_before>
var config = require('../config');
var images = require('collections-online/lib/controllers/images');
var es = require('collections-online/lib/services/elasticsearch');
var Q = require('q');
var fs = require('fs');
var path = require('path');
var Canvas = require('canvas')
var Image = Canvas.Image;
const Transform = require('stream').Transform;

const POSSIBLE_SIZES = ['lille', 'mellem', 'stor', 'originalJPEG', 'original'];
const WATERMARK_SCALE = 0.33; // 20% of the width of the thumbnail
const THUMBNAIL_SIZE = 350;
// Looping through the licenses to find the on
const LICENSE_IDS_NEEDING_WATERMARK = config.licenseMapping
.map((license, licenseId) => {
  return {
    id: licenseId,
    watermark: license.watermark
  };
})
.filter(license => license.watermark)
.map(license => license.id);

// Resolving the watermark path relative to the app dir's images dir
var watermarkPath = path.normalize(config.appDir + '/images/watermarks');
const WATERMARK_BUFFERS = {
  'kbh-museum': fs.readFileSync(watermarkPath + '/kbh-museum.png'),
  'kbh-arkiv': fs.readFileSync(watermarkPath + '/kbh-arkiv.png'),
}

exports.download = function(req, res, next) {
  var catalog = req.params.catalog;
  var id = req.params.id;
  var size = req.params.size;

  var url = config.cip.baseURL + '/asset/download/' + catalog + '/' + id;

  if (size && POSSIBLE_SIZES.indexOf(size) !== -1) {
    url += '?options=' + size;
  } else {
    throw new Error('The size is required and must be one of ' +
                    POSSIBLE_SIZES +
                    ' given: "' + size + '"');
  }

  images.proxy(url, next).pipe(res);
}

function bottomRightPosition(img, watermarkImg) {
  var watermarkRatio = watermarkImg.height / watermarkImg.width;

  var watermarkWidth = img.width * WATERMARK_SCALE;
  var watermarkHeight = watermarkWidth * watermarkRatio;

  return {<commit_msg>Give the file a proper file extension when downloading it<commit_after>
var config = require('../config');
var images = require('collections-online/lib/controllers/images');
var es = require('collections-online/lib/services/elasticsearch');
var Q = require('q');
var fs = require('fs');
var path = require('path');
var Canvas = require('canvas')
var Image = Canvas.Image;
const Transform = require('stream').Transform;

const POSSIBLE_SIZES = ['lille', 'mellem', 'stor', 'originalJPEG', 'original'];
const WATERMARK_SCALE = 0.33; // 20% of the width of the thumbnail
const THUMBNAIL_SIZE = 350;
// Looping through the licenses to find the on
const LICENSE_IDS_NEEDING_WATERMARK = config.licenseMapping
.map((license, licenseId) => {
  return {
    id: licenseId,
    watermark: license.watermark
  };
})
.filter(license => license.watermark)
.map(license => license.id);

// Resolving the watermark path relative to the app dir's images dir
var watermarkPath = path.normalize(config.appDir + '/images/watermarks');
const WATERMARK_BUFFERS = {
  'kbh-museum': fs.readFileSync(watermarkPath + '/kbh-museum.png'),
  'kbh-arkiv': fs.readFileSync(watermarkPath + '/kbh-arkiv.png'),
}

const contentDispositionRegexp = /.*\.([^.]+)$/i;

exports.download = function(req, res, next) {
  var catalog = req.params.catalog;
  var id = req.params.id;
  var size = req.params.size;

  var url = config.cip.baseURL + '/asset/download/' + catalog + '/' + id;

  if (size && POSSIBLE_SIZES.indexOf(size) !== -1) {
    url += '?options=' + size;
  } else {
    throw new Error('The size is required and must be one of ' +
                    POSSIBLE_SIZES +
                    ' given: "' + size + '"');
  }

  var proxyRequest = images.proxy(url, next);

  res._writeHead = res.writeHead;
  res.writeHead = function(statusCode, reasonPhrase, headers) {
    // Reading the file extension from the response from CIP
    var responseHeaders = proxyRequest.response.headers;
    var contentDispositionHeader = responseHeaders['content-disposition'] || '';
    var extension = contentDispositionHeader.match(contentDispositionRegexp);
    if(extension) {
      extension = '.' + extension[1];
    } else {
      extension = '.jpg'; // Default extension, when the CIP is not responsing
    }
    // Generating a new filename
    var filename = catalog + '-' + id + extension;
    res.header('content-disposition', 'attachment; filename=' + filename);
    res._writeHead(statusCode, reasonPhrase, headers);
  };

  proxyRequest.pipe(res);
}

function bottomRightPosition(img, watermarkImg) {
  var watermarkRatio = watermarkImg.height / watermarkImg.width;

  var watermarkWidth = img.width * WATERMARK_SCALE;
  var watermarkHeight = watermarkWidth * watermarkRatio;

  return {
{'fraction_non_alphanumeric': 0.13903281519861832, 'fraction_numerical': 0.004101899827288429, 'mean_word_length': 3.918259023354565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1152296', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1454, 'n_words': 373}